-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext.py
More file actions
140 lines (104 loc) · 3.76 KB
/
Copy pathcontext.py
File metadata and controls
140 lines (104 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import typing as t
from abc import ABC, ABCMeta, abstractmethod, abstractproperty
from ellar.common.constants import empty_receive
from ellar.common.types import T, TReceive, TScope, TSend
from starlette.requests import empty_send
from starlette.responses import Response
if t.TYPE_CHECKING: # pragma: no cover
from ellar.app import App
from ellar.common.models import ControllerBase, Identity
from ellar.core import HTTPConnection, Request, WebSocket
from ellar.core.routing import RouteOperationBase
from ellar.di.injector import EllarInjector
class IHTTPHostContext(ABC):
@property
@abstractmethod
def has_response(self) -> bool:
"""checks if response was requested before send"""
@abstractmethod
def get_response(self) -> "Response":
"""Gets response"""
@abstractmethod
def get_request(self) -> "Request":
"""Returns Request instance"""
@abstractmethod
def get_client(self) -> "HTTPConnection":
"""Returns HTTPConnection instance"""
class IWebSocketHostContext(ABC):
@abstractmethod
def get_client(self) -> "WebSocket":
"""Returns WebSocket instance"""
class IHostContext(ABC, metaclass=ABCMeta):
@abstractmethod
def get_service_provider(self) -> "EllarInjector":
"""Gets RequestServiceProvider instance"""
@abstractmethod
def switch_to_http_connection(self) -> IHTTPHostContext:
"""Returns HTTPConnection instance"""
@abstractmethod
def switch_to_websocket(self) -> IWebSocketHostContext:
"""Returns WebSocket instance"""
@abstractmethod
def get_app(self) -> "App":
"""Gets application instance"""
@abstractmethod
def get_type(self) -> str:
"""returns scope type"""
@abstractmethod
def get_args(self) -> t.Tuple[TScope, TReceive, TSend]:
"""returns all args passed to asgi function"""
@abstractproperty
def user(self) -> "Identity":
"""gets user identity"""
@user.setter
def user(self, value: t.Any) -> None:
"""Sets user identity"""
class IExecutionContext(IHostContext, ABC):
@abstractmethod
def get_handler(self) -> t.Callable:
"""Gets operation handler"""
@abstractmethod
def get_class(self) -> t.Optional[t.Type["ControllerBase"]]:
"""Gets operation handler controller class"""
class IHostContextFactory(ABC):
@abstractmethod
def create_context(
self,
scope: TScope,
receive: TReceive = empty_receive,
send: TSend = empty_send,
) -> IHostContext:
"""Create Context Action"""
class IExecutionContextFactory(ABC):
@abstractmethod
def create_context(
self,
operation: "RouteOperationBase",
scope: TScope,
receive: TReceive = empty_receive,
send: TSend = empty_send,
) -> IExecutionContext:
"""Create Context Action"""
class SubHostContextFactory(t.Generic[T], ABC):
"""
Factory for creating HostContext types and validating them.
"""
context_type: t.Type[T]
__slots__ = ()
def __call__(self, context: IHostContext) -> T:
self.validate(context)
return self.create_context_type(context)
@abstractmethod
def validate(self, context: IHostContext) -> None:
"""Validation Action"""
def create_context_type(self, context: IHostContext) -> T:
scope, receive, send = context.get_args()
return self.context_type( # type:ignore
scope=scope,
receive=receive,
send=send,
)
class IHTTPConnectionContextFactory(SubHostContextFactory[IHTTPHostContext], ABC):
"""HttpContext Factory Interface"""
class IWebSocketContextFactory(SubHostContextFactory[IWebSocketHostContext], ABC):
"""WebsocketContext Factory Interface"""