Routing¶
sanic_routing models¶
- class sanic_routing.route.Route(router, raw_path, name, handler, methods, requirements=None, strict=False, unquote=False, static=False, regex=False, overloaded=False)¶
- Parameters
raw_path (str) –
name (str) –
handler (Callable[[...], Any]) –
methods (FrozenSet[str]) –
requirements (sanic_routing.route.Requirements) –
strict (bool) –
unquote (bool) –
static (bool) –
regex (bool) –
overloaded (bool) –
- parse_parameter_string(parameter_string)¶
Parse a parameter string into its constituent name, type, and pattern
For example:
parse_parameter_string('<param_one:[A-z]>')` -> ('param_one', '[A-z]', <class 'str'>, '[A-z]')
- Parameters
parameter_string (str) – String to parse
- Returns
tuple containing (parameter_name, parameter_type, parameter_pattern)
- ctx¶
A container for route meta-data
- handler¶
The route handler
- methods¶
The HTTP methods that the route can handle
- name¶
The route name, either generated or as defined in the route definition
- path¶
The _reconstructed_ path after the Route has been normalized. Does not contain preceding
/
(see alsouri
)
- property raw_path¶
The raw path from the route definition
- regex¶
Whether the route requires regular expression evaluation
- requirements¶
A representation of the non-path route requirements
- property segments: Tuple[str, ...]¶
Same as
parts
except generalized so that any dynamic parts do not include param keys since they have no impact on routing.
- static¶
When
True
, the route does not have any dynamic path parameters
- strict¶
Whether the route should be matched with strict evaluation
- unquote¶
Whether the route should be unquoted after matching if (for example) it is suspected to contain non-URL friendly characters
- class sanic_routing.group.RouteGroup(*routes)¶
- Return type
None
- merge(group, overwrite=False, append=False)¶
The purpose of merge is to group routes with the same path, but declarared individually. In other words to group these:
@app.get("/path/to") def handler1(...): ... @app.post("/path/to") def handler2(...): ...
The other main purpose is to look for conflicts and raise
RouteExists
A duplicate route is when: 1. They have the same path and any overlapping methods; AND 2. If they have requirements, they are the same
- Parameters
group (RouteGroup) – Incoming route group
overwrite (bool, optional) – whether to allow an otherwise duplicate route group to overwrite the existing, if
True
will not raise exception on duplicates, defaults to Falseappend (bool, optional) – whether to allow an otherwise duplicate route group to append its routes to the existing route group, defaults to False
- Raises
RouteExists – Raised when there is a duplicate
- Return type
None
- path: str¶
The _reconstructed_ path after the Route has been normalized. Does not contain preceding
/
(see alsouri
)
- regex: bool¶
Whether the route requires regular expression evaluation
- segments: Tuple[str, ...]¶
Same as
parts
except generalized so that any dynamic parts do not include param keys since they have no impact on routing.
- strict: bool¶
Whether the route should be matched with strict evaluation
- unquote: bool¶
Whether the route should be unquoted after matching if (for example) it is suspected to contain non-URL friendly characters
sanic.router¶
- class sanic.router.Router(delimiter='/', exception=<class 'sanic_routing.exceptions.NotFound'>, method_handler_exception=<class 'sanic_routing.exceptions.NoMethod'>, route_class=<class 'sanic_routing.route.Route'>, group_class=<class 'sanic_routing.group.RouteGroup'>, stacking=False, cascade_not_found=False)¶
Bases:
sanic_routing.router.BaseRouter
The router implementation responsible for routing a
Request
object to the appropriate handler.- Parameters
delimiter (str) –
exception (Type[sanic_routing.exceptions.NotFound]) –
method_handler_exception (Type[sanic_routing.exceptions.NoMethod]) –
route_class (Type[sanic_routing.route.Route]) –
group_class (Type[sanic_routing.group.RouteGroup]) –
stacking (bool) –
cascade_not_found (bool) –
- Return type
None
- add(uri, methods, handler, host=None, strict_slashes=False, stream=False, ignore_body=False, version=None, name=None, unquote=False, static=False, version_prefix='/v')¶
Add a handler to the router
- 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], optional) – host that the route should be on, defaults to None
strict_slashes (bool, optional) – whether to apply strict slashes, defaults to False
stream (bool, optional) – whether to stream the response, defaults to False
ignore_body (bool, optional) – whether the incoming request body should be read, defaults to False
version (Union[str, float, int], optional) – a version modifier for the uri, defaults to None
name (Optional[str], optional) – an identifying name of the route, defaults to None
unquote (bool) –
static (bool) –
version_prefix (str) –
- Returns
the route object
- Return type
- finalize(*args, **kwargs)¶
After all routes are added, we can put everything into a final state and build the routing dource
- Parameters
do_compile (bool, optional) – Whether to compile the source, mainly a debugging tool, defaults to True
do_optimize (bool, optional) – Experimental feature that uses AST module to make some optimizations, defaults to False
- Raises
FinalizationError – Cannot finalize if there are no routes, or the router has already been finalized (can call reset() to undo it)
- find_route_by_view_name(view_name, name=None)¶
Find a route in the router based on the specified view name.
- Parameters
view_name – string of view name to search by
kwargs – additional params, usually for static files
- Returns
tuple containing (uri, Route)
- get(path, method, host)¶
Retrieve a Route object containg the details about how to handle a response for a given request