Skip to content

Commit c57c091

Browse files
committed
Removed asgi args and renamed to request scope context
1 parent 4e3a3a3 commit c57c091

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

ellar/asgi_args.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import typing as t
22

3-
from ellar.types import TReceive, TScope, TSend
4-
53
if t.TYPE_CHECKING: # pragma: no cover
64
from ellar.di.providers import Provider
75

86

9-
class ASGIArgs:
10-
__slots__ = ("scope", "receive", "send", "context")
7+
class RequestScopeContext:
8+
__slots__ = ("_injector_scoped_context",)
119

12-
def __init__(self, scope: TScope, receive: TReceive, send: TSend) -> None:
13-
self.scope = scope
14-
self.receive = receive
15-
self.send = send
16-
self.context: t.Dict[t.Type, "Provider"] = {}
10+
def __init__(self) -> None:
11+
self._injector_scoped_context: t.Dict[t.Type, "Provider"] = {}
1712

18-
def get_args(self) -> t.Tuple[TScope, TReceive, TSend]:
19-
return self.scope, self.receive, self.send
13+
@property
14+
def context(self) -> t.Dict[t.Type, "Provider"]:
15+
return self._injector_scoped_context

ellar/constants.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
SHAPE_TUPLE_ELLIPSIS,
1212
)
1313

14-
from .asgi_args import ASGIArgs
14+
from .asgi_args import RequestScopeContext
1515

16-
ASGI_CONTEXT_VAR: contextvars.ContextVar[Optional[ASGIArgs]] = contextvars.ContextVar(
17-
"ASGI-CONTEXT-VAR"
18-
)
19-
ASGI_CONTEXT_VAR.set(None)
16+
SCOPED_CONTEXT_VAR: contextvars.ContextVar[
17+
Optional[RequestScopeContext]
18+
] = contextvars.ContextVar("ASGI-CONTEXT-VAR")
19+
SCOPED_CONTEXT_VAR.set(None)
2020

2121

2222
class _AnnotationToValue(type):
@@ -49,9 +49,8 @@ def __new__(mcls, name, bases, namespace):
4949
ROUTE_METHODS = [POST, PUT, PATCH, DELETE, GET, HEAD, OPTIONS, TRACE]
5050

5151
SCOPE_SERVICE_PROVIDER = "SERVICE_PROVIDER"
52-
SCOPE_EXECUTION_CONTEXT_PROVIDER = "SERVICE_EXECUTION_CONTEXT_PROVIDER"
53-
SCOPE_HOST_CONTEXT_PROVIDER = "SCOPE_HOST_CONTEXT_PROVIDER"
54-
SCOPE_RESPONSE_STARTED = "response_started"
52+
SCOPE_RESPONSE_STARTED = "__response_started__"
53+
SCOPED_RESPONSE = "__response__"
5554
SCOPE_API_VERSIONING_RESOLVER = "API_VERSIONING_RESOLVER"
5655
SCOPE_API_VERSIONING_SCHEME = "API_VERSIONING_SCHEME"
5756
ELLAR_CONFIG_MODULE = "ELLAR_CONFIG_MODULE"

ellar/core/middleware/di.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ellar.constants import SCOPE_RESPONSE_STARTED, SCOPE_SERVICE_PROVIDER
88
from ellar.core.connection.http import Request
9-
from ellar.core.context import IHostContext
9+
from ellar.core.context import IHostContextFactory
1010
from ellar.core.response import Response
1111
from ellar.di import EllarInjector
1212
from ellar.types import ASGIApp, TMessage, TReceive, TScope, TSend
@@ -48,20 +48,22 @@ async def sender(message: TMessage) -> None:
4848
await send(message)
4949
return
5050

51-
async with self.injector.create_asgi_args(
52-
scope, receive, sender
53-
) as service_provider:
54-
scope[SCOPE_SERVICE_PROVIDER] = service_provider
51+
_sender = sender if scope["type"] == "http" else send
5552

56-
host_context: IHostContext = service_provider.get(IHostContext)
53+
async with self.injector.create_asgi_args() as service_provider:
54+
scope[SCOPE_SERVICE_PROVIDER] = service_provider
5755

58-
if host_context.get_type() == "http":
59-
await super().__call__(*host_context.get_args())
56+
if scope["type"] == "http":
57+
await super().__call__(scope, receive, _sender)
6058
else:
61-
await self.app(*host_context.get_args())
59+
await self.app(scope, receive, _sender)
6260

6361
async def error_handler(self, request: Request, exc: Exception) -> Response:
64-
host_context = request.scope[SCOPE_SERVICE_PROVIDER].get(IHostContext)
62+
host_context_factory: IHostContextFactory = request.scope[
63+
SCOPE_SERVICE_PROVIDER
64+
].get(IHostContextFactory)
65+
host_context = host_context_factory.create_context(request.scope)
66+
6567
assert self._500_error_handler
6668
response = await self._500_error_handler.catch(host_context, exc)
6769
return response

ellar/di/injector/ellar_injector.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from injector import Injector
55

6-
from ellar.asgi_args import ASGIArgs
6+
from ellar.asgi_args import RequestScopeContext
77
from ellar.compatible import asynccontextmanager
8-
from ellar.constants import ASGI_CONTEXT_VAR, MODULE_REF_TYPES
8+
from ellar.constants import MODULE_REF_TYPES, SCOPED_CONTEXT_VAR
99
from ellar.logger import logger as log
10-
from ellar.types import T, TReceive, TScope, TSend
10+
from ellar.types import T
1111

1212
from ..providers import InstanceProvider, Provider
1313
from ..scopes import DIScope, ScopeDecorator
@@ -88,7 +88,7 @@ def get(
8888
interface: t.Type[T],
8989
scope: t.Union[ScopeDecorator, t.Type[DIScope]] = None,
9090
) -> T:
91-
scoped_context = ASGI_CONTEXT_VAR.get()
91+
scoped_context = SCOPED_CONTEXT_VAR.get()
9292
context = None
9393
if scoped_context:
9494
context = scoped_context.context
@@ -119,7 +119,7 @@ def get(
119119
def update_scoped_context(self, interface: t.Type[T], value: T) -> None:
120120
# Sets RequestScope contexts so that they can be available when needed
121121
#
122-
scoped_context = ASGI_CONTEXT_VAR.get()
122+
scoped_context = SCOPED_CONTEXT_VAR.get()
123123
if scoped_context is None:
124124
return
125125

@@ -129,11 +129,9 @@ def update_scoped_context(self, interface: t.Type[T], value: T) -> None:
129129
scoped_context.context.update({interface: InstanceProvider(value)})
130130

131131
@asynccontextmanager
132-
async def create_asgi_args(
133-
self, scope: TScope, receive: TReceive, send: TSend
134-
) -> t.AsyncGenerator["EllarInjector", None]:
132+
async def create_asgi_args(self) -> t.AsyncGenerator["EllarInjector", None]:
135133
try:
136-
ASGI_CONTEXT_VAR.set(ASGIArgs(scope, receive, send))
134+
SCOPED_CONTEXT_VAR.set(RequestScopeContext())
137135
yield self
138136
finally:
139-
ASGI_CONTEXT_VAR.set(None)
137+
SCOPED_CONTEXT_VAR.set(None)

0 commit comments

Comments
 (0)