Router Classes¶
The router system provides powerful routing capabilities including subrouting and modular route organization.
Router¶
Router ¶
Router class for handling route registration and resolution.
Provides methods for registering routes with different HTTP methods, mounting subrouters, and resolving incoming requests to appropriate handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
Optional path prefix for all routes in this router |
''
|
Attributes:
Name | Type | Description |
---|---|---|
prefix |
Path prefix for this router |
|
routes |
dict[str, dict[str, Route]]
|
Dictionary of registered routes |
subrouters |
dict[str, Router]
|
Dictionary of mounted subrouters |
Source code in src/artanis/routing.py
Functions¶
all ¶
all(
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register a route that responds to all HTTP methods.
This registers the handler for all standard HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Example
Source code in src/artanis/routing.py
delete ¶
delete(
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register a DELETE route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Source code in src/artanis/routing.py
find_route ¶
Find a route handler and extract path parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
HTTP method |
required |
path
|
str
|
Request path |
required |
Returns:
Type | Description |
---|---|
tuple[Route | None, dict[str, str], Router | None]
|
Tuple of (route, path_parameters, source_router) or (None, {}, None) if not found |
Source code in src/artanis/routing.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
get ¶
get(
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register a GET route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Source code in src/artanis/routing.py
get_all_routes ¶
Get all routes from this router and subrouters.
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
List of all route dictionaries |
Source code in src/artanis/routing.py
get_allowed_methods ¶
Get allowed HTTP methods for a given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Request path |
required |
Returns:
Type | Description |
---|---|
list[str]
|
List of allowed HTTP methods |
Source code in src/artanis/routing.py
mount ¶
Mount a subrouter at the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path prefix where the subrouter should be mounted |
required |
router
|
Router
|
Router instance to mount |
required |
Source code in src/artanis/routing.py
options ¶
options(
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register an OPTIONS route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Source code in src/artanis/routing.py
patch ¶
patch(
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register a PATCH route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Source code in src/artanis/routing.py
post ¶
post(
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register a POST route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Source code in src/artanis/routing.py
put ¶
put(
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register a PUT route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Source code in src/artanis/routing.py
register_route ¶
register_route(
method: str,
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
) -> None
Register a route with the router.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
HTTP method |
required |
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Source code in src/artanis/routing.py
Route¶
Route ¶
Route(
method: str,
path: str,
handler: Callable[..., Any],
middleware: list[Callable[..., Any]] | None = None,
)
Represents a single route with its handler and metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
HTTP method (GET, POST, PUT, DELETE, etc.) |
required |
path
|
str
|
URL path pattern with optional parameters |
required |
handler
|
Callable[..., Any]
|
Route handler function or coroutine |
required |
middleware
|
list[Callable[..., Any]] | None
|
Optional route-specific middleware |
None
|
Attributes:
Name | Type | Description |
---|---|---|
method |
HTTP method |
|
path |
URL path pattern |
|
handler |
Route handler function |
|
pattern |
Compiled regex pattern for path matching |
|
middleware |
Route-specific middleware |
Source code in src/artanis/routing.py
Functions¶
match ¶
Check if this route matches the given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path to match against |
required |
Returns:
Type | Description |
---|---|
dict[str, str] | None
|
Dictionary of extracted path parameters if match, None otherwise |
Source code in src/artanis/routing.py
to_dict ¶
Convert route to dictionary for compatibility.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary representation of the route |