GIT-2045: enable versioning and strict slash on BlueprintGroup (#2047)
* GIT-2045: enable versioning and strict slash on BlueprintGroup * GIT-2045: convert named tuple into typed format + unit tests * GIT-2045: add example code for versioned bpg * GIT-2045: None value for strict slashes check * GIT-2045: refactor handler types and add benchmark for urlparse * GIT-2045: reduce urlparse benchmark iterations * GIT-2045: add unit test and url merge behavior * GIT-2045: cleanup example code and remove print * GIT-2045: add test for slash duplication avoidence * GIT-2045: fix issue with tailing / getting appended * GIT-2045: use Optional instead of Union for Typing * GIT-2045: use string for version arg * GIT-2045: combine optional with union
This commit is contained in:
@@ -1,39 +1,52 @@
|
||||
from collections import namedtuple
|
||||
from pathlib import PurePath
|
||||
from typing import NamedTuple, List, Union, Iterable, Optional
|
||||
|
||||
from sanic.models.handler_types import (
|
||||
ListenerType,
|
||||
MiddlewareType,
|
||||
ErrorMiddlewareType,
|
||||
)
|
||||
|
||||
|
||||
FutureRoute = namedtuple(
|
||||
"FutureRoute",
|
||||
[
|
||||
"handler",
|
||||
"uri",
|
||||
"methods",
|
||||
"host",
|
||||
"strict_slashes",
|
||||
"stream",
|
||||
"version",
|
||||
"name",
|
||||
"ignore_body",
|
||||
"websocket",
|
||||
"subprotocols",
|
||||
"unquote",
|
||||
"static",
|
||||
],
|
||||
)
|
||||
FutureListener = namedtuple("FutureListener", ["listener", "event"])
|
||||
FutureMiddleware = namedtuple("FutureMiddleware", ["middleware", "attach_to"])
|
||||
FutureException = namedtuple("FutureException", ["handler", "exceptions"])
|
||||
FutureStatic = namedtuple(
|
||||
"FutureStatic",
|
||||
[
|
||||
"uri",
|
||||
"file_or_directory",
|
||||
"pattern",
|
||||
"use_modified_since",
|
||||
"use_content_range",
|
||||
"stream_large_files",
|
||||
"name",
|
||||
"host",
|
||||
"strict_slashes",
|
||||
"content_type",
|
||||
],
|
||||
)
|
||||
class FutureRoute(NamedTuple):
|
||||
handler: str
|
||||
uri: str
|
||||
methods: Optional[Iterable[str]]
|
||||
host: str
|
||||
strict_slashes: bool
|
||||
stream: bool
|
||||
version: Optional[int]
|
||||
name: str
|
||||
ignore_body: bool
|
||||
websocket: bool
|
||||
subprotocols: Optional[List[str]]
|
||||
unquote: bool
|
||||
static: bool
|
||||
|
||||
|
||||
class FutureListener(NamedTuple):
|
||||
listener: ListenerType
|
||||
event: str
|
||||
|
||||
|
||||
class FutureMiddleware(NamedTuple):
|
||||
middleware: MiddlewareType
|
||||
attach_to: str
|
||||
|
||||
|
||||
class FutureException(NamedTuple):
|
||||
handler: ErrorMiddlewareType
|
||||
exceptions: List[BaseException]
|
||||
|
||||
|
||||
class FutureStatic(NamedTuple):
|
||||
uri: str
|
||||
file_or_directory: Union[str, bytes, PurePath]
|
||||
pattern: str
|
||||
use_modified_since: bool
|
||||
use_content_range: bool
|
||||
stream_large_files: bool
|
||||
name: str
|
||||
host: Optional[str]
|
||||
strict_slashes: Optional[bool]
|
||||
content_type: Optional[bool]
|
||||
|
||||
25
sanic/models/handler_types.py
Normal file
25
sanic/models/handler_types.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from asyncio.events import AbstractEventLoop
|
||||
from typing import Any, Callable, Coroutine, Optional, TypeVar, Union
|
||||
|
||||
from sanic.request import Request
|
||||
from sanic.response import BaseHTTPResponse, HTTPResponse
|
||||
|
||||
|
||||
Sanic = TypeVar("Sanic")
|
||||
|
||||
|
||||
MiddlewareResponse = Union[
|
||||
Optional[HTTPResponse], Coroutine[Any, Any, Optional[HTTPResponse]]
|
||||
]
|
||||
RequestMiddlewareType = Callable[[Request], MiddlewareResponse]
|
||||
ResponseMiddlewareType = Callable[
|
||||
[Request, BaseHTTPResponse], MiddlewareResponse
|
||||
]
|
||||
ErrorMiddlewareType = Callable[
|
||||
[Request, BaseException], Optional[Coroutine[Any, Any, None]]
|
||||
]
|
||||
MiddlewareType = Union[RequestMiddlewareType, ResponseMiddlewareType]
|
||||
ListenerType = Callable[
|
||||
[Sanic, AbstractEventLoop], Optional[Coroutine[Any, Any, None]]
|
||||
]
|
||||
RouteHandler = Callable[..., Coroutine[Any, Any, HTTPResponse]]
|
||||
Reference in New Issue
Block a user