wsgi_tools.routing module¶
With routing you can forward your request to specific WSGI-apps.
There are two main classes in this module: Router()
and Rule()
The Router()
is the WSGI-app, which uses rules to identify, to which WSGI-app it should forward.
You construct the Router()
with a list of all rules you use and with an arg for each route and rule.
The Rule()
defines if the request and the (arg of this rule in the) route match.
Example
>>> from wsgi_tools.routing import CONTENT_TYPE_RULE, METHOD_RULE, PathRule, Router
>>> path_rule = PathRule()
>>> app = Router(
... [path_rule, METHOD_RULE, CONTENT_TYPE_RULE],
... {
... (('/create',), 'POST', 'json'): create_app,
... (('/', int, '/options'), 'GET', None): options_app
... }
... )
- wsgi_tools.routing.CONTENT_TYPE_RULE = <wsgi_tools.routing.ContentTypeRule object>¶
A rule which checks whether the content_types match.
The arg you have to specifiy in each route is a
str
which represents the content-type orNone
if you do not expect content in this route (e.g. for aGET
request).If the arg is a
str
and includes a/
, the rule will match, if arg and content-type match exactly.If the arg is a
str
, but it does not includes any/
, the rule will match, if the arg is located at any place after the/
in the content type.For example:
json
matchesapplication/json
,application/foo+json
,hello/foo+json+bar
, etc.application/json
only matchesapplication/json
.
- class wsgi_tools.routing.ContentTypeRule¶
Bases:
wsgi_tools.routing.Rule
A rule which checks whether the content_types match.
The arg you have to specifiy in each route is a
str
which represents the content-type orNone
if you do not expect content in this route (e.g. for aGET
request).If the arg is a
str
and includes a/
, the rule will match, if arg and content-type match exactly.If the arg is a
str
, but it does not includes any/
, the rule will match, if the arg is located at any place after the/
in the content type.For example:
json
matchesapplication/json
,application/foo+json
,hello/foo+json+bar
, etc.application/json
only matchesapplication/json
.Note
You can use the constant
CONTENT_TYPE_RULE()
, because this rule does not have any attributes.- check(environ, value)¶
The Method, which controls, whether a request and a route are matching this rule.
- Parameters
environ (dict) – A WSGI-environ
value – the value of this rule for a route
- Returns
True if environ and value are matching, False otherwise
- Return type
bool
- get_exception()¶
If no route matches this rule, an exception has to be thrown.
- Returns
The exception which will be thrown, if no route supports thie rule.
- Return type
- wsgi_tools.routing.METHOD_RULE = <wsgi_tools.routing.MethodRule object>¶
A rule which checks whether the http methods match.
The arg you have to specifiy in each route is a
str
representing the http-method (e.g.'GET'
,'POST'
or'DELETE'
).
- class wsgi_tools.routing.MethodRule¶
Bases:
wsgi_tools.routing.Rule
A rule which checks whether the http methods match.
The arg you have to specifiy in each route is a
str
representing the http-method (e.g.'GET'
,'POST'
or'DELETE'
).Note
You can use the constant
METHOD_RULE()
, because this rule does not have any attributes.- check(environ, value)¶
The Method, which controls, whether a request and a route are matching this rule.
- Parameters
environ (dict) – A WSGI-environ
value – the value of this rule for a route
- Returns
True if environ and value are matching, False otherwise
- Return type
bool
- get_exception()¶
If no route matches this rule, an exception has to be thrown.
- Returns
The exception which will be thrown, if no route supports thie rule.
- Return type
- class wsgi_tools.routing.PathRule¶
Bases:
wsgi_tools.routing.Rule
A Rule, which controls the path of the request.
The arg you have to specifiy in each route is a tuple or a list. This tuple or list has to begin with a string.
The simplest arg is a tuple, which is only one string.
('/',)
is the root document.('/hello',)
ishttp://{host}/hello
You get an generic path, if you include callables, which have a
str
as the only arg and raise a ValueError if the input is wrong.Examples for these callables are
int
andfloat
, but you can create your own functions as well.It is not allowed that two of these callables are directly follow upon each other.
('/', str, '/foo')
for/bar/foo
andargs
is['bar']
or/hello/foo
andargs
is['hello']
.('/id/', int, '/user/', str, '/create')
for/id/321/user/root/create
andargs
is[321, 'root']
.- args¶
The list of the generic parts of the path.
- Type
list
- check(environ, value)¶
The Method, which controls, whether a request and a route are matching this rule.
- Parameters
environ (dict) – A WSGI-environ
value – the value of this rule for a route
- Returns
True if environ and value are matching, False otherwise
- Return type
bool
- get_exception()¶
If no route matches this rule, an exception has to be thrown.
- Returns
The exception which will be thrown, if no route supports thie rule.
- Return type
- class wsgi_tools.routing.Router(rules, routes)¶
Bases:
object
The WSGI-app, which forwards requests on their environ.
- Parameters
rules (list(Rule)) – A list of the rules you want to use.
routes (dict) – A dict representing the routes. A route is a dict-entry with a tuple or list with the args for all rules for this route as the key and the WSGI-app the router should forward to as the value.
- class wsgi_tools.routing.Rule¶
Bases:
object
A Rule is an object, which defines if requests can be forwarded to a route.
Some Rules also store data of the current request which are accessible from the code which is executed at this request.
- abstract check(environ, value)¶
The Method, which controls, whether a request and a route are matching this rule.
- Parameters
environ (dict) – A WSGI-environ
value – the value of this rule for a route
- Returns
True if environ and value are matching, False otherwise
- Return type
bool
- abstract get_exception()¶
If no route matches this rule, an exception has to be thrown.
- Returns
The exception which will be thrown, if no route supports thie rule.
- Return type