Expose scope parameter in request object (#2432)
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
@@ -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):
|
||||
...
|
||||
|
||||
|
||||
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user