Merge pull request #671 from banteg/uri-template

Expose matched request uri template
This commit is contained in:
Eli Uriegas 2017-04-28 14:22:59 -07:00 committed by GitHub
commit 6dc6f9bbb5
4 changed files with 8 additions and 5 deletions

View File

@ -94,6 +94,7 @@ The following variables are accessible as properties on `Request` objects:
- `host`: The host associated with the request: `localhost:8080`
- `path`: The path of the request: `/posts/1/`
- `query_string`: The query string of the request: `foo=bar` or a blank string `''`
- `uri_template`: Template for matching route handler: `/posts/<id>/`
## Accessing values using `get` and `getlist`

View File

@ -458,7 +458,8 @@ class Sanic:
# -------------------------------------------- #
# Fetch handler from router
handler, args, kwargs = self.router.get(request)
handler, args, kwargs, uri = self.router.get(request)
request.uri_template = uri
if handler is None:
raise ServerError(
("'None' was returned while requesting a "

View File

@ -38,7 +38,7 @@ class Request(dict):
__slots__ = (
'app', 'headers', 'version', 'method', '_cookies', 'transport',
'body', 'parsed_json', 'parsed_args', 'parsed_form', 'parsed_files',
'_ip', '_parsed_url',
'_ip', '_parsed_url', 'uri_template'
)
def __init__(self, url_bytes, headers, version, method, transport):
@ -57,6 +57,7 @@ class Request(dict):
self.parsed_form = None
self.parsed_files = None
self.parsed_args = None
self.uri_template = None
self._cookies = None
@property

View File

@ -8,7 +8,7 @@ from sanic.views import CompositionView
Route = namedtuple(
'Route',
['handler', 'methods', 'pattern', 'parameters', 'name'])
['handler', 'methods', 'pattern', 'parameters', 'name', 'uri'])
Parameter = namedtuple('Parameter', ['name', 'cast'])
REGEX_TYPES = {
@ -225,7 +225,7 @@ class Router:
route = Route(
handler=handler, methods=methods, pattern=pattern,
parameters=parameters, name=handler_name)
parameters=parameters, name=handler_name, uri=uri)
self.routes_all[uri] = route
if properties['unhashable']:
@ -344,4 +344,4 @@ class Router:
route_handler = route.handler
if hasattr(route_handler, 'handlers'):
route_handler = route_handler.handlers[method]
return route_handler, [], kwargs
return route_handler, [], kwargs, route.uri