@@ -7,7 +7,7 @@ handler :term:`decorators <decorator>` exported from Litestar.
7
7
For example:
8
8
9
9
.. code-block :: python
10
- :caption: Defining a route handler by decorating a function with the :class :`@ get() < .handlers.get> ` :term:`decorator`
10
+ :caption: Defining a route handler by decorating a function with the :func :`@ get() < .handlers.get> ` :term:`decorator`
11
11
12
12
from litestar import get
13
13
@@ -146,12 +146,11 @@ There are several reasons for why this limitation is enforced:
146
146
HTTP route handlers
147
147
------------------ -
148
148
149
- The most commonly used route handlers are those that handle HTTP requests and responses.
150
- These route handlers all inherit from the :class :`~ .handlers.HTTPRouteHandler` class , which is aliased as the
151
- :term:`decorator` called :func:`~ .handlers.route` :
149
+ The :class :`~ .handlers.HTTPRouteHandler` is used to handle HTTP requests, and can be
150
+ created with the :func:`~ .handlers.route` :term:`decorator` :
152
151
153
152
.. code- block:: python
154
- :caption: Defining a route handler by decorating a function with the :class :`@ route() < .handlers.route> `
153
+ :caption: Defining a route handler by decorating a function with the :func :`@ route() < .handlers.route> `
155
154
:term:`decorator`
156
155
157
156
from litestar import HttpMethod, route
@@ -160,20 +159,24 @@ These route handlers all inherit from the :class:`~.handlers.HTTPRouteHandler` c
160
159
@ route(path = " /some-path" , http_method = [HttpMethod.GET , HttpMethod.POST ])
161
160
async def my_endpoint() -> None : ...
162
161
163
- As mentioned above, :func:`@ route() < .handlers.route> ` is merely an alias for `` HTTPRouteHandler`` ,
164
- thus the below code is equivalent to the one above:
162
+ The same can be achieved without a decorator, by using `` HTTPRouteHandler`` directly:
165
163
166
164
.. code- block:: python
167
- :caption: Defining a route handler by decorating a function with the
168
- :class :`HTTPRouteHandler < .handlers.HTTPRouteHandler> ` class
165
+ :caption: Defining a route handler creating an instance of
166
+ :class :`HTTPRouteHandler < .handlers.HTTPRouteHandler> `
169
167
170
168
from litestar import HttpMethod
171
169
from litestar.handlers.http_handlers import HTTPRouteHandler
172
170
173
171
174
- @ HTTPRouteHandler(path = " /some-path" , http_method = [HttpMethod.GET , HttpMethod.POST ])
175
172
async def my_endpoint() -> None : ...
176
173
174
+ handler = HTTPRouteHandler(
175
+ path = " /some-path" ,
176
+ http_method = [HttpMethod.GET , HttpMethod.POST ],
177
+ fn = my_endpoint
178
+ )
179
+
177
180
178
181
Semantic handler :term:`decorators < decorator> `
179
182
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -189,8 +192,8 @@ which correlates with their name:
189
192
* :func:`@ post() < .handlers.post> `
190
193
* :func:`@ put() < .handlers.put> `
191
194
192
- These are used exactly like :func:`@ route() < .handlers.route> ` with the sole exception that you cannot configure the
193
- :paramref:`~ .handlers.HTTPRouteHandler.http_method` :term:`kwarg < argument> ` :
195
+ These are used exactly like :func:`@ route() < .handlers.route> ` with the sole exception that you don ' t need to configure
196
+ the :paramref:`~ .handlers.HTTPRouteHandler.http_method` :term:`kwarg < argument> ` :
194
197
195
198
.. dropdown:: Click to see the predefined route handlers
196
199
@@ -240,11 +243,6 @@ These are used exactly like :func:`@route() <.handlers.route>` with the sole exc
240
243
@ delete(path = " /resources/{pk: int} " )
241
244
async def delete_resource(pk: int ) -> None : ...
242
245
243
- Although these :term:`decorators < decorator> ` are merely subclasses of :class :`~ .handlers.HTTPRouteHandler` that pre- set
244
- the :paramref:`~ .handlers.HTTPRouteHandler.http_method` , using :func:`@ get() < .handlers.get> ` ,
245
- :func:`@ patch() < .handlers.patch> ` , :func:`@ put() < .handlers.put> ` , :func:`@ delete() < .handlers.delete> ` , or
246
- :func:`@ post() < .handlers.post> ` instead of :func:`@ route() < .handlers.route> ` makes the code clearer and simpler.
247
-
248
246
Furthermore, in the OpenAPI specification each unique combination of HTTP verb (e.g. `` GET `` , `` POST `` , etc.) and path
249
247
is regarded as a distinct `operation < https:// spec.openapis.org/ oas/ latest.html# operation-object>`_\ , and each
250
248
operation should be distinguished by a unique :paramref:`~ .handlers.HTTPRouteHandler.operation_id` and optimally
@@ -277,22 +275,25 @@ A WebSocket connection can be handled with a :func:`@websocket() <.handlers.Webs
277
275
await socket.send_json({... })
278
276
await socket.close()
279
277
280
- The :func:`@ websocket() < .handlers.WebsocketRouteHandler> ` :term:`decorator` is an alias of the
281
- :class :`~ .handlers.WebsocketRouteHandler` class . Thus , the below code is equivalent to the one above:
278
+ The :func:`@ websocket() < .handlers.WebsocketRouteHandler> ` :term:`decorator` can be used to create an instance of
279
+ :class :`~ .handlers.WebsocketRouteHandler` . Therefore , the below code is equivalent to the one above:
282
280
283
281
.. code- block:: python
284
282
:caption: Using the :class :`~ .handlers.WebsocketRouteHandler` class directly
285
283
286
284
from litestar import WebSocket
287
285
from litestar.handlers.websocket_handlers import WebsocketRouteHandler
288
286
289
-
290
- @ WebsocketRouteHandler(path = " /socket" )
291
287
async def my_websocket_handler(socket: WebSocket) -> None :
292
288
await socket.accept()
293
289
await socket.send_json({... })
294
290
await socket.close()
295
291
292
+ my_websocket_handler = WebsocketRouteHandler(
293
+ path = " /socket" ,
294
+ fn = my_websocket_handler,
295
+ )
296
+
296
297
In difference to HTTP routes handlers, websocket handlers have the following requirements:
297
298
298
299
# . They **must** declare a ``socket`` :term:`kwarg <argument>`.
@@ -332,8 +333,8 @@ If you need to write your own ASGI application, you can do so using the :func:`@
332
333
)
333
334
await response(scope = scope, receive = receive, send = send)
334
335
335
- Like other route handlers, the :func:`@ asgi() < .handlers.asgi> ` :term:`decorator` is an alias of the
336
- :class :`~ .handlers.ASGIRouteHandler` class . Thus , the code below is equivalent to the one above:
336
+ :func:`@ asgi() < .handlers.asgi> ` :term:`decorator` can be used to create an instance of
337
+ :class :`~ .handlers.ASGIRouteHandler` . Therefore , the code below is equivalent to the one above:
337
338
338
339
.. code- block:: python
339
340
:caption: Using the :class :`~ .handlers.ASGIRouteHandler` class directly
@@ -343,8 +344,6 @@ Like other route handlers, the :func:`@asgi() <.handlers.asgi>` :term:`decorator
343
344
from litestar.status_codes import HTTP_400_BAD_REQUEST
344
345
from litestar.types import Scope, Receive, Send
345
346
346
-
347
- @ ASGIRouteHandler(path = " /my-asgi-app" )
348
347
async def my_asgi_app(scope: Scope, receive: Receive, send: Send) -> None :
349
348
if scope[" type" ] == " http" :
350
349
if scope[" method" ] == " GET" :
@@ -356,7 +355,10 @@ Like other route handlers, the :func:`@asgi() <.handlers.asgi>` :term:`decorator
356
355
)
357
356
await response(scope = scope, receive = receive, send = send)
358
357
359
- Limitations of ASGI route handlers
358
+ my_asgi_app = ASGIRouteHandler(path = " /my-asgi-app" , fn = my_asgi_app)
359
+
360
+
361
+ ASGI route handler considerations
360
362
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
361
363
362
364
In difference to the other route handlers, the :func:`@ asgi() < .handlers.asgi> ` route handler accepts only three
0 commit comments