Skip to content

routes

Route/endpoint related items.

CompiledRoute

A route that has been compiled to include uri variables.

Parameters:

Name Type Description Default
route Route

The route to compile.

required
Source code in wom/routes.py
class CompiledRoute:
    """A route that has been compiled to include uri variables.

    Args:
        route: The route to compile.
    """

    __slots__ = ("_route", "_uri", "_params")

    def __init__(self, route: Route, uri: str) -> None:
        self._uri = uri
        self._route = route
        self._params: t.Dict[str, t.Union[str, int]] = {}

    @property
    def route(self) -> Route:
        """The route itself."""
        return self._route

    @property
    def uri(self) -> str:
        """The routes uri endpoint."""
        return self._uri

    @uri.setter
    def uri(self, uri: str) -> None:
        self._uri = uri

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

    @property
    def params(self) -> t.Dict[str, t.Union[str, int]]:
        """The query params for the route."""
        return self._params

    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 property

params: Dict[str, Union[str, int]]

The query params for the route.

route property

route: Route

The route itself.

uri property writable

uri: str

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 Dict[str, Any]

The query params to compile.

required

Returns:

Type Description
CompiledRoute

The compiled route for chained calls.

Source code in wom/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 wom/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 Union[str, int]

The arguments to insert into the uri.

()

Returns:

Type Description
CompiledRoute

The compiled route.

Source code in wom/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