@@ -7,7 +7,7 @@ handler :term:`decorators <decorator>` exported from Litestar.
77For example:
88
99.. 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`
1111
1212 from litestar import get
1313
@@ -146,12 +146,11 @@ There are several reasons for why this limitation is enforced:
146146HTTP route handlers
147147------------------ -
148148
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` :
152151
153152.. 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> `
155154 :term:`decorator`
156155
157156 from litestar import HttpMethod, route
@@ -160,20 +159,24 @@ These route handlers all inherit from the :class:`~.handlers.HTTPRouteHandler` c
160159 @ route(path = " /some-path" , http_method = [HttpMethod.GET , HttpMethod.POST ])
161160 async def my_endpoint() -> None : ...
162161
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:
165163
166164.. 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> `
169167
170168 from litestar import HttpMethod
171169 from litestar.handlers.http_handlers import HTTPRouteHandler
172170
173171
174- @ HTTPRouteHandler(path = " /some-path" , http_method = [HttpMethod.GET , HttpMethod.POST ])
175172 async def my_endpoint() -> None : ...
176173
174+ handler = HTTPRouteHandler(
175+ path = " /some-path" ,
176+ http_method = [HttpMethod.GET , HttpMethod.POST ],
177+ fn = my_endpoint
178+ )
179+
177180
178181Semantic handler :term:`decorators < decorator> `
179182~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -189,8 +192,8 @@ which correlates with their name:
189192* :func:`@ post() < .handlers.post> `
190193* :func:`@ put() < .handlers.put> `
191194
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> ` :
194197
195198.. dropdown:: Click to see the predefined route handlers
196199
@@ -240,11 +243,6 @@ These are used exactly like :func:`@route() <.handlers.route>` with the sole exc
240243 @ delete(path = " /resources/{pk: int} " )
241244 async def delete_resource(pk: int ) -> None : ...
242245
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-
248246Furthermore, in the OpenAPI specification each unique combination of HTTP verb (e.g. `` GET `` , `` POST `` , etc.) and path
249247is regarded as a distinct `operation < https:// spec.openapis.org/ oas/ latest.html# operation-object>`_\ , and each
250248operation 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
277275 await socket.send_json({... })
278276 await socket.close()
279277
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:
282280
283281.. code- block:: python
284282 :caption: Using the :class :`~ .handlers.WebsocketRouteHandler` class directly
285283
286284 from litestar import WebSocket
287285 from litestar.handlers.websocket_handlers import WebsocketRouteHandler
288286
289-
290- @ WebsocketRouteHandler(path = " /socket" )
291287 async def my_websocket_handler(socket: WebSocket) -> None :
292288 await socket.accept()
293289 await socket.send_json({... })
294290 await socket.close()
295291
292+ my_websocket_handler = WebsocketRouteHandler(
293+ path = " /socket" ,
294+ fn = my_websocket_handler,
295+ )
296+
296297In difference to HTTP routes handlers, websocket handlers have the following requirements:
297298
298299# . 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:`@
332333 )
333334 await response(scope = scope, receive = receive, send = send)
334335
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:
337338
338339.. code- block:: python
339340 :caption: Using the :class :`~ .handlers.ASGIRouteHandler` class directly
@@ -343,8 +344,6 @@ Like other route handlers, the :func:`@asgi() <.handlers.asgi>` :term:`decorator
343344 from litestar.status_codes import HTTP_400_BAD_REQUEST
344345 from litestar.types import Scope, Receive, Send
345346
346-
347- @ ASGIRouteHandler(path = " /my-asgi-app" )
348347 async def my_asgi_app(scope: Scope, receive: Receive, send: Send) -> None :
349348 if scope[" type" ] == " http" :
350349 if scope[" method" ] == " GET" :
@@ -356,7 +355,10 @@ Like other route handlers, the :func:`@asgi() <.handlers.asgi>` :term:`decorator
356355 )
357356 await response(scope = scope, receive = receive, send = send)
358357
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
360362~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
361363
362364In difference to the other route handlers, the :func:`@ asgi() < .handlers.asgi> ` route handler accepts only three
0 commit comments