Table of Contents
Routersanic.routersanic.router.Router#
The router implementation responsible for routing a Request
object to the appropriate handler.
class Router(delimiter: <class 'str'> = /, exception: typing.Type[sanic_routing.exceptions.NotFound] = <class 'sanic_routing.exceptions.NotFound'>, method_handler_exception: typing.Type[sanic_routing.exceptions.NoMethod] = <class 'sanic_routing.exceptions.NoMethod'>, route_class: typing.Type[sanic_routing.route.Route] = <class 'sanic_routing.route.Route'>, group_class: typing.Type[sanic_routing.group.RouteGroup] = <class 'sanic_routing.group.RouteGroup'>, stacking: <class 'bool'> = False, cascade_not_found: <class 'bool'> = False): -> None
add#
Add a handler to the router
def add(self, uri: str, methods: Iterable[str], handler: RouteHandler, host: Optional[Union[str, Iterable[str]]] = None, strict_slashes: bool = False, stream: bool = False, ignore_body: bool = False, version: Optional[Union[str, float, int]] = None, name: Optional[str] = None, unquote: bool = False, static: bool = False, version_prefix: str = "/v", overwrite: bool = False, error_format: Optional[str] = None): -> Union[Route, List[Route]]
Parameters
- uri
str The path of the route.
- methods
Iterable[str] The types of HTTP methods that should be attached, example: ["GET", "POST", "OPTIONS"].
- handler
RouteHandler The sync or async function to be executed.
- host
Optional[str] Host that the route should be on. Defaults to None.
- strict_slashes
bool Whether to apply strict slashes. Defaults to False.
- stream
bool Whether to stream the response. Defaults to False.
- ignore_body
bool Whether the incoming request body should be read. Defaults to False.
- version
Union[str, float, int] A version modifier for the uri. Defaults to None.
- name
Optional[str] An identifying name of the route. Defaults to None.
Return
- Route
The route object.
finalize#
Finalize the router.
def finalize(self, args, kwargs): -> None
Raises
- SanicException
if a route contains a parameter name that starts with "__" and is not in ALLOWED_LABELS
find_route#
@property
def find_route(self)
groups#
@property
def groups(self)
matchers#
@property
def matchers(self)
register_pattern#
Add a custom parameter type to the router. The cast should raise a
def register_pattern(self, label: <class 'str'>, cast: typing.Callable[[str], typing.Any], pattern: typing.Union[typing.Pattern, str], param_info_class: typing.Type[sanic_routing.patterns.ParamInfo] = <class 'sanic_routing.patterns.ParamInfo'>)
ValueError if it is an incorrect type. The order of registration is important if it is possible that a single value could pass multiple pattern types. Therefore, patterns are tried in the REVERSE order of registration. All custom patterns will be evaluated before any built-in patterns.
Parameters
- label
str The parts that is used to signify the type: example
- cast
t.Callable[[str], t.Any] The callable that casts the value to the desired type, or fails trying
- pattern
Union[t.Pattern, str] A regular expression that could also match the path segment
reset#
def reset(self)
resolve#
def resolve(self, path: <class 'str'>, method: typing.Optional[str] = None, orig: typing.Optional[str] = None, extra: typing.Optional[typing.Dict[str, str]] = None): -> typing.Tuple[sanic_routing.route.Route, typing.Callable[..., typing.Any], typing.Dict[str, typing.Any]]
routes#
@property
def routes(self)
routes_all#
Return all routes in the router.
@property
def routes_all(self): -> Dict[Tuple[str, ...], Route]
Return
- Dict[Tuple[str, ...], Route]
a dictionary of routes
routes_dynamic#
Return all dynamic routes in the router.
@property
def routes_dynamic(self): -> Dict[Tuple[str, ...], RouteGroup]
Dynamic routes are routes that contain path parameters.
Return
- Dict[Tuple[str, ...], Route]
a dictionary of routes
routes_regex#
Return all regex routes in the router.
@property
def routes_regex(self): -> Dict[Tuple[str, ...], RouteGroup]
Regex routes are routes that contain path parameters with regex expressions, or otherwise need regex to resolve.
Return
- Dict[Tuple[str, ...], Route]
a dictionary of routes
routes_static#
Return all static routes in the router.
@property
def routes_static(self): -> Dict[Tuple[str, ...], RouteGroup]
In this context "static" routes do not refer to the app.static()
method. Instead, they refer to routes that do not contain
any path parameters.
Return
- Dict[Tuple[str, ...], Route]
a dictionary of routes