Coverage for src / lexigram / contracts / web / routing.py: 50%
18 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-19 05:41 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-19 05:41 +0800
1"""HTTP route metadata decorators.
3These decorators attach route configuration to handler functions so that
4any controller — regardless of which extension package it lives in — can
5declare routes without importing from ``lexigram-web``.
7The decorators themselves have zero dependencies: they only write a
8``_route_config`` dict onto the decorated callable.
10Example::
12 from lexigram.contracts.web.routing import get, post
14 class MyController(ControllerProtocol):
15 @get("/items")
16 async def list_items(self) -> list: ...
18 @post("/items")
19 async def create_item(self, body: Item) -> Item: ...
20"""
22from __future__ import annotations
24from typing import TYPE_CHECKING, Any
26if TYPE_CHECKING:
27 from collections.abc import Callable
30def _route(method: str, path: str, **kwargs: Any) -> Callable[..., Any]:
31 """Create a route decorator for the given HTTP method.
33 Args:
34 method: HTTP method string (``"GET"``, ``"POST"``, etc.).
35 path: URL path pattern for the route.
36 **kwargs: Additional route configuration (e.g. ``summary``, ``tags``).
38 Returns:
39 A decorator that stamps ``_route_config`` onto the handler function.
40 """
42 def decorator(func: Any) -> Any:
43 func._route_config = {"method": method, "path": path, **kwargs}
44 return func
46 return decorator
49def get(path: str, **kwargs: Any) -> Callable[..., Any]:
50 """Declare a GET route handler.
52 Args:
53 path: URL path pattern.
54 **kwargs: Extra route configuration.
56 Returns:
57 Route decorator.
58 """
59 return _route("GET", path, **kwargs)
62def post(path: str, **kwargs: Any) -> Callable[..., Any]:
63 """Declare a POST route handler.
65 Args:
66 path: URL path pattern.
67 **kwargs: Extra route configuration.
69 Returns:
70 Route decorator.
71 """
72 return _route("POST", path, **kwargs)
75def put(path: str, **kwargs: Any) -> Callable[..., Any]:
76 """Declare a PUT route handler.
78 Args:
79 path: URL path pattern.
80 **kwargs: Extra route configuration.
82 Returns:
83 Route decorator.
84 """
85 return _route("PUT", path, **kwargs)
88def delete(path: str, **kwargs: Any) -> Callable[..., Any]:
89 """Declare a DELETE route handler.
91 Args:
92 path: URL path pattern.
93 **kwargs: Extra route configuration.
95 Returns:
96 Route decorator.
97 """
98 return _route("DELETE", path, **kwargs)
101def patch(path: str, **kwargs: Any) -> Callable[..., Any]:
102 """Declare a PATCH route handler.
104 Args:
105 path: URL path pattern.
106 **kwargs: Extra route configuration.
108 Returns:
109 Route decorator.
110 """
111 return _route("PATCH", path, **kwargs)
114__all__ = ["delete", "get", "patch", "post", "put"]