Merge pull request #1445 from huge-success/r0fls-977
add handler name to request as endpoint
This commit is contained in:
commit
2af229eb1a
|
@ -126,3 +126,40 @@ args.get('titles') # => 'Post 1'
|
||||||
|
|
||||||
args.getlist('titles') # => ['Post 1', 'Post 2']
|
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)
|
||||||
|
```
|
||||||
|
|
||||||
|
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)
|
||||||
|
```
|
||||||
|
|
21
sanic/app.py
21
sanic/app.py
|
@ -456,6 +456,13 @@ class Sanic:
|
||||||
def response(handler):
|
def response(handler):
|
||||||
async def websocket_handler(request, *args, **kwargs):
|
async def websocket_handler(request, *args, **kwargs):
|
||||||
request.app = self
|
request.app = self
|
||||||
|
if not getattr(handler, "__blueprintname__", False):
|
||||||
|
request.endpoint = handler.__name__
|
||||||
|
else:
|
||||||
|
request.endpoint = (
|
||||||
|
getattr(handler, "__blueprintname__", "")
|
||||||
|
+ handler.__name__
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
protocol = request.transport.get_protocol()
|
protocol = request.transport.get_protocol()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -890,6 +897,16 @@ class Sanic:
|
||||||
"handler from the router"
|
"handler from the router"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
if not getattr(handler, "__blueprintname__", False):
|
||||||
|
request.endpoint = self._build_endpoint_name(
|
||||||
|
handler.__name__
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
request.endpoint = self._build_endpoint_name(
|
||||||
|
getattr(handler, "__blueprintname__", ""),
|
||||||
|
handler.__name__,
|
||||||
|
)
|
||||||
|
|
||||||
# Run response handler
|
# Run response handler
|
||||||
response = handler(request, *args, **kwargs)
|
response = handler(request, *args, **kwargs)
|
||||||
|
@ -1318,3 +1335,7 @@ class Sanic:
|
||||||
logger.info("Goin' Fast @ {}://{}:{}".format(proto, host, port))
|
logger.info("Goin' Fast @ {}://{}:{}".format(proto, host, port))
|
||||||
|
|
||||||
return server_settings
|
return server_settings
|
||||||
|
|
||||||
|
def _build_endpoint_name(self, *parts):
|
||||||
|
parts = [self.name, *parts]
|
||||||
|
return ".".join(parts)
|
||||||
|
|
|
@ -69,26 +69,27 @@ class Request(dict):
|
||||||
"""Properties of an HTTP request such as URL, headers, etc."""
|
"""Properties of an HTTP request such as URL, headers, etc."""
|
||||||
|
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
"app",
|
"__weakref__",
|
||||||
"headers",
|
|
||||||
"version",
|
|
||||||
"method",
|
|
||||||
"_cookies",
|
"_cookies",
|
||||||
"transport",
|
|
||||||
"body",
|
|
||||||
"parsed_json",
|
|
||||||
"parsed_args",
|
|
||||||
"parsed_form",
|
|
||||||
"parsed_files",
|
|
||||||
"_ip",
|
"_ip",
|
||||||
"_parsed_url",
|
"_parsed_url",
|
||||||
"uri_template",
|
"_port",
|
||||||
"stream",
|
|
||||||
"_remote_addr",
|
"_remote_addr",
|
||||||
"_socket",
|
"_socket",
|
||||||
"_port",
|
"app",
|
||||||
"__weakref__",
|
"body",
|
||||||
|
"endpoint",
|
||||||
|
"headers",
|
||||||
|
"method",
|
||||||
|
"parsed_args",
|
||||||
|
"parsed_files",
|
||||||
|
"parsed_form",
|
||||||
|
"parsed_json",
|
||||||
"raw_url",
|
"raw_url",
|
||||||
|
"stream",
|
||||||
|
"transport",
|
||||||
|
"uri_template",
|
||||||
|
"version",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, url_bytes, headers, version, method, transport):
|
def __init__(self, url_bytes, headers, version, method, transport):
|
||||||
|
@ -111,6 +112,7 @@ class Request(dict):
|
||||||
self.uri_template = None
|
self.uri_template = None
|
||||||
self._cookies = None
|
self._cookies = None
|
||||||
self.stream = None
|
self.stream = None
|
||||||
|
self.endpoint = None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if self.method is None or not self.path:
|
if self.method is None or not self.path:
|
||||||
|
|
|
@ -5,6 +5,7 @@ from sanic.config import BASE_LOGO
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import uvloop # noqa
|
import uvloop # noqa
|
||||||
|
|
||||||
ROW = 0
|
ROW = 0
|
||||||
except BaseException:
|
except BaseException:
|
||||||
ROW = 1
|
ROW = 1
|
||||||
|
|
|
@ -7,6 +7,8 @@ from urllib.parse import urlparse
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from sanic import Sanic
|
||||||
|
from sanic import Blueprint
|
||||||
from sanic.exceptions import ServerError
|
from sanic.exceptions import ServerError
|
||||||
from sanic.request import DEFAULT_HTTP_CONTENT_TYPE
|
from sanic.request import DEFAULT_HTTP_CONTENT_TYPE
|
||||||
from sanic.response import json, text
|
from sanic.response import json, text
|
||||||
|
@ -698,3 +700,42 @@ def test_request_form_invalid_content_type(app):
|
||||||
request, response = app.test_client.post("/", json={"test": "OK"})
|
request, response = app.test_client.post("/", json={"test": "OK"})
|
||||||
|
|
||||||
assert request.form == {}
|
assert request.form == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_endpoint_basic():
|
||||||
|
app = Sanic()
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def my_unique_handler(request):
|
||||||
|
return text("Hello")
|
||||||
|
|
||||||
|
request, response = app.test_client.get("/")
|
||||||
|
|
||||||
|
assert request.endpoint == "test_requests.my_unique_handler"
|
||||||
|
|
||||||
|
|
||||||
|
def test_endpoint_named_app():
|
||||||
|
app = Sanic("named")
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def my_unique_handler(request):
|
||||||
|
return text("Hello")
|
||||||
|
|
||||||
|
request, response = app.test_client.get("/")
|
||||||
|
|
||||||
|
assert request.endpoint == "named.my_unique_handler"
|
||||||
|
|
||||||
|
|
||||||
|
def test_endpoint_blueprint():
|
||||||
|
bp = Blueprint("my_blueprint", url_prefix="/bp")
|
||||||
|
|
||||||
|
@bp.route("/")
|
||||||
|
async def bp_root(request):
|
||||||
|
return text("Hello")
|
||||||
|
|
||||||
|
app = Sanic("named")
|
||||||
|
app.blueprint(bp)
|
||||||
|
|
||||||
|
request, response = app.test_client.get("/bp")
|
||||||
|
|
||||||
|
assert request.endpoint == "named.my_blueprint.bp_root"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user