Skip to content

route_table.py

Provides routing table class.

RouteTable

Stores and manages registered routes.

Provides lookup for handlers based on URL and HTTP method.

Source code in jetweb/routing/route_table.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class RouteTable:
    """
    Stores and manages registered routes.

    Provides lookup for handlers based on URL and HTTP method.
    """

    def __init__(self):
        self.routes = []

    def include(self, prefix: str, route_table: RouteTable) -> None:
        """
        Include routes from another table under a prefix.

        :param prefix: Endpoint prefix.
        :param route_table: Route table with routes for including.
        """
        for route in route_table.routes:
            self.routes.append(
                Route(endpoint=prefix + route.endpoint, handler=route.handler, methods=route.methods)
            )

    def add_route(
        self, prefix: str, endpoint: str, handler: Callable, methods: Union[Iterable[str], None] = None
    ) -> None:
        """
        Register a new route.

        :param prefix: Endpoint prefix.
        :param endpoint: Route endpoint.
        :param handler: Request handler.
        :param methods: Allowed request methods (default: ["GET"]). Adds "OPTIONS" if GET is allowed.
        """
        methods = [method.upper() for method in (methods or ["GET"])]
        if "GET" in methods:
            methods.append("OPTIONS")

        self.routes.append(
            Route(endpoint=prefix + endpoint, handler=handler, methods=methods)
        )

    def find_handler(self, endpoint: str, method: str) -> tuple[Callable, dict]:
        """
        Find a request handler for the given endpoint and method.

        :param endpoint: Request endpoint.
        :param method: Request method.
        :returns: Request handler and parsed path parameters.
        :raises HTTPException(404): If no route matches for endpoint.
        :raises HTTPException(405): If no method matches for matched route.
        """
        for route in self.routes:
            matched, path_params = route.match_endpoint(endpoint)
            if matched:
                if route.match_method(method):
                    return route.handler, path_params
                raise HTTPException(status=405)
        raise HTTPException(status=404)

add_route(prefix, endpoint, handler, methods=None)

Register a new route.

Parameters:

Name Type Description Default
prefix str

Endpoint prefix.

required
endpoint str

Route endpoint.

required
handler Callable

Request handler.

required
methods Union[Iterable[str], None]

Allowed request methods (default: ["GET"]). Adds "OPTIONS" if GET is allowed.

None
Source code in jetweb/routing/route_table.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def add_route(
    self, prefix: str, endpoint: str, handler: Callable, methods: Union[Iterable[str], None] = None
) -> None:
    """
    Register a new route.

    :param prefix: Endpoint prefix.
    :param endpoint: Route endpoint.
    :param handler: Request handler.
    :param methods: Allowed request methods (default: ["GET"]). Adds "OPTIONS" if GET is allowed.
    """
    methods = [method.upper() for method in (methods or ["GET"])]
    if "GET" in methods:
        methods.append("OPTIONS")

    self.routes.append(
        Route(endpoint=prefix + endpoint, handler=handler, methods=methods)
    )

find_handler(endpoint, method)

Find a request handler for the given endpoint and method.

Parameters:

Name Type Description Default
endpoint str

Request endpoint.

required
method str

Request method.

required

Returns:

Type Description
tuple[Callable, dict]

Request handler and parsed path parameters.

Raises:

Type Description
HTTPException(404)

If no route matches for endpoint.

HTTPException(405)

If no method matches for matched route.

Source code in jetweb/routing/route_table.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def find_handler(self, endpoint: str, method: str) -> tuple[Callable, dict]:
    """
    Find a request handler for the given endpoint and method.

    :param endpoint: Request endpoint.
    :param method: Request method.
    :returns: Request handler and parsed path parameters.
    :raises HTTPException(404): If no route matches for endpoint.
    :raises HTTPException(405): If no method matches for matched route.
    """
    for route in self.routes:
        matched, path_params = route.match_endpoint(endpoint)
        if matched:
            if route.match_method(method):
                return route.handler, path_params
            raise HTTPException(status=405)
    raise HTTPException(status=404)

include(prefix, route_table)

Include routes from another table under a prefix.

Parameters:

Name Type Description Default
prefix str

Endpoint prefix.

required
route_table RouteTable

Route table with routes for including.

required
Source code in jetweb/routing/route_table.py
23
24
25
26
27
28
29
30
31
32
33
def include(self, prefix: str, route_table: RouteTable) -> None:
    """
    Include routes from another table under a prefix.

    :param prefix: Endpoint prefix.
    :param route_table: Route table with routes for including.
    """
    for route in route_table.routes:
        self.routes.append(
            Route(endpoint=prefix + route.endpoint, handler=route.handler, methods=route.methods)
        )