Updated route documentation for multi-handler paths and per-method API changes.
+22
-3
@@ -33,6 +33,22 @@ async def config_page(ctx: RouteContext) -> dict:
|
||||
return {"current_config": ctx.config.as_dict()}
|
||||
```
|
||||
|
||||
Multiple handlers can be registered on the same path with different methods. This allows splitting logic across separate functions instead of branching on `ctx.request.method`:
|
||||
|
||||
```python
|
||||
@on_route("/data", methods=["GET"])
|
||||
async def get_data(ctx: RouteContext) -> dict:
|
||||
return {"items": [1, 2, 3]}
|
||||
|
||||
@on_route("/data", methods=["POST"])
|
||||
async def create_data(ctx: RouteContext) -> dict:
|
||||
body = await ctx.request.json()
|
||||
# ... store the data
|
||||
return {"created": True}
|
||||
```
|
||||
|
||||
Each handler's method set must not overlap with any other handler on the same path. Registering a handler whose methods conflict with an existing one raises `ValueError`.
|
||||
|
||||
## Namespacing
|
||||
|
||||
Routes are automatically prefixed under `/owlbot/<module_name>/`. So if the module `stats` registers `@on_route("/dashboard")`, the actual URL is:
|
||||
@@ -249,9 +265,12 @@ async def setup(ctx: ModuleContext) -> None:
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `ctx.routes.register(path, handler, methods=...)` | Register a route. Accepts the same parameters as `@on_route`. Returns `RouteInfo`. |
|
||||
| `ctx.routes.unregister(path)` | Remove a route by path. Returns `True` if found. |
|
||||
| `ctx.routes.get(path)` | Look up a `RouteInfo` by path. Returns `None` if not found. |
|
||||
| `ctx.routes.exists(path)` | Check if a route exists at the path. |
|
||||
| `ctx.routes.unregister(path)` | Remove all handlers at the path. Returns `True` if found. |
|
||||
| `ctx.routes.unregister(path, method=...)` | Remove only the handler covering that method. Returns `True` if found. |
|
||||
| `ctx.routes.get(path)` | Get all handlers at the path. Returns `list[RouteInfo]`. |
|
||||
| `ctx.routes.get(path, method=...)` | Get the handler for a specific method. Returns `RouteInfo \| None`. |
|
||||
| `ctx.routes.exists(path)` | Check if any handler is registered at the path. |
|
||||
| `ctx.routes.exists(path, method=...)` | Check if a handler for a specific method exists. |
|
||||
| `ctx.routes.url_for(path)` | Build a full public URL for a route. |
|
||||
| `ctx.routes.module_routes` | Property: list of all `RouteInfo` for the module. |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user