Name endpoints at startup (#1972)
* Name endpoints at startup * Beautify * Fix reformatting
This commit is contained in:
parent
bde0428d0c
commit
614be40438
18
sanic/app.py
18
sanic/app.py
|
@ -68,7 +68,7 @@ class Sanic:
|
||||||
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.asgi = False
|
self.asgi = False
|
||||||
self.router = router or Router()
|
self.router = router or Router(self)
|
||||||
self.request_class = request_class
|
self.request_class = request_class
|
||||||
self.error_handler = error_handler or ErrorHandler()
|
self.error_handler = error_handler or ErrorHandler()
|
||||||
self.config = Config(load_env=load_env)
|
self.config = Config(load_env=load_env)
|
||||||
|
@ -900,7 +900,9 @@ class Sanic:
|
||||||
name = None
|
name = None
|
||||||
try:
|
try:
|
||||||
# Fetch handler from router
|
# Fetch handler from router
|
||||||
handler, args, kwargs, uri, name = self.router.get(request)
|
handler, args, kwargs, uri, name, endpoint = self.router.get(
|
||||||
|
request
|
||||||
|
)
|
||||||
|
|
||||||
# -------------------------------------------- #
|
# -------------------------------------------- #
|
||||||
# Request Middleware
|
# Request Middleware
|
||||||
|
@ -922,16 +924,8 @@ class Sanic:
|
||||||
"handler from the router"
|
"handler from the router"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
if not getattr(handler, "__blueprintname__", False):
|
request.endpoint = endpoint
|
||||||
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)
|
||||||
|
|
|
@ -11,7 +11,16 @@ from sanic.views import CompositionView
|
||||||
|
|
||||||
|
|
||||||
Route = namedtuple(
|
Route = namedtuple(
|
||||||
"Route", ["handler", "methods", "pattern", "parameters", "name", "uri"]
|
"Route",
|
||||||
|
[
|
||||||
|
"handler",
|
||||||
|
"methods",
|
||||||
|
"pattern",
|
||||||
|
"parameters",
|
||||||
|
"name",
|
||||||
|
"uri",
|
||||||
|
"endpoint",
|
||||||
|
],
|
||||||
)
|
)
|
||||||
Parameter = namedtuple("Parameter", ["name", "cast"])
|
Parameter = namedtuple("Parameter", ["name", "cast"])
|
||||||
|
|
||||||
|
@ -79,7 +88,8 @@ class Router:
|
||||||
routes_always_check = None
|
routes_always_check = None
|
||||||
parameter_pattern = re.compile(r"<(.+?)>")
|
parameter_pattern = re.compile(r"<(.+?)>")
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, app):
|
||||||
|
self.app = app
|
||||||
self.routes_all = {}
|
self.routes_all = {}
|
||||||
self.routes_names = {}
|
self.routes_names = {}
|
||||||
self.routes_static_files = {}
|
self.routes_static_files = {}
|
||||||
|
@ -299,11 +309,15 @@ class Router:
|
||||||
|
|
||||||
handler_name = f"{bp_name}.{name or handler.__name__}"
|
handler_name = f"{bp_name}.{name or handler.__name__}"
|
||||||
else:
|
else:
|
||||||
handler_name = name or getattr(handler, "__name__", None)
|
handler_name = name or getattr(
|
||||||
|
handler, "__name__", handler.__class__.__name__
|
||||||
|
)
|
||||||
|
|
||||||
if route:
|
if route:
|
||||||
route = merge_route(route, methods, handler)
|
route = merge_route(route, methods, handler)
|
||||||
else:
|
else:
|
||||||
|
endpoint = self.app._build_endpoint_name(handler_name)
|
||||||
|
|
||||||
route = Route(
|
route = Route(
|
||||||
handler=handler,
|
handler=handler,
|
||||||
methods=methods,
|
methods=methods,
|
||||||
|
@ -311,6 +325,7 @@ class Router:
|
||||||
parameters=parameters,
|
parameters=parameters,
|
||||||
name=handler_name,
|
name=handler_name,
|
||||||
uri=uri,
|
uri=uri,
|
||||||
|
endpoint=endpoint,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.routes_all[uri] = route
|
self.routes_all[uri] = route
|
||||||
|
@ -449,7 +464,8 @@ class Router:
|
||||||
route_handler = route.handler
|
route_handler = route.handler
|
||||||
if hasattr(route_handler, "handlers"):
|
if hasattr(route_handler, "handlers"):
|
||||||
route_handler = route_handler.handlers[method]
|
route_handler = route_handler.handlers[method]
|
||||||
return route_handler, [], kwargs, route.uri, route.name
|
|
||||||
|
return route_handler, [], kwargs, route.uri, route.name, route.endpoint
|
||||||
|
|
||||||
def is_stream_handler(self, request):
|
def is_stream_handler(self, request):
|
||||||
"""Handler for request is stream or not.
|
"""Handler for request is stream or not.
|
||||||
|
|
|
@ -90,6 +90,7 @@ class CompositionView:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.handlers = {}
|
self.handlers = {}
|
||||||
|
self.name = self.__class__.__name__
|
||||||
|
|
||||||
def add(self, methods, handler, stream=False):
|
def add(self, methods, handler, stream=False):
|
||||||
if stream:
|
if stream:
|
||||||
|
|
|
@ -95,10 +95,10 @@ class RouteStringGenerator:
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
def sanic_router():
|
def sanic_router(app):
|
||||||
# noinspection PyProtectedMember
|
# noinspection PyProtectedMember
|
||||||
def _setup(route_details: tuple) -> (Router, tuple):
|
def _setup(route_details: tuple) -> (Router, tuple):
|
||||||
router = Router()
|
router = Router(app)
|
||||||
added_router = []
|
added_router = []
|
||||||
for method, route in route_details:
|
for method, route in route_details:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -117,7 +117,7 @@ def test_app_route_raise_value_error(app):
|
||||||
|
|
||||||
def test_app_handle_request_handler_is_none(app, monkeypatch):
|
def test_app_handle_request_handler_is_none(app, monkeypatch):
|
||||||
def mockreturn(*args, **kwargs):
|
def mockreturn(*args, **kwargs):
|
||||||
return None, [], {}, "", ""
|
return None, [], {}, "", "", None
|
||||||
|
|
||||||
# Not sure how to make app.router.get() return None, so use mock here.
|
# Not sure how to make app.router.get() return None, so use mock here.
|
||||||
monkeypatch.setattr(app.router, "get", mockreturn)
|
monkeypatch.setattr(app.router, "get", mockreturn)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user