Add dotted endpoint notation and additional tests

This commit is contained in:
Adam Hopkins
2018-12-31 13:40:07 +02:00
5 changed files with 116 additions and 14 deletions

View File

@@ -454,6 +454,13 @@ class Sanic:
def response(handler):
async def websocket_handler(request, *args, **kwargs):
request.app = self
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:
@@ -888,6 +895,16 @@ class Sanic:
"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
response = handler(request, *args, **kwargs)
@@ -1276,3 +1293,7 @@ class Sanic:
logger.info("Goin' Fast @ {}://{}:{}".format(proto, host, port))
return server_settings
def _build_endpoint_name(self, *parts):
parts = [self.name, *parts]
return ".".join(parts)

View File

@@ -69,26 +69,27 @@ class Request(dict):
"""Properties of an HTTP request such as URL, headers, etc."""
__slots__ = (
"app",
"headers",
"version",
"method",
"__weakref__",
"_cookies",
"transport",
"body",
"parsed_json",
"parsed_args",
"parsed_form",
"parsed_files",
"_ip",
"_parsed_url",
"uri_template",
"stream",
"_port",
"_remote_addr",
"_socket",
"_port",
"__weakref__",
"app",
"body",
"endpoint",
"headers",
"method",
"parsed_args",
"parsed_files",
"parsed_form",
"parsed_json",
"raw_url",
"stream",
"transport",
"uri_template",
"version",
)
def __init__(self, url_bytes, headers, version, method, transport):
@@ -111,6 +112,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: