-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp.py
More file actions
66 lines (53 loc) · 2.04 KB
/
Copy pathhttp.py
File metadata and controls
66 lines (53 loc) · 2.04 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
import typing as t
from ellar.common.compatible import cached_property
from ellar.common.constants import SCOPED_RESPONSE
from ellar.common.exceptions import HostContextException
from ellar.common.interfaces import IHTTPHostContext
from ellar.common.types import TReceive, TScope, TSend
from ellar.core.connection import HTTPConnection, Request
from starlette.background import BackgroundTasks
from starlette.responses import Response
class HTTPHostContext(IHTTPHostContext):
"""
Provides a context around HTTP Connection
"""
__slots__ = (
"scope",
"receive",
"send",
"_response",
)
def __init__(self, scope: TScope, receive: TReceive, send: TSend) -> None:
self.scope = scope
self.receive = receive
self.send = send
self._response: t.Optional[Response] = None
@cached_property
def _http_connection(self) -> HTTPConnection:
return HTTPConnection(scope=self.scope, receive=self.receive)
@cached_property
def _request(self) -> Request:
if self.scope["type"] != "http":
raise HostContextException(
f"Request Context is not allow for scope[type]={self.scope['type']}"
)
return Request(scope=self.scope, receive=self.receive, send=self.send)
@property
def has_response(self) -> bool:
return SCOPED_RESPONSE in self.scope
def get_response(self) -> Response:
if SCOPED_RESPONSE not in self.scope:
if self.scope["type"] != "http":
raise HostContextException(
f"Response is not allow for connection type scope[type]={self.scope['type']}"
)
self.scope[SCOPED_RESPONSE] = Response(
background=BackgroundTasks(),
content=None,
status_code=-100,
)
return t.cast(Response, self.scope[SCOPED_RESPONSE])
def get_request(self) -> Request:
return self._request
def get_client(self) -> HTTPConnection:
return self._http_connection