-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
be905e0
commit 2c25af8
Showing
13 changed files
with
316 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from sanic import Sanic | ||
from sanic.blueprints import Blueprint | ||
from sanic.response import json | ||
|
||
|
||
app = Sanic(name="blue-print-group-version-example") | ||
|
||
bp1 = Blueprint(name="ultron", url_prefix="/ultron") | ||
bp2 = Blueprint(name="vision", url_prefix="/vision", strict_slashes=None) | ||
|
||
bpg = Blueprint.group([bp1, bp2], url_prefix="/sentient/robot", version=1, strict_slashes=True) | ||
|
||
|
||
@bp1.get("/name") | ||
async def bp1_name(request): | ||
"""This will expose an Endpoint GET /v1/sentient/robot/ultron/name""" | ||
return json({"name": "Ultron"}) | ||
|
||
|
||
@bp2.get("/name") | ||
async def bp2_name(request): | ||
"""This will expose an Endpoint GET /v1/sentient/robot/vision/name""" | ||
return json({"name": "vision"}) | ||
|
||
|
||
@bp2.get("/name", version=2) | ||
async def bp2_revised_name(request): | ||
"""This will expose an Endpoint GET /v2/sentient/robot/vision/name""" | ||
return json({"name": "new vision"}) | ||
|
||
|
||
app.blueprint(bpg) | ||
|
||
if __name__ == '__main__': | ||
app.run(host="0.0.0.0", port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,52 @@ | ||
from collections import namedtuple | ||
|
||
|
||
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", | ||
], | ||
from pathlib import PurePath | ||
from typing import NamedTuple, List, Union, Iterable, Optional | ||
|
||
from sanic.models.handler_types import ( | ||
ListenerType, | ||
MiddlewareType, | ||
ErrorMiddlewareType, | ||
) | ||
|
||
|
||
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]] |
Oops, something went wrong.