This commit is contained in:
Adam Hopkins 2021-01-31 16:31:04 +02:00
parent e04f206c50
commit 7b47a4bebc
3 changed files with 54 additions and 9 deletions

View File

@ -985,6 +985,7 @@ class Sanic(BaseSanic):
three arguments: scope, receive, send. See the ASGI reference for more
details: https://asgi.readthedocs.io/en/latest/"""
self.asgi = True
self.router.finalize()
asgi_app = await ASGIApp.create(self, scope, receive, send)
await asgi_app()

View File

@ -1,4 +1,5 @@
from functools import lru_cache
from typing import Iterable, Optional, Union
from sanic_routing import BaseRouter
from sanic_routing.route import Route
@ -8,11 +9,27 @@ from sanic.request import Request
class Router(BaseRouter):
"""
The router implementation responsible for routing a :class:`Request` object
to the appropriate handler.
"""
DEFAULT_METHOD = "GET"
ALLOWED_METHODS = HTTP_METHODS
@lru_cache
def get(self, request: Request):
"""
Retrieve a `Route` object containg the details about how to handle
a response for a given request
:param request: the incoming request object
:type request: Request
:return: details needed for handling the request and returning the
correct response
:rtype: Tuple[ RouteHandler, Tuple[Any, ...], Dict[str, Any], str, str,
Optional[str], bool, ]
"""
route, handler, params = self.resolve(
path=request.path,
method=request.method,
@ -34,16 +51,43 @@ class Router(BaseRouter):
def add(
self,
uri,
methods,
uri: str,
methods: Iterable[str],
handler,
host=None,
strict_slashes=False,
stream=False,
ignore_body=False,
version=None,
name=None,
host: Optional[str] = None,
strict_slashes: bool = False,
stream: bool = False,
ignore_body: bool = False,
version: Union[str, float, int] = None,
name: Optional[str] = None,
) -> Route:
"""
Add a handler to the router
:param uri: the path of the route
:type uri: str
:param methods: the types of HTTP methods that should be attached,
example: ``["GET", "POST", "OPTIONS"]``
:type methods: Iterable[str]
:param handler: the sync or async function to be executed
:type handler: RouteHandler
:param host: host that the route should be on, defaults to None
:type host: Optional[str], optional
:param strict_slashes: whether to apply strict slashes, defaults
to False
:type strict_slashes: bool, optional
:param stream: whether to stream the response, defaults to False
:type stream: bool, optional
:param ignore_body: whether the incoming request body should be read,
defaults to False
:type ignore_body: bool, optional
:param version: a version modifier for the uri, defaults to None
:type version: Union[str, float, int], optional
:param name: an identifying name of the route, defaults to None
:type name: Optional[str], optional
:return: the route object
:rtype: Route
"""
# TODO: Implement
# - host
# - strict_slashes

View File

@ -325,7 +325,7 @@ async def test_cookie_customization(app):
@pytest.mark.asyncio
async def test_json_content_type(app):
async def test_content_type(app):
@app.get("/json")
def send_json(request):
return json({"foo": "bar"})