Expose scope parameter in request object (#2432)

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
Michael Azimov
2022-04-26 17:25:29 +03:00
committed by GitHub
parent 78b6723149
commit 5d683c6ea4
4 changed files with 47 additions and 1 deletions

View File

@@ -2,6 +2,8 @@ import sys
from typing import Any, AnyStr, TypeVar, Union
from sanic.models.asgi import ASGIScope
if sys.version_info < (3, 8):
from asyncio import BaseTransport
@@ -17,6 +19,8 @@ else:
from typing import Protocol
class TransportProtocol(Protocol):
scope: ASGIScope
def get_protocol(self):
...

View File

@@ -14,6 +14,7 @@ from typing import (
from sanic_routing.route import Route # type: ignore
from sanic.models.asgi import ASGIScope
from sanic.models.http_types import Credentials
@@ -831,6 +832,21 @@ class Request:
view_name, _external=True, _scheme=scheme, _server=netloc, **kwargs
)
@property
def scope(self) -> ASGIScope:
"""
:return: The ASGI scope of the request.
If the app isn't an ASGI app, then raises an exception.
:rtype: Optional[ASGIScope]
"""
if not self.app.asgi:
raise NotImplementedError(
"App isn't running in ASGI mode. "
"Scope is only available for ASGI apps."
)
return self.transport.scope
class File(NamedTuple):
"""