Skip to content

routes

CompiledRoute

A route that has been compiled to include uri variables.

Parameters:

Name Type Description Default
route Route

The route itself.

required
uri str

The endpoint for this route.

required
Source code in unkey/routes.py
@attrs.define(weakref_slot=False)
class CompiledRoute:
    """A route that has been compiled to include uri variables.

    Args:
        route: The route itself.
        uri: The endpoint for this route.
    """

    route: Route
    """The route itself."""

    uri: str
    """The routes uri endpoint."""

    params: t.Dict[str, t.Union[str, int]] = attrs.field(init=False)
    """The query params for the route."""

    def __init__(self, route: Route, uri: str) -> None:
        self.route = route
        self.uri = uri
        self.params = {}

    @property
    def method(self) -> str:
        """The routes method, i.e. GET, POST..."""
        return self.route.method

    def with_params(self, params: t.Dict[str, t.Any]) -> CompiledRoute:
        """Adds additional query params to this compiled route.

        Args:
            params: The query params to compile.

        Returns:
            The compiled route for chained calls.
        """
        if params:
            self.params.update(params)

        return self

method property

method: str

The routes method, i.e. GET, POST...

params class-attribute instance-attribute

params: t.Dict[str, t.Union[str, int]] = {}

The query params for the route.

route instance-attribute

route: Route = route

The route itself.

uri instance-attribute

uri: str = uri

The routes uri endpoint.

with_params

with_params(params: t.Dict[str, t.Any]) -> CompiledRoute

Adds additional query params to this compiled route.

Parameters:

Name Type Description Default
params t.Dict[str, t.Any]

The query params to compile.

required

Returns:

Type Description
CompiledRoute

The compiled route for chained calls.

Source code in unkey/routes.py
def with_params(self, params: t.Dict[str, t.Any]) -> CompiledRoute:
    """Adds additional query params to this compiled route.

    Args:
        params: The query params to compile.

    Returns:
        The compiled route for chained calls.
    """
    if params:
        self.params.update(params)

    return self

Route

A route that has not been compiled yet.

Source code in unkey/routes.py
@attrs.define(weakref_slot=False)
class Route:
    """A route that has not been compiled yet."""

    method: str
    """The request method to use."""

    uri: str
    """The request uri."""

    def compile(self, *args: t.Union[str, int]) -> CompiledRoute:
        """Turn this route into a compiled route.

        Args:
            *args: The arguments to insert into the uri.

        Returns:
            The compiled route.
        """
        compiled = CompiledRoute(self, self.uri)

        for arg in args:
            compiled.uri = compiled.uri.replace(r"{}", str(arg), 1)

        return compiled

method instance-attribute

method: str

The request method to use.

uri instance-attribute

uri: str

The request uri.

compile

compile(*args: t.Union[str, int]) -> CompiledRoute

Turn this route into a compiled route.

Parameters:

Name Type Description Default
*args t.Union[str, int]

The arguments to insert into the uri.

()

Returns:

Type Description
CompiledRoute

The compiled route.

Source code in unkey/routes.py
def compile(self, *args: t.Union[str, int]) -> CompiledRoute:
    """Turn this route into a compiled route.

    Args:
        *args: The arguments to insert into the uri.

    Returns:
        The compiled route.
    """
    compiled = CompiledRoute(self, self.uri)

    for arg in args:
        compiled.uri = compiled.uri.replace(r"{}", str(arg), 1)

    return compiled