Skip to content

Commit fc5572e

Browse files
committed
Update docs to reflect ExecutionContext refactor
1 parent 100e9d4 commit fc5572e

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

docs/handling-response/response.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Handling Responses
22

3-
## Define a response Schema
4-
53
**Ellar** allows you to define the schema of your responses both for validation and documentation purposes.
64

75
The response schema is defined on the HTTP method decorator and its applied

docs/overview/controllers.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ from ellar.core import ControllerBase, Request
124124
class DogsController(ControllerBase):
125125
@get()
126126
def get_all(self):
127-
assert isinstance(self.context.switch_to_request(), Request) # True
127+
assert isinstance(self.context.switch_to_http_connection().get_request(), Request) # True
128128
return 'This action returns all dogs'
129129
...
130130
```
@@ -164,7 +164,7 @@ class DogsController(ControllerBase):
164164

165165
@get()
166166
def get_all(self):
167-
assert isinstance(self.context.switch_to_request(), Request) # True
167+
assert isinstance(self.context.switch_to_http_connection().get_request(), Request) # True
168168
return 'This action returns all dogs'
169169
...
170170
```
@@ -201,7 +201,7 @@ class DogsController(ControllerBase):
201201

202202
@get()
203203
async def get_all(self):
204-
assert isinstance(self.context.switch_to_request(), Request) # True
204+
assert isinstance(self.context.switch_to_http_connection().get_request(), Request) # True
205205
return 'This action returns all dogs'
206206
...
207207
```
@@ -292,7 +292,7 @@ class DogsController(ControllerBase):
292292

293293
@get()
294294
async def get_all(self, query: DogListFilter = Query()):
295-
assert isinstance(self.context.switch_to_request(), Request) # True
295+
assert isinstance(self.context.switch_to_http_connection().get_request(), Request) # True
296296
return f'This action returns all dogs at limit={query.limit}, offset={query.offset}'
297297

298298
```

docs/overview/exception_handling.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ At the root project folder, create a file `custom_exceptions.py`,
9191
# project_name/custom_exceptions.py
9292
import typing as t
9393
from ellar.core.exceptions import IExceptionHandler
94-
from ellar.core.context import IExecutionContext
94+
from ellar.core.context import IHostContext
9595
from starlette.responses import Response
9696

9797

@@ -103,7 +103,7 @@ class MyCustomExceptionHandler(IExceptionHandler):
103103
exception_type_or_code = MyCustomException
104104

105105
async def catch(
106-
self, ctx: IExecutionContext, exc: MyCustomException
106+
self, ctx: IHostContext, exc: MyCustomException
107107
) -> t.Union[Response, t.Any]:
108108
app_config = ctx.get_app().config
109109
return app_config.DEFAULT_JSON_CLASS(
@@ -123,7 +123,7 @@ Let's create a handler for `MethodNotAllowedException` which, according to the H
123123
# project_name/apps/custom_exceptions.py
124124
import typing as t
125125
from ellar.core.exceptions import IExceptionHandler
126-
from ellar.core.context import IExecutionContext
126+
from ellar.core.context import IHostContext
127127
from ellar.core import render_template
128128
from starlette.responses import Response
129129
from starlette.exceptions import HTTPException
@@ -136,7 +136,7 @@ class MyCustomExceptionHandler(IExceptionHandler):
136136
exception_type_or_code = MyCustomException
137137

138138
async def catch(
139-
self, ctx: IExecutionContext, exc: MyCustomException
139+
self, ctx: IHostContext, exc: MyCustomException
140140
) -> t.Union[Response, t.Any]:
141141
app_config = ctx.get_app().config
142142
return app_config.DEFAULT_JSON_CLASS(
@@ -148,10 +148,10 @@ class ExceptionHandlerAction405(IExceptionHandler):
148148
exception_type_or_code = 405
149149

150150
async def catch(
151-
self, ctx: IExecutionContext, exc: HTTPException
151+
self, ctx: IHostContext, exc: HTTPException
152152
) -> t.Union[Response, t.Any]:
153153
context_kwargs = {}
154-
return render_template('405.html', request=ctx.switch_to_request(), **context_kwargs)
154+
return render_template('405.html', request=ctx.switch_to_http_connection().get_request(), **context_kwargs)
155155
```
156156
We have registered a handler for any `HTTP` exception with a `405` status code which we are returning a template `405.html` as a response.
157157

@@ -214,15 +214,15 @@ For example:
214214
# project_name/apps/custom_exceptions.py
215215
import typing as t
216216
from ellar.core.exceptions import IExceptionHandler, APIException
217-
from ellar.core.context import IExecutionContext
217+
from ellar.core.context import IHostContext
218218
from starlette.responses import Response
219219

220220

221221
class OverrideAPIExceptionHandler(IExceptionHandler):
222222
exception_type_or_code = APIException
223223

224224
async def catch(
225-
self, ctx: IExecutionContext, exc: APIException
225+
self, ctx: IHostContext, exc: APIException
226226
) -> t.Union[Response, t.Any]:
227227
app_config = ctx.get_app().config
228228
return app_config.DEFAULT_JSON_CLASS(

docs/overview/modules.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ Custom exception handlers can be registered through modules.
129129
```python
130130
from ellar.common import Module, exception_handler
131131
from ellar.core import ModuleBase, JSONResponse, Response
132-
from ellar.core.context import IExecutionContext
132+
from ellar.core.context import IHostContext
133133

134134
@Module()
135135
class ModuleExceptionSample(ModuleBase):
136136
@exception_handler(404)
137-
def exception_404_handler(cls, context: IExecutionContext, exc: Exception) -> Response:
137+
def exception_404_handler(cls, context: IHostContext, exc: Exception) -> Response:
138138
return JSONResponse(dict(detail="Resource not found."))
139139
```
140140
`exception_404_handler` will be register to the application at runtime during `ModuleExceptionSample` computation.
@@ -202,29 +202,28 @@ For example:
202202
```python
203203
from ellar.common import Module, middleware
204204
from ellar.core import ModuleBase
205-
from ellar.core.context import IExecutionContext
205+
from ellar.core.context import IHostContext
206206
from starlette.responses import PlainTextResponse
207207

208208

209209
@Module()
210210
class ModuleMiddlewareSample(ModuleBase):
211211
@middleware()
212-
async def my_middleware_function_1(cls, context: IExecutionContext, call_next):
213-
request = context.switch_to_request() # for http response only
212+
async def my_middleware_function_1(cls, context: IHostContext, call_next):
213+
request = context.switch_to_http_connection().get_request() # for http response only
214214
request.state.my_middleware_function_1 = True
215215
await call_next()
216216

217217
@middleware()
218-
async def my_middleware_function_2(cls, context: IExecutionContext, call_next):
219-
connection = context.switch_to_http_connection() # for websocket response only
220-
if connection.scope['type'] == 'websocket':
221-
websocket = context.switch_to_websocket()
218+
async def my_middleware_function_2(cls, context: IHostContext, call_next):
219+
if context.get_type() == 'websocket':
220+
websocket = context.switch_to_websocket().get_client()
222221
websocket.state.my_middleware_function_2 = True
223222
await call_next()
224223

225224
@middleware()
226-
async def my_middleware_function_3(cls, context: IExecutionContext, call_next):
227-
connection = context.switch_to_http_connection() # for http response only
225+
async def my_middleware_function_3(cls, context: IHostContext, call_next):
226+
connection = context.switch_to_http_connection().get_client() # for http response only
228227
if connection.headers['somekey']:
229228
# response = context.get_response() -> use the `response` to add extra definitions to things you want to see on
230229
return PlainTextResponse('Header is not allowed.')

ellar/core/middleware/di.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ def get_http_connection() -> HTTPConnection:
5858
http_connection_context = service_provider.get(IHTTPConnectionHost) # type: ignore
5959
return http_connection_context.get_client()
6060

61-
service_provider.register(HTTPConnection, CallableProvider(get_http_connection))
62-
service_provider.register(
61+
service_provider.update_context(
62+
HTTPConnection, CallableProvider(get_http_connection)
63+
)
64+
service_provider.update_context(
6365
StarletteHTTPConnection, CallableProvider(get_http_connection)
6466
)
6567

@@ -69,8 +71,10 @@ def get_request_connection() -> Request:
6971
http_connection_context = service_provider.get(IHTTPConnectionHost) # type: ignore
7072
return http_connection_context.get_request()
7173

72-
service_provider.register(Request, CallableProvider(get_request_connection))
73-
service_provider.register(
74+
service_provider.update_context(
75+
Request, CallableProvider(get_request_connection)
76+
)
77+
service_provider.update_context(
7478
StarletteRequest, CallableProvider(get_request_connection)
7579
)
7680

@@ -80,7 +84,7 @@ def get_response() -> Response:
8084
http_connection_context = service_provider.get(IHTTPConnectionHost) # type: ignore
8185
return http_connection_context.get_response()
8286

83-
service_provider.register(Response, CallableProvider(get_response))
87+
service_provider.update_context(Response, CallableProvider(get_response))
8488

8589
@classmethod
8690
def _register_websocket(cls, service_provider: "RequestServiceProvider") -> None:
@@ -89,8 +93,8 @@ def get_websocket_connection() -> WebSocket:
8993
return ws_connection_context.get_client()
9094

9195
websocket_context = CallableProvider(get_websocket_connection)
92-
service_provider.register(WebSocket, websocket_context)
93-
service_provider.register(StarletteWebSocket, websocket_context)
96+
service_provider.update_context(WebSocket, websocket_context)
97+
service_provider.update_context(StarletteWebSocket, websocket_context)
9498

9599
async def __call__(self, scope: TScope, receive: TReceive, send: TSend) -> None:
96100
if scope["type"] not in ["http", "websocket"]: # pragma: no cover
@@ -109,12 +113,12 @@ async def sender(message: TMessage) -> None:
109113
service_provider.update_context(t.cast(t.Type, IHostContext), host_context)
110114
service_provider.update_context(t.cast(t.Type, HostContext), host_context)
111115

112-
service_provider.register_scoped(
116+
service_provider.update_context(
113117
IHTTPConnectionHost,
114118
CallableProvider(HTTPConnectionContextFactory(host_context)),
115119
)
116120

117-
service_provider.register_scoped(
121+
service_provider.update_context(
118122
IWebSocketConnectionHost,
119123
CallableProvider(WebSocketContextFactory(host_context)),
120124
)

0 commit comments

Comments
 (0)