From 75f2180cb176c7095b67f1a78f49866eb21577ed Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 16 Oct 2017 22:40:21 -0700 Subject: [PATCH 1/2] add handler name to request as endpoint --- docs/sanic/request_data.md | 16 ++++++++++++++++ sanic/app.py | 2 ++ sanic/request.py | 4 +++- tests/test_requests.py | 11 +++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/sanic/request_data.md b/docs/sanic/request_data.md index e778faf6..5423aa13 100644 --- a/docs/sanic/request_data.md +++ b/docs/sanic/request_data.md @@ -120,3 +120,19 @@ args.get('titles') # => 'Post 1' args.getlist('titles') # => ['Post 1', 'Post 2'] ``` + +## Accessing the handler name with the request.endpoint attribute + +The `request.endpoint` attribute holds the handler's name. For instance, the below +route will return "hello". + +```python +from sanic.response import text +from sanic import Sanic + +app = Sanic() + +@app.get("/") +def hello(request): + return text(request.endpoint) +``` diff --git a/sanic/app.py b/sanic/app.py index 8f70b6e7..8f621599 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -247,6 +247,7 @@ class Sanic: def response(handler): async def websocket_handler(request, *args, **kwargs): request.app = self + request.endpoint = handler.__name__ try: protocol = request.transport.get_protocol() except AttributeError: @@ -540,6 +541,7 @@ class Sanic: # Fetch handler from router handler, args, kwargs, uri = self.router.get(request) + request.endpoint = handler.__name__ request.uri_template = uri if handler is None: raise ServerError( diff --git a/sanic/request.py b/sanic/request.py index 26f19baf..2893a5b7 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -46,7 +46,8 @@ class Request(dict): __slots__ = ( 'app', 'headers', 'version', 'method', '_cookies', 'transport', 'body', 'parsed_json', 'parsed_args', 'parsed_form', 'parsed_files', - '_ip', '_parsed_url', 'uri_template', 'stream', '_remote_addr' + '_ip', '_parsed_url', 'uri_template', 'stream', '_remote_addr', + 'endpoint', ) def __init__(self, url_bytes, headers, version, method, transport): @@ -68,6 +69,7 @@ class Request(dict): self.uri_template = None self._cookies = None self.stream = None + self.endpoint = None def __repr__(self): if self.method is None or not self.path: diff --git a/tests/test_requests.py b/tests/test_requests.py index f0696c7f..1f98673d 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -369,3 +369,14 @@ def test_url_attributes_with_ssl(path, query, expected_url): assert parsed.path == request.path assert parsed.query == request.query_string assert parsed.netloc == request.host + +def test_endpoint_name(): + app = Sanic('test_text') + + @app.route('/') + def my_unique_handler(request): + return text('Hello') + + request, response = app.test_client.get('/') + + assert request.endpoint == 'my_unique_handler' From 9150767574fc2f213b1e8aca6d61d9e2f70e7e40 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 16 Oct 2017 23:11:56 -0700 Subject: [PATCH 2/2] add blueprint name to request.endpoint --- docs/sanic/request_data.md | 21 +++++++++++++++++++++ sanic/app.py | 14 ++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/sanic/request_data.md b/docs/sanic/request_data.md index 5423aa13..82a89823 100644 --- a/docs/sanic/request_data.md +++ b/docs/sanic/request_data.md @@ -136,3 +136,24 @@ app = Sanic() def hello(request): return text(request.endpoint) ``` + +Or, with a blueprint it will be include both, separated by a period. For example, + the below route would return foo.bar: + +```python +from sanic import Sanic +from sanic import Blueprint +from sanic.response import text + + +app = Sanic(__name__) +blueprint = Blueprint('foo') + +@blueprint.get('/') +async def bar(request): + return text(request.endpoint) + +app.blueprint(blueprint) + +app.run(host="0.0.0.0", port=8000, debug=True) +``` diff --git a/sanic/app.py b/sanic/app.py index 8f621599..f1790a3e 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -247,7 +247,12 @@ class Sanic: def response(handler): async def websocket_handler(request, *args, **kwargs): request.app = self - request.endpoint = handler.__name__ + if not getattr(handler, '__blueprintname__', False): + request.endpoint = handler.__name__ + else: + request.endpoint = getattr(handler, + '__blueprintname__', + '') + handler.__name__ try: protocol = request.transport.get_protocol() except AttributeError: @@ -541,7 +546,12 @@ class Sanic: # Fetch handler from router handler, args, kwargs, uri = self.router.get(request) - request.endpoint = handler.__name__ + if not getattr(handler, '__blueprintname__', False): + request.endpoint = handler.__name__ + else: + request.endpoint = getattr(handler, + '__blueprintname__', + '') + handler.__name__ request.uri_template = uri if handler is None: raise ServerError(