File: fastapi.rst

package info (click to toggle)
litestar 2.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,568 kB
  • sloc: python: 70,588; makefile: 254; javascript: 104; sh: 60
file content (483 lines) | stat: -rw-r--r-- 14,519 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
From Starlette / FastAPI
------------------------

Routing Decorators
~~~~~~~~~~~~~~~~~~

Litestar does not include any decorator as part of the ``Router`` or ``Litestar`` instances.
Instead, all routes are declared using :doc:`route handlers </usage/routing/handlers>`, either as standalone functions or
controller methods. The handler can then be registered on an application or router instance.

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            from fastapi import FastAPI


            app = FastAPI()


            @app.get("/")
            async def index() -> dict[str, str]: ...

    .. tab-item:: Starlette
        :sync: starlette


        .. code-block:: python

            from starlette.applications import Starlette
            from starlette.routing import Route


            async def index(request): ...


            routes = [Route("/", endpoint=index)]

            app = Starlette(routes=routes)

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

           from litestar import Litestar, get


           @get("/")
           async def index() -> dict[str, str]: ...


           app = Litestar([index])


..  seealso::

    To learn more about registering routes, check out this chapter
    in the documentation:

    * :ref:`Routing - Registering Routes <usage/routing/overview:registering routes>`

Routers and Routes
~~~~~~~~~~~~~~~~~~

There are a few key differences between Litestar’s and Starlette’s ``Router`` class:

1. The Litestar version is not an ASGI app
2. The Litestar version does not include decorators: Use :doc:`route handlers </usage/routing/handlers>`.
3. The Litestar version does not support lifecycle hooks: Those have to be handled on the application layer. See :doc:`lifecycle hooks </usage/lifecycle-hooks>`

If you are using Starlette’s ``Route``\ s, you will need to replace these with :doc:`route handlers </usage/routing/handlers>`.

Host based routing
~~~~~~~~~~~~~~~~~~

Host based routing class is intentionally unsupported. If your application relies on ``Host`` you will have to separate
the logic into different services and handle this part of request dispatching with a proxy server like `nginx <https://www.nginx.com/>`_
or `traefik <https://traefik.io/>`_.

Dependency Injection
~~~~~~~~~~~~~~~~~~~~

The Litestar dependency injection system is different from the one used by FastAPI. You can read about it in
the :doc:`dependency injection </usage/dependency-injection>` section of the documentation.

In FastAPI you declare dependencies either as a list of functions passed to the ``Router`` or ``FastAPI`` instances, or as a
default function argument value wrapped in an instance of the ``Depends`` class.

In Litestar **dependencies are always declared using a dictionary** with a string key and the value wrapped in an
instance of the ``Provide`` class. This also allows to transparently override dependencies on every level of the application,
and to easily access dependencies from higher levels.

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

           from fastapi import FastAPI, Depends, APIRouter


           async def route_dependency() -> bool: ...


           async def nested_dependency() -> str: ...


           async def router_dependency() -> int: ...


           async def app_dependency(data: str = Depends(nested_dependency)) -> int: ...


           router = APIRouter(dependencies=[Depends(router_dependency)])
           app = FastAPI(dependencies=[Depends(nested_dependency)])
           app.include_router(router)


           @app.get("/")
           async def handler(
               val_route: bool = Depends(route_dependency),
               val_router: int = Depends(router_dependency),
               val_nested: str = Depends(nested_dependency),
               val_app: int = Depends(app_dependency),
           ) -> None: ...



    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

           from litestar import Litestar, get, Router
           from litestar.di import Provide


           async def route_dependency() -> bool: ...


           async def nested_dependency() -> str: ...


           async def router_dependency() -> int: ...


           async def app_dependency(nested: str) -> int: ...


           @get("/", dependencies={"val_route": Provide(route_dependency)})
           async def handler(
               val_route: bool, val_router: int, val_nested: str, val_app: int
           ) -> None: ...


           router = Router(dependencies={"val_router": Provide(router_dependency)})
           app = Litestar(
               route_handlers=[handler],
               dependencies={
                   "val_app": Provide(app_dependency),
                   "val_nested": Provide(nested_dependency),
               },
           )


..  seealso::

    To learn more about dependency injection, check out this chapter
    in the documentation:

    * :doc:`/usage/dependency-injection`

Lifespan
~~~~~~~~

Litestar uses the same async context manager style as FastAPI, so the code does not need to be changed:

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            @asynccontextmanager
            async def lifespan(
                app: FastAPI
            ):
                # Setup code here
                yield
                # Teardown code here

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            @asynccontextmanager
            async def lifespan(
                app: Litestar
            ):
                # Setup code here
                yield
                # Teardown code here


Cookies
~~~~~~~

While with FastAPI you usually set cookies on the response ``Response`` object, in Litestar there are two options: At the decorator level, using the ``response_cookies`` keyword argument, or dynamically at the response level (see: :ref:`Setting Cookies dynamically <usage/responses:setting cookies dynamically>`)

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            @app.get("/")
            async def index(response: Response) -> dict[str, str]:
                response.set_cookie(key="my_cookie", value="cookie_value")
                ...

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            @get(response_cookies={"my-cookie": "cookie-value"})
            async def handler() -> str:
                ...


Dependencies parameters
~~~~~~~~~~~~~~~~~~~~~~~
The way dependencies parameters are passed differs between FastAPI and Litestar, note the `state: State` parameter in the Litestar example.
You can get the state either with the state kwarg in the handler or ``request.state`` (which point to the same object, a request local state, inherited from the application's state), or via `request.app.state`, the application's state.

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            from fastapi import Request

            async def get_arqredis(request: Request) -> ArqRedis:
                return request.state.arqredis

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            from litestar import State

            async def get_arqredis(state: State) -> ArqRedis:
                return state.arqredis

Post json
~~~~~~~~~

In FastAPI, you pass the JSON object directly as a parameter to the endpoint, which will then be validated by Pydantic. In Litestar, you use the `data` keyword argument. The data will be parsed and validated by the associated modelling library.

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python


            class ObjectType(BaseModel):
                name: str

            @app.post("/items/")
            async def create_item(object_name: ObjectType) -> dict[str, str]:
                return {"name": object_name.name}

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            from litestar import Litestar, post
            from pydantic import BaseModel

            class ObjectType(BaseModel):
                name: str

            @post("/items/")
            async def create_item(data: ObjectType) -> dict[str, str]:
                return {"name": data.name}


Default status codes
~~~~~~~~~~~~~~~~~~~~

Post defaults to 200 in FastApi and 201 in Litestar.

Templates
~~~~~~~~~

In FastAPI, you use `TemplateResponse` to render templates. In Litestar, you use the `Template` class.
Also FastAPI let you pass a dictionary while in Litestar you need to explicitly pass the context kwarg.

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            @app.get("/uploads")
            async def get_uploads(request: Request):
                return templates.TemplateResponse(
                    "uploads.html", {"request": request, "debug": app.state.debug}
                )

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            @get("/uploads")
            async def get_uploads(app_settings) -> Template:
                return Template(
                    name="uploads.html", context={"debug": app_settings.debug}
                )

Uploads
~~~~~~~

In FastAPI, you use the `File` class to handle file uploads. In Litestar, you use the `data` keyword argument with `Body` and specify the `media_type` as `RequestEncodingType.MULTI_PART`.
While this is more verbose, it's also more explicit and communicates the intent more clearly.

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            @app.post("/upload/")
            async def upload_file(files: list[UploadFile] = File(...)) -> dict[str, str]:
                return {"file_names": [file.filename for file in files]}

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            @post("/upload/")
            async def upload_file(data: Annotated[list[UploadFile], Body(media_type=RequestEncodingType.MULTI_PART)]) -> dict[str, str]:
                return {"file_names": [file.filename for file in data]}

            app = Litestar([upload_file])


Exceptions signature
~~~~~~~~~~~~~~~~~~~~

In FastAPI, status code and exception details can be passed to `HTTPException` as positional arguments, while in Litestar they are set with keywords arguments, e.g. `status_code`. Positional arguments to `HTTPException` in Litestar will be added to the exception detail.
If migrating you just change your HTTPException import this will break.

.. tab-set::

    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            from fastapi import FastAPI, HTTPException

            app = FastAPI()

            @app.get("/")
            async def index() -> None:
                response_fields = {"array": "value"}
                raise HTTPException(
                    400, detail=f"can't get that field: {response_fields.get('array')}"
                )

    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            from litestar import Litestar, get
            from litestar.exceptions import HTTPException

            @get("/")
            async def index() -> None:
                response_fields = {"array": "value"}
                raise HTTPException(
                    status_code=400, detail=f"can't get that field: {response_fields.get('array')}"
                )

            app = Litestar([index])


Authentication
~~~~~~~~~~~~~~

FastAPI promotes a pattern of using dependency injection for authentication. You can do the same in Litestar, but the
preferred way of handling this is extending :doc:`/usage/security/abstract-authentication-middleware`.

.. tab-set::
    .. tab-item:: FastAPI
        :sync: fastapi

        .. code-block:: python

            from fastapi import FastAPI, Depends, Request


            async def authenticate(request: Request) -> None: ...


            app = FastAPI()


            @app.get("/", dependencies=[Depends(authenticate)])
            async def index() -> dict[str, str]: ...


    .. tab-item:: Litestar
        :sync: litestar

        .. code-block:: python

            from litestar import Litestar, get, ASGIConnection, BaseRouteHandler


            async def authenticate(
                connection: ASGIConnection, route_handler: BaseRouteHandler
            ) -> None: ...


            @get("/", guards=[authenticate])
            async def index() -> dict[str, str]: ...


..  seealso::

    To learn more about security and authentication, check out this chapter in the
    documentation:

    * :doc:`/usage/security/index`

Dependency overrides
~~~~~~~~~~~~~~~~~~~~

While FastAPI includes a mechanism to override dependencies on an existing application object,
Litestar promotes architectural solutions to the issue this is aimed to solve. Therefore, overriding
dependencies in Litestar is strictly supported at definition time, i.e. when you’re defining
handlers, controllers, routers, and applications. Dependency overrides are fundamentally
the same idea as mocking and should be approached with the same caution and used sparingly
instead of being the default.

To achieve the same effect there are three general approaches:

1. Structuring the application with different environments in mind. This could mean for example
   connecting to a different database depending on the environment, which in turn is set via
   and env-variable. This is sufficient and most cases and designing your application around this
   principle is a general good practice since it facilitates configurability and integration-testing
   capabilities
2. Isolating tests for unit testing and using ``create_test_client``
3. Resort to mocking if none of the above approaches can be made to work

Middleware
~~~~~~~~~~

Pure ASGI middleware is fully compatible, and can be used with any ASGI framework. Middlewares
that make use of FastAPI/Starlette specific middleware features such as
Starlette’s `BaseHTTPMiddleware <https://www.starlette.io/middleware/#basehttpmiddleware>`_ are not compatible,
but can be easily replaced by :doc:`Creating Middlewares </usage/middleware/creating-middleware>`.