fastapi 0.128.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. fastapi/__init__.py +25 -0
  2. fastapi/__main__.py +3 -0
  3. fastapi/_compat/__init__.py +41 -0
  4. fastapi/_compat/shared.py +206 -0
  5. fastapi/_compat/v2.py +568 -0
  6. fastapi/applications.py +4669 -0
  7. fastapi/background.py +60 -0
  8. fastapi/cli.py +13 -0
  9. fastapi/concurrency.py +41 -0
  10. fastapi/datastructures.py +183 -0
  11. fastapi/dependencies/__init__.py +0 -0
  12. fastapi/dependencies/models.py +193 -0
  13. fastapi/dependencies/utils.py +1021 -0
  14. fastapi/encoders.py +346 -0
  15. fastapi/exception_handlers.py +34 -0
  16. fastapi/exceptions.py +246 -0
  17. fastapi/logger.py +3 -0
  18. fastapi/middleware/__init__.py +1 -0
  19. fastapi/middleware/asyncexitstack.py +18 -0
  20. fastapi/middleware/cors.py +1 -0
  21. fastapi/middleware/gzip.py +1 -0
  22. fastapi/middleware/httpsredirect.py +3 -0
  23. fastapi/middleware/trustedhost.py +3 -0
  24. fastapi/middleware/wsgi.py +1 -0
  25. fastapi/openapi/__init__.py +0 -0
  26. fastapi/openapi/constants.py +3 -0
  27. fastapi/openapi/docs.py +344 -0
  28. fastapi/openapi/models.py +438 -0
  29. fastapi/openapi/utils.py +567 -0
  30. fastapi/param_functions.py +2369 -0
  31. fastapi/params.py +755 -0
  32. fastapi/py.typed +0 -0
  33. fastapi/requests.py +2 -0
  34. fastapi/responses.py +48 -0
  35. fastapi/routing.py +4508 -0
  36. fastapi/security/__init__.py +15 -0
  37. fastapi/security/api_key.py +318 -0
  38. fastapi/security/base.py +6 -0
  39. fastapi/security/http.py +423 -0
  40. fastapi/security/oauth2.py +663 -0
  41. fastapi/security/open_id_connect_url.py +94 -0
  42. fastapi/security/utils.py +10 -0
  43. fastapi/staticfiles.py +1 -0
  44. fastapi/templating.py +1 -0
  45. fastapi/testclient.py +1 -0
  46. fastapi/types.py +11 -0
  47. fastapi/utils.py +164 -0
  48. fastapi/websockets.py +3 -0
  49. fastapi-0.128.0.dist-info/METADATA +645 -0
  50. fastapi-0.128.0.dist-info/RECORD +53 -0
  51. fastapi-0.128.0.dist-info/WHEEL +4 -0
  52. fastapi-0.128.0.dist-info/entry_points.txt +5 -0
  53. fastapi-0.128.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,4669 @@
1
+ from collections.abc import Awaitable, Coroutine, Sequence
2
+ from enum import Enum
3
+ from typing import (
4
+ Annotated,
5
+ Any,
6
+ Callable,
7
+ Optional,
8
+ TypeVar,
9
+ Union,
10
+ )
11
+
12
+ from annotated_doc import Doc
13
+ from fastapi import routing
14
+ from fastapi.datastructures import Default, DefaultPlaceholder
15
+ from fastapi.exception_handlers import (
16
+ http_exception_handler,
17
+ request_validation_exception_handler,
18
+ websocket_request_validation_exception_handler,
19
+ )
20
+ from fastapi.exceptions import RequestValidationError, WebSocketRequestValidationError
21
+ from fastapi.logger import logger
22
+ from fastapi.middleware.asyncexitstack import AsyncExitStackMiddleware
23
+ from fastapi.openapi.docs import (
24
+ get_redoc_html,
25
+ get_swagger_ui_html,
26
+ get_swagger_ui_oauth2_redirect_html,
27
+ )
28
+ from fastapi.openapi.utils import get_openapi
29
+ from fastapi.params import Depends
30
+ from fastapi.types import DecoratedCallable, IncEx
31
+ from fastapi.utils import generate_unique_id
32
+ from starlette.applications import Starlette
33
+ from starlette.datastructures import State
34
+ from starlette.exceptions import HTTPException
35
+ from starlette.middleware import Middleware
36
+ from starlette.middleware.base import BaseHTTPMiddleware
37
+ from starlette.middleware.errors import ServerErrorMiddleware
38
+ from starlette.middleware.exceptions import ExceptionMiddleware
39
+ from starlette.requests import Request
40
+ from starlette.responses import HTMLResponse, JSONResponse, Response
41
+ from starlette.routing import BaseRoute
42
+ from starlette.types import ASGIApp, ExceptionHandler, Lifespan, Receive, Scope, Send
43
+ from typing_extensions import deprecated
44
+
45
+ AppType = TypeVar("AppType", bound="FastAPI")
46
+
47
+
48
+ class FastAPI(Starlette):
49
+ """
50
+ `FastAPI` app class, the main entrypoint to use FastAPI.
51
+
52
+ Read more in the
53
+ [FastAPI docs for First Steps](https://fastapi.tiangolo.com/tutorial/first-steps/).
54
+
55
+ ## Example
56
+
57
+ ```python
58
+ from fastapi import FastAPI
59
+
60
+ app = FastAPI()
61
+ ```
62
+ """
63
+
64
+ def __init__(
65
+ self: AppType,
66
+ *,
67
+ debug: Annotated[
68
+ bool,
69
+ Doc(
70
+ """
71
+ Boolean indicating if debug tracebacks should be returned on server
72
+ errors.
73
+
74
+ Read more in the
75
+ [Starlette docs for Applications](https://www.starlette.dev/applications/#instantiating-the-application).
76
+ """
77
+ ),
78
+ ] = False,
79
+ routes: Annotated[
80
+ Optional[list[BaseRoute]],
81
+ Doc(
82
+ """
83
+ **Note**: you probably shouldn't use this parameter, it is inherited
84
+ from Starlette and supported for compatibility.
85
+
86
+ ---
87
+
88
+ A list of routes to serve incoming HTTP and WebSocket requests.
89
+ """
90
+ ),
91
+ deprecated(
92
+ """
93
+ You normally wouldn't use this parameter with FastAPI, it is inherited
94
+ from Starlette and supported for compatibility.
95
+
96
+ In FastAPI, you normally would use the *path operation methods*,
97
+ like `app.get()`, `app.post()`, etc.
98
+ """
99
+ ),
100
+ ] = None,
101
+ title: Annotated[
102
+ str,
103
+ Doc(
104
+ """
105
+ The title of the API.
106
+
107
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
108
+
109
+ Read more in the
110
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-api).
111
+
112
+ **Example**
113
+
114
+ ```python
115
+ from fastapi import FastAPI
116
+
117
+ app = FastAPI(title="ChimichangApp")
118
+ ```
119
+ """
120
+ ),
121
+ ] = "FastAPI",
122
+ summary: Annotated[
123
+ Optional[str],
124
+ Doc(
125
+ """
126
+ A short summary of the API.
127
+
128
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
129
+
130
+ Read more in the
131
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-api).
132
+
133
+ **Example**
134
+
135
+ ```python
136
+ from fastapi import FastAPI
137
+
138
+ app = FastAPI(summary="Deadpond's favorite app. Nuff said.")
139
+ ```
140
+ """
141
+ ),
142
+ ] = None,
143
+ description: Annotated[
144
+ str,
145
+ Doc(
146
+ '''
147
+ A description of the API. Supports Markdown (using
148
+ [CommonMark syntax](https://commonmark.org/)).
149
+
150
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
151
+
152
+ Read more in the
153
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-api).
154
+
155
+ **Example**
156
+
157
+ ```python
158
+ from fastapi import FastAPI
159
+
160
+ app = FastAPI(
161
+ description="""
162
+ ChimichangApp API helps you do awesome stuff. 🚀
163
+
164
+ ## Items
165
+
166
+ You can **read items**.
167
+
168
+ ## Users
169
+
170
+ You will be able to:
171
+
172
+ * **Create users** (_not implemented_).
173
+ * **Read users** (_not implemented_).
174
+
175
+ """
176
+ )
177
+ ```
178
+ '''
179
+ ),
180
+ ] = "",
181
+ version: Annotated[
182
+ str,
183
+ Doc(
184
+ """
185
+ The version of the API.
186
+
187
+ **Note** This is the version of your application, not the version of
188
+ the OpenAPI specification nor the version of FastAPI being used.
189
+
190
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
191
+
192
+ Read more in the
193
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-api).
194
+
195
+ **Example**
196
+
197
+ ```python
198
+ from fastapi import FastAPI
199
+
200
+ app = FastAPI(version="0.0.1")
201
+ ```
202
+ """
203
+ ),
204
+ ] = "0.1.0",
205
+ openapi_url: Annotated[
206
+ Optional[str],
207
+ Doc(
208
+ """
209
+ The URL where the OpenAPI schema will be served from.
210
+
211
+ If you set it to `None`, no OpenAPI schema will be served publicly, and
212
+ the default automatic endpoints `/docs` and `/redoc` will also be
213
+ disabled.
214
+
215
+ Read more in the
216
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#openapi-url).
217
+
218
+ **Example**
219
+
220
+ ```python
221
+ from fastapi import FastAPI
222
+
223
+ app = FastAPI(openapi_url="/api/v1/openapi.json")
224
+ ```
225
+ """
226
+ ),
227
+ ] = "/openapi.json",
228
+ openapi_tags: Annotated[
229
+ Optional[list[dict[str, Any]]],
230
+ Doc(
231
+ """
232
+ A list of tags used by OpenAPI, these are the same `tags` you can set
233
+ in the *path operations*, like:
234
+
235
+ * `@app.get("/users/", tags=["users"])`
236
+ * `@app.get("/items/", tags=["items"])`
237
+
238
+ The order of the tags can be used to specify the order shown in
239
+ tools like Swagger UI, used in the automatic path `/docs`.
240
+
241
+ It's not required to specify all the tags used.
242
+
243
+ The tags that are not declared MAY be organized randomly or based
244
+ on the tools' logic. Each tag name in the list MUST be unique.
245
+
246
+ The value of each item is a `dict` containing:
247
+
248
+ * `name`: The name of the tag.
249
+ * `description`: A short description of the tag.
250
+ [CommonMark syntax](https://commonmark.org/) MAY be used for rich
251
+ text representation.
252
+ * `externalDocs`: Additional external documentation for this tag. If
253
+ provided, it would contain a `dict` with:
254
+ * `description`: A short description of the target documentation.
255
+ [CommonMark syntax](https://commonmark.org/) MAY be used for
256
+ rich text representation.
257
+ * `url`: The URL for the target documentation. Value MUST be in
258
+ the form of a URL.
259
+
260
+ Read more in the
261
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-tags).
262
+
263
+ **Example**
264
+
265
+ ```python
266
+ from fastapi import FastAPI
267
+
268
+ tags_metadata = [
269
+ {
270
+ "name": "users",
271
+ "description": "Operations with users. The **login** logic is also here.",
272
+ },
273
+ {
274
+ "name": "items",
275
+ "description": "Manage items. So _fancy_ they have their own docs.",
276
+ "externalDocs": {
277
+ "description": "Items external docs",
278
+ "url": "https://fastapi.tiangolo.com/",
279
+ },
280
+ },
281
+ ]
282
+
283
+ app = FastAPI(openapi_tags=tags_metadata)
284
+ ```
285
+ """
286
+ ),
287
+ ] = None,
288
+ servers: Annotated[
289
+ Optional[list[dict[str, Union[str, Any]]]],
290
+ Doc(
291
+ """
292
+ A `list` of `dict`s with connectivity information to a target server.
293
+
294
+ You would use it, for example, if your application is served from
295
+ different domains and you want to use the same Swagger UI in the
296
+ browser to interact with each of them (instead of having multiple
297
+ browser tabs open). Or if you want to leave fixed the possible URLs.
298
+
299
+ If the servers `list` is not provided, or is an empty `list`, the
300
+ `servers` property in the generated OpenAPI will be:
301
+
302
+ * a `dict` with a `url` value of the application's mounting point
303
+ (`root_path`) if it's different from `/`.
304
+ * otherwise, the `servers` property will be omitted from the OpenAPI
305
+ schema.
306
+
307
+ Each item in the `list` is a `dict` containing:
308
+
309
+ * `url`: A URL to the target host. This URL supports Server Variables
310
+ and MAY be relative, to indicate that the host location is relative
311
+ to the location where the OpenAPI document is being served. Variable
312
+ substitutions will be made when a variable is named in `{`brackets`}`.
313
+ * `description`: An optional string describing the host designated by
314
+ the URL. [CommonMark syntax](https://commonmark.org/) MAY be used for
315
+ rich text representation.
316
+ * `variables`: A `dict` between a variable name and its value. The value
317
+ is used for substitution in the server's URL template.
318
+
319
+ Read more in the
320
+ [FastAPI docs for Behind a Proxy](https://fastapi.tiangolo.com/advanced/behind-a-proxy/#additional-servers).
321
+
322
+ **Example**
323
+
324
+ ```python
325
+ from fastapi import FastAPI
326
+
327
+ app = FastAPI(
328
+ servers=[
329
+ {"url": "https://stag.example.com", "description": "Staging environment"},
330
+ {"url": "https://prod.example.com", "description": "Production environment"},
331
+ ]
332
+ )
333
+ ```
334
+ """
335
+ ),
336
+ ] = None,
337
+ dependencies: Annotated[
338
+ Optional[Sequence[Depends]],
339
+ Doc(
340
+ """
341
+ A list of global dependencies, they will be applied to each
342
+ *path operation*, including in sub-routers.
343
+
344
+ Read more about it in the
345
+ [FastAPI docs for Global Dependencies](https://fastapi.tiangolo.com/tutorial/dependencies/global-dependencies/).
346
+
347
+ **Example**
348
+
349
+ ```python
350
+ from fastapi import Depends, FastAPI
351
+
352
+ from .dependencies import func_dep_1, func_dep_2
353
+
354
+ app = FastAPI(dependencies=[Depends(func_dep_1), Depends(func_dep_2)])
355
+ ```
356
+ """
357
+ ),
358
+ ] = None,
359
+ default_response_class: Annotated[
360
+ type[Response],
361
+ Doc(
362
+ """
363
+ The default response class to be used.
364
+
365
+ Read more in the
366
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class).
367
+
368
+ **Example**
369
+
370
+ ```python
371
+ from fastapi import FastAPI
372
+ from fastapi.responses import ORJSONResponse
373
+
374
+ app = FastAPI(default_response_class=ORJSONResponse)
375
+ ```
376
+ """
377
+ ),
378
+ ] = Default(JSONResponse),
379
+ redirect_slashes: Annotated[
380
+ bool,
381
+ Doc(
382
+ """
383
+ Whether to detect and redirect slashes in URLs when the client doesn't
384
+ use the same format.
385
+
386
+ **Example**
387
+
388
+ ```python
389
+ from fastapi import FastAPI
390
+
391
+ app = FastAPI(redirect_slashes=True) # the default
392
+
393
+ @app.get("/items/")
394
+ async def read_items():
395
+ return [{"item_id": "Foo"}]
396
+ ```
397
+
398
+ With this app, if a client goes to `/items` (without a trailing slash),
399
+ they will be automatically redirected with an HTTP status code of 307
400
+ to `/items/`.
401
+ """
402
+ ),
403
+ ] = True,
404
+ docs_url: Annotated[
405
+ Optional[str],
406
+ Doc(
407
+ """
408
+ The path to the automatic interactive API documentation.
409
+ It is handled in the browser by Swagger UI.
410
+
411
+ The default URL is `/docs`. You can disable it by setting it to `None`.
412
+
413
+ If `openapi_url` is set to `None`, this will be automatically disabled.
414
+
415
+ Read more in the
416
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#docs-urls).
417
+
418
+ **Example**
419
+
420
+ ```python
421
+ from fastapi import FastAPI
422
+
423
+ app = FastAPI(docs_url="/documentation", redoc_url=None)
424
+ ```
425
+ """
426
+ ),
427
+ ] = "/docs",
428
+ redoc_url: Annotated[
429
+ Optional[str],
430
+ Doc(
431
+ """
432
+ The path to the alternative automatic interactive API documentation
433
+ provided by ReDoc.
434
+
435
+ The default URL is `/redoc`. You can disable it by setting it to `None`.
436
+
437
+ If `openapi_url` is set to `None`, this will be automatically disabled.
438
+
439
+ Read more in the
440
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#docs-urls).
441
+
442
+ **Example**
443
+
444
+ ```python
445
+ from fastapi import FastAPI
446
+
447
+ app = FastAPI(docs_url="/documentation", redoc_url="redocumentation")
448
+ ```
449
+ """
450
+ ),
451
+ ] = "/redoc",
452
+ swagger_ui_oauth2_redirect_url: Annotated[
453
+ Optional[str],
454
+ Doc(
455
+ """
456
+ The OAuth2 redirect endpoint for the Swagger UI.
457
+
458
+ By default it is `/docs/oauth2-redirect`.
459
+
460
+ This is only used if you use OAuth2 (with the "Authorize" button)
461
+ with Swagger UI.
462
+ """
463
+ ),
464
+ ] = "/docs/oauth2-redirect",
465
+ swagger_ui_init_oauth: Annotated[
466
+ Optional[dict[str, Any]],
467
+ Doc(
468
+ """
469
+ OAuth2 configuration for the Swagger UI, by default shown at `/docs`.
470
+
471
+ Read more about the available configuration options in the
472
+ [Swagger UI docs](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/).
473
+ """
474
+ ),
475
+ ] = None,
476
+ middleware: Annotated[
477
+ Optional[Sequence[Middleware]],
478
+ Doc(
479
+ """
480
+ List of middleware to be added when creating the application.
481
+
482
+ In FastAPI you would normally do this with `app.add_middleware()`
483
+ instead.
484
+
485
+ Read more in the
486
+ [FastAPI docs for Middleware](https://fastapi.tiangolo.com/tutorial/middleware/).
487
+ """
488
+ ),
489
+ ] = None,
490
+ exception_handlers: Annotated[
491
+ Optional[
492
+ dict[
493
+ Union[int, type[Exception]],
494
+ Callable[[Request, Any], Coroutine[Any, Any, Response]],
495
+ ]
496
+ ],
497
+ Doc(
498
+ """
499
+ A dictionary with handlers for exceptions.
500
+
501
+ In FastAPI, you would normally use the decorator
502
+ `@app.exception_handler()`.
503
+
504
+ Read more in the
505
+ [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).
506
+ """
507
+ ),
508
+ ] = None,
509
+ on_startup: Annotated[
510
+ Optional[Sequence[Callable[[], Any]]],
511
+ Doc(
512
+ """
513
+ A list of startup event handler functions.
514
+
515
+ You should instead use the `lifespan` handlers.
516
+
517
+ Read more in the [FastAPI docs for `lifespan`](https://fastapi.tiangolo.com/advanced/events/).
518
+ """
519
+ ),
520
+ ] = None,
521
+ on_shutdown: Annotated[
522
+ Optional[Sequence[Callable[[], Any]]],
523
+ Doc(
524
+ """
525
+ A list of shutdown event handler functions.
526
+
527
+ You should instead use the `lifespan` handlers.
528
+
529
+ Read more in the
530
+ [FastAPI docs for `lifespan`](https://fastapi.tiangolo.com/advanced/events/).
531
+ """
532
+ ),
533
+ ] = None,
534
+ lifespan: Annotated[
535
+ Optional[Lifespan[AppType]],
536
+ Doc(
537
+ """
538
+ A `Lifespan` context manager handler. This replaces `startup` and
539
+ `shutdown` functions with a single context manager.
540
+
541
+ Read more in the
542
+ [FastAPI docs for `lifespan`](https://fastapi.tiangolo.com/advanced/events/).
543
+ """
544
+ ),
545
+ ] = None,
546
+ terms_of_service: Annotated[
547
+ Optional[str],
548
+ Doc(
549
+ """
550
+ A URL to the Terms of Service for your API.
551
+
552
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
553
+
554
+ Read more at the
555
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-api).
556
+
557
+ **Example**
558
+
559
+ ```python
560
+ app = FastAPI(terms_of_service="http://example.com/terms/")
561
+ ```
562
+ """
563
+ ),
564
+ ] = None,
565
+ contact: Annotated[
566
+ Optional[dict[str, Union[str, Any]]],
567
+ Doc(
568
+ """
569
+ A dictionary with the contact information for the exposed API.
570
+
571
+ It can contain several fields.
572
+
573
+ * `name`: (`str`) The name of the contact person/organization.
574
+ * `url`: (`str`) A URL pointing to the contact information. MUST be in
575
+ the format of a URL.
576
+ * `email`: (`str`) The email address of the contact person/organization.
577
+ MUST be in the format of an email address.
578
+
579
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
580
+
581
+ Read more at the
582
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-api).
583
+
584
+ **Example**
585
+
586
+ ```python
587
+ app = FastAPI(
588
+ contact={
589
+ "name": "Deadpoolio the Amazing",
590
+ "url": "http://x-force.example.com/contact/",
591
+ "email": "dp@x-force.example.com",
592
+ }
593
+ )
594
+ ```
595
+ """
596
+ ),
597
+ ] = None,
598
+ license_info: Annotated[
599
+ Optional[dict[str, Union[str, Any]]],
600
+ Doc(
601
+ """
602
+ A dictionary with the license information for the exposed API.
603
+
604
+ It can contain several fields.
605
+
606
+ * `name`: (`str`) **REQUIRED** (if a `license_info` is set). The
607
+ license name used for the API.
608
+ * `identifier`: (`str`) An [SPDX](https://spdx.dev/) license expression
609
+ for the API. The `identifier` field is mutually exclusive of the `url`
610
+ field. Available since OpenAPI 3.1.0, FastAPI 0.99.0.
611
+ * `url`: (`str`) A URL to the license used for the API. This MUST be
612
+ the format of a URL.
613
+
614
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
615
+
616
+ Read more at the
617
+ [FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#metadata-for-api).
618
+
619
+ **Example**
620
+
621
+ ```python
622
+ app = FastAPI(
623
+ license_info={
624
+ "name": "Apache 2.0",
625
+ "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
626
+ }
627
+ )
628
+ ```
629
+ """
630
+ ),
631
+ ] = None,
632
+ openapi_prefix: Annotated[
633
+ str,
634
+ Doc(
635
+ """
636
+ A URL prefix for the OpenAPI URL.
637
+ """
638
+ ),
639
+ deprecated(
640
+ """
641
+ "openapi_prefix" has been deprecated in favor of "root_path", which
642
+ follows more closely the ASGI standard, is simpler, and more
643
+ automatic.
644
+ """
645
+ ),
646
+ ] = "",
647
+ root_path: Annotated[
648
+ str,
649
+ Doc(
650
+ """
651
+ A path prefix handled by a proxy that is not seen by the application
652
+ but is seen by external clients, which affects things like Swagger UI.
653
+
654
+ Read more about it at the
655
+ [FastAPI docs for Behind a Proxy](https://fastapi.tiangolo.com/advanced/behind-a-proxy/).
656
+
657
+ **Example**
658
+
659
+ ```python
660
+ from fastapi import FastAPI
661
+
662
+ app = FastAPI(root_path="/api/v1")
663
+ ```
664
+ """
665
+ ),
666
+ ] = "",
667
+ root_path_in_servers: Annotated[
668
+ bool,
669
+ Doc(
670
+ """
671
+ To disable automatically generating the URLs in the `servers` field
672
+ in the autogenerated OpenAPI using the `root_path`.
673
+
674
+ Read more about it in the
675
+ [FastAPI docs for Behind a Proxy](https://fastapi.tiangolo.com/advanced/behind-a-proxy/#disable-automatic-server-from-root_path).
676
+
677
+ **Example**
678
+
679
+ ```python
680
+ from fastapi import FastAPI
681
+
682
+ app = FastAPI(root_path_in_servers=False)
683
+ ```
684
+ """
685
+ ),
686
+ ] = True,
687
+ responses: Annotated[
688
+ Optional[dict[Union[int, str], dict[str, Any]]],
689
+ Doc(
690
+ """
691
+ Additional responses to be shown in OpenAPI.
692
+
693
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
694
+
695
+ Read more about it in the
696
+ [FastAPI docs for Additional Responses in OpenAPI](https://fastapi.tiangolo.com/advanced/additional-responses/).
697
+
698
+ And in the
699
+ [FastAPI docs for Bigger Applications](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies).
700
+ """
701
+ ),
702
+ ] = None,
703
+ callbacks: Annotated[
704
+ Optional[list[BaseRoute]],
705
+ Doc(
706
+ """
707
+ OpenAPI callbacks that should apply to all *path operations*.
708
+
709
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
710
+
711
+ Read more about it in the
712
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
713
+ """
714
+ ),
715
+ ] = None,
716
+ webhooks: Annotated[
717
+ Optional[routing.APIRouter],
718
+ Doc(
719
+ """
720
+ Add OpenAPI webhooks. This is similar to `callbacks` but it doesn't
721
+ depend on specific *path operations*.
722
+
723
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
724
+
725
+ **Note**: This is available since OpenAPI 3.1.0, FastAPI 0.99.0.
726
+
727
+ Read more about it in the
728
+ [FastAPI docs for OpenAPI Webhooks](https://fastapi.tiangolo.com/advanced/openapi-webhooks/).
729
+ """
730
+ ),
731
+ ] = None,
732
+ deprecated: Annotated[
733
+ Optional[bool],
734
+ Doc(
735
+ """
736
+ Mark all *path operations* as deprecated. You probably don't need it,
737
+ but it's available.
738
+
739
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
740
+
741
+ Read more about it in the
742
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
743
+ """
744
+ ),
745
+ ] = None,
746
+ include_in_schema: Annotated[
747
+ bool,
748
+ Doc(
749
+ """
750
+ To include (or not) all the *path operations* in the generated OpenAPI.
751
+ You probably don't need it, but it's available.
752
+
753
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
754
+
755
+ Read more about it in the
756
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
757
+ """
758
+ ),
759
+ ] = True,
760
+ swagger_ui_parameters: Annotated[
761
+ Optional[dict[str, Any]],
762
+ Doc(
763
+ """
764
+ Parameters to configure Swagger UI, the autogenerated interactive API
765
+ documentation (by default at `/docs`).
766
+
767
+ Read more about it in the
768
+ [FastAPI docs about how to Configure Swagger UI](https://fastapi.tiangolo.com/how-to/configure-swagger-ui/).
769
+ """
770
+ ),
771
+ ] = None,
772
+ generate_unique_id_function: Annotated[
773
+ Callable[[routing.APIRoute], str],
774
+ Doc(
775
+ """
776
+ Customize the function used to generate unique IDs for the *path
777
+ operations* shown in the generated OpenAPI.
778
+
779
+ This is particularly useful when automatically generating clients or
780
+ SDKs for your API.
781
+
782
+ Read more about it in the
783
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
784
+ """
785
+ ),
786
+ ] = Default(generate_unique_id),
787
+ separate_input_output_schemas: Annotated[
788
+ bool,
789
+ Doc(
790
+ """
791
+ Whether to generate separate OpenAPI schemas for request body and
792
+ response body when the results would be more precise.
793
+
794
+ This is particularly useful when automatically generating clients.
795
+
796
+ For example, if you have a model like:
797
+
798
+ ```python
799
+ from pydantic import BaseModel
800
+
801
+ class Item(BaseModel):
802
+ name: str
803
+ tags: list[str] = []
804
+ ```
805
+
806
+ When `Item` is used for input, a request body, `tags` is not required,
807
+ the client doesn't have to provide it.
808
+
809
+ But when using `Item` for output, for a response body, `tags` is always
810
+ available because it has a default value, even if it's just an empty
811
+ list. So, the client should be able to always expect it.
812
+
813
+ In this case, there would be two different schemas, one for input and
814
+ another one for output.
815
+ """
816
+ ),
817
+ ] = True,
818
+ openapi_external_docs: Annotated[
819
+ Optional[dict[str, Any]],
820
+ Doc(
821
+ """
822
+ This field allows you to provide additional external documentation links.
823
+ If provided, it must be a dictionary containing:
824
+
825
+ * `description`: A brief description of the external documentation.
826
+ * `url`: The URL pointing to the external documentation. The value **MUST**
827
+ be a valid URL format.
828
+
829
+ **Example**:
830
+
831
+ ```python
832
+ from fastapi import FastAPI
833
+
834
+ external_docs = {
835
+ "description": "Detailed API Reference",
836
+ "url": "https://example.com/api-docs",
837
+ }
838
+
839
+ app = FastAPI(openapi_external_docs=external_docs)
840
+ ```
841
+ """
842
+ ),
843
+ ] = None,
844
+ **extra: Annotated[
845
+ Any,
846
+ Doc(
847
+ """
848
+ Extra keyword arguments to be stored in the app, not used by FastAPI
849
+ anywhere.
850
+ """
851
+ ),
852
+ ],
853
+ ) -> None:
854
+ self.debug = debug
855
+ self.title = title
856
+ self.summary = summary
857
+ self.description = description
858
+ self.version = version
859
+ self.terms_of_service = terms_of_service
860
+ self.contact = contact
861
+ self.license_info = license_info
862
+ self.openapi_url = openapi_url
863
+ self.openapi_tags = openapi_tags
864
+ self.root_path_in_servers = root_path_in_servers
865
+ self.docs_url = docs_url
866
+ self.redoc_url = redoc_url
867
+ self.swagger_ui_oauth2_redirect_url = swagger_ui_oauth2_redirect_url
868
+ self.swagger_ui_init_oauth = swagger_ui_init_oauth
869
+ self.swagger_ui_parameters = swagger_ui_parameters
870
+ self.servers = servers or []
871
+ self.separate_input_output_schemas = separate_input_output_schemas
872
+ self.openapi_external_docs = openapi_external_docs
873
+ self.extra = extra
874
+ self.openapi_version: Annotated[
875
+ str,
876
+ Doc(
877
+ """
878
+ The version string of OpenAPI.
879
+
880
+ FastAPI will generate OpenAPI version 3.1.0, and will output that as
881
+ the OpenAPI version. But some tools, even though they might be
882
+ compatible with OpenAPI 3.1.0, might not recognize it as a valid.
883
+
884
+ So you could override this value to trick those tools into using
885
+ the generated OpenAPI. Have in mind that this is a hack. But if you
886
+ avoid using features added in OpenAPI 3.1.0, it might work for your
887
+ use case.
888
+
889
+ This is not passed as a parameter to the `FastAPI` class to avoid
890
+ giving the false idea that FastAPI would generate a different OpenAPI
891
+ schema. It is only available as an attribute.
892
+
893
+ **Example**
894
+
895
+ ```python
896
+ from fastapi import FastAPI
897
+
898
+ app = FastAPI()
899
+
900
+ app.openapi_version = "3.0.2"
901
+ ```
902
+ """
903
+ ),
904
+ ] = "3.1.0"
905
+ self.openapi_schema: Optional[dict[str, Any]] = None
906
+ if self.openapi_url:
907
+ assert self.title, "A title must be provided for OpenAPI, e.g.: 'My API'"
908
+ assert self.version, "A version must be provided for OpenAPI, e.g.: '2.1.0'"
909
+ # TODO: remove when discarding the openapi_prefix parameter
910
+ if openapi_prefix:
911
+ logger.warning(
912
+ '"openapi_prefix" has been deprecated in favor of "root_path", which '
913
+ "follows more closely the ASGI standard, is simpler, and more "
914
+ "automatic. Check the docs at "
915
+ "https://fastapi.tiangolo.com/advanced/sub-applications/"
916
+ )
917
+ self.webhooks: Annotated[
918
+ routing.APIRouter,
919
+ Doc(
920
+ """
921
+ The `app.webhooks` attribute is an `APIRouter` with the *path
922
+ operations* that will be used just for documentation of webhooks.
923
+
924
+ Read more about it in the
925
+ [FastAPI docs for OpenAPI Webhooks](https://fastapi.tiangolo.com/advanced/openapi-webhooks/).
926
+ """
927
+ ),
928
+ ] = webhooks or routing.APIRouter()
929
+ self.root_path = root_path or openapi_prefix
930
+ self.state: Annotated[
931
+ State,
932
+ Doc(
933
+ """
934
+ A state object for the application. This is the same object for the
935
+ entire application, it doesn't change from request to request.
936
+
937
+ You normally wouldn't use this in FastAPI, for most of the cases you
938
+ would instead use FastAPI dependencies.
939
+
940
+ This is simply inherited from Starlette.
941
+
942
+ Read more about it in the
943
+ [Starlette docs for Applications](https://www.starlette.dev/applications/#storing-state-on-the-app-instance).
944
+ """
945
+ ),
946
+ ] = State()
947
+ self.dependency_overrides: Annotated[
948
+ dict[Callable[..., Any], Callable[..., Any]],
949
+ Doc(
950
+ """
951
+ A dictionary with overrides for the dependencies.
952
+
953
+ Each key is the original dependency callable, and the value is the
954
+ actual dependency that should be called.
955
+
956
+ This is for testing, to replace expensive dependencies with testing
957
+ versions.
958
+
959
+ Read more about it in the
960
+ [FastAPI docs for Testing Dependencies with Overrides](https://fastapi.tiangolo.com/advanced/testing-dependencies/).
961
+ """
962
+ ),
963
+ ] = {}
964
+ self.router: routing.APIRouter = routing.APIRouter(
965
+ routes=routes,
966
+ redirect_slashes=redirect_slashes,
967
+ dependency_overrides_provider=self,
968
+ on_startup=on_startup,
969
+ on_shutdown=on_shutdown,
970
+ lifespan=lifespan,
971
+ default_response_class=default_response_class,
972
+ dependencies=dependencies,
973
+ callbacks=callbacks,
974
+ deprecated=deprecated,
975
+ include_in_schema=include_in_schema,
976
+ responses=responses,
977
+ generate_unique_id_function=generate_unique_id_function,
978
+ )
979
+ self.exception_handlers: dict[
980
+ Any, Callable[[Request, Any], Union[Response, Awaitable[Response]]]
981
+ ] = {} if exception_handlers is None else dict(exception_handlers)
982
+ self.exception_handlers.setdefault(HTTPException, http_exception_handler)
983
+ self.exception_handlers.setdefault(
984
+ RequestValidationError, request_validation_exception_handler
985
+ )
986
+ self.exception_handlers.setdefault(
987
+ WebSocketRequestValidationError,
988
+ # Starlette still has incorrect type specification for the handlers
989
+ websocket_request_validation_exception_handler, # type: ignore
990
+ )
991
+
992
+ self.user_middleware: list[Middleware] = (
993
+ [] if middleware is None else list(middleware)
994
+ )
995
+ self.middleware_stack: Union[ASGIApp, None] = None
996
+ self.setup()
997
+
998
+ def build_middleware_stack(self) -> ASGIApp:
999
+ # Duplicate/override from Starlette to add AsyncExitStackMiddleware
1000
+ # inside of ExceptionMiddleware, inside of custom user middlewares
1001
+ debug = self.debug
1002
+ error_handler = None
1003
+ exception_handlers: dict[Any, ExceptionHandler] = {}
1004
+
1005
+ for key, value in self.exception_handlers.items():
1006
+ if key in (500, Exception):
1007
+ error_handler = value
1008
+ else:
1009
+ exception_handlers[key] = value
1010
+
1011
+ middleware = (
1012
+ [Middleware(ServerErrorMiddleware, handler=error_handler, debug=debug)]
1013
+ + self.user_middleware
1014
+ + [
1015
+ Middleware(
1016
+ ExceptionMiddleware, handlers=exception_handlers, debug=debug
1017
+ ),
1018
+ # Add FastAPI-specific AsyncExitStackMiddleware for closing files.
1019
+ # Before this was also used for closing dependencies with yield but
1020
+ # those now have their own AsyncExitStack, to properly support
1021
+ # streaming responses while keeping compatibility with the previous
1022
+ # versions (as of writing 0.117.1) that allowed doing
1023
+ # except HTTPException inside a dependency with yield.
1024
+ # This needs to happen after user middlewares because those create a
1025
+ # new contextvars context copy by using a new AnyIO task group.
1026
+ # This AsyncExitStack preserves the context for contextvars, not
1027
+ # strictly necessary for closing files but it was one of the original
1028
+ # intentions.
1029
+ # If the AsyncExitStack lived outside of the custom middlewares and
1030
+ # contextvars were set, for example in a dependency with 'yield'
1031
+ # in that internal contextvars context, the values would not be
1032
+ # available in the outer context of the AsyncExitStack.
1033
+ # By placing the middleware and the AsyncExitStack here, inside all
1034
+ # user middlewares, the same context is used.
1035
+ # This is currently not needed, only for closing files, but used to be
1036
+ # important when dependencies with yield were closed here.
1037
+ Middleware(AsyncExitStackMiddleware),
1038
+ ]
1039
+ )
1040
+
1041
+ app = self.router
1042
+ for cls, args, kwargs in reversed(middleware):
1043
+ app = cls(app, *args, **kwargs)
1044
+ return app
1045
+
1046
+ def openapi(self) -> dict[str, Any]:
1047
+ """
1048
+ Generate the OpenAPI schema of the application. This is called by FastAPI
1049
+ internally.
1050
+
1051
+ The first time it is called it stores the result in the attribute
1052
+ `app.openapi_schema`, and next times it is called, it just returns that same
1053
+ result. To avoid the cost of generating the schema every time.
1054
+
1055
+ If you need to modify the generated OpenAPI schema, you could modify it.
1056
+
1057
+ Read more in the
1058
+ [FastAPI docs for OpenAPI](https://fastapi.tiangolo.com/how-to/extending-openapi/).
1059
+ """
1060
+ if not self.openapi_schema:
1061
+ self.openapi_schema = get_openapi(
1062
+ title=self.title,
1063
+ version=self.version,
1064
+ openapi_version=self.openapi_version,
1065
+ summary=self.summary,
1066
+ description=self.description,
1067
+ terms_of_service=self.terms_of_service,
1068
+ contact=self.contact,
1069
+ license_info=self.license_info,
1070
+ routes=self.routes,
1071
+ webhooks=self.webhooks.routes,
1072
+ tags=self.openapi_tags,
1073
+ servers=self.servers,
1074
+ separate_input_output_schemas=self.separate_input_output_schemas,
1075
+ external_docs=self.openapi_external_docs,
1076
+ )
1077
+ return self.openapi_schema
1078
+
1079
+ def setup(self) -> None:
1080
+ if self.openapi_url:
1081
+ urls = (server_data.get("url") for server_data in self.servers)
1082
+ server_urls = {url for url in urls if url}
1083
+
1084
+ async def openapi(req: Request) -> JSONResponse:
1085
+ root_path = req.scope.get("root_path", "").rstrip("/")
1086
+ if root_path not in server_urls:
1087
+ if root_path and self.root_path_in_servers:
1088
+ self.servers.insert(0, {"url": root_path})
1089
+ server_urls.add(root_path)
1090
+ return JSONResponse(self.openapi())
1091
+
1092
+ self.add_route(self.openapi_url, openapi, include_in_schema=False)
1093
+ if self.openapi_url and self.docs_url:
1094
+
1095
+ async def swagger_ui_html(req: Request) -> HTMLResponse:
1096
+ root_path = req.scope.get("root_path", "").rstrip("/")
1097
+ openapi_url = root_path + self.openapi_url
1098
+ oauth2_redirect_url = self.swagger_ui_oauth2_redirect_url
1099
+ if oauth2_redirect_url:
1100
+ oauth2_redirect_url = root_path + oauth2_redirect_url
1101
+ return get_swagger_ui_html(
1102
+ openapi_url=openapi_url,
1103
+ title=f"{self.title} - Swagger UI",
1104
+ oauth2_redirect_url=oauth2_redirect_url,
1105
+ init_oauth=self.swagger_ui_init_oauth,
1106
+ swagger_ui_parameters=self.swagger_ui_parameters,
1107
+ )
1108
+
1109
+ self.add_route(self.docs_url, swagger_ui_html, include_in_schema=False)
1110
+
1111
+ if self.swagger_ui_oauth2_redirect_url:
1112
+
1113
+ async def swagger_ui_redirect(req: Request) -> HTMLResponse:
1114
+ return get_swagger_ui_oauth2_redirect_html()
1115
+
1116
+ self.add_route(
1117
+ self.swagger_ui_oauth2_redirect_url,
1118
+ swagger_ui_redirect,
1119
+ include_in_schema=False,
1120
+ )
1121
+ if self.openapi_url and self.redoc_url:
1122
+
1123
+ async def redoc_html(req: Request) -> HTMLResponse:
1124
+ root_path = req.scope.get("root_path", "").rstrip("/")
1125
+ openapi_url = root_path + self.openapi_url
1126
+ return get_redoc_html(
1127
+ openapi_url=openapi_url, title=f"{self.title} - ReDoc"
1128
+ )
1129
+
1130
+ self.add_route(self.redoc_url, redoc_html, include_in_schema=False)
1131
+
1132
+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
1133
+ if self.root_path:
1134
+ scope["root_path"] = self.root_path
1135
+ await super().__call__(scope, receive, send)
1136
+
1137
+ def add_api_route(
1138
+ self,
1139
+ path: str,
1140
+ endpoint: Callable[..., Any],
1141
+ *,
1142
+ response_model: Any = Default(None),
1143
+ status_code: Optional[int] = None,
1144
+ tags: Optional[list[Union[str, Enum]]] = None,
1145
+ dependencies: Optional[Sequence[Depends]] = None,
1146
+ summary: Optional[str] = None,
1147
+ description: Optional[str] = None,
1148
+ response_description: str = "Successful Response",
1149
+ responses: Optional[dict[Union[int, str], dict[str, Any]]] = None,
1150
+ deprecated: Optional[bool] = None,
1151
+ methods: Optional[list[str]] = None,
1152
+ operation_id: Optional[str] = None,
1153
+ response_model_include: Optional[IncEx] = None,
1154
+ response_model_exclude: Optional[IncEx] = None,
1155
+ response_model_by_alias: bool = True,
1156
+ response_model_exclude_unset: bool = False,
1157
+ response_model_exclude_defaults: bool = False,
1158
+ response_model_exclude_none: bool = False,
1159
+ include_in_schema: bool = True,
1160
+ response_class: Union[type[Response], DefaultPlaceholder] = Default(
1161
+ JSONResponse
1162
+ ),
1163
+ name: Optional[str] = None,
1164
+ openapi_extra: Optional[dict[str, Any]] = None,
1165
+ generate_unique_id_function: Callable[[routing.APIRoute], str] = Default(
1166
+ generate_unique_id
1167
+ ),
1168
+ ) -> None:
1169
+ self.router.add_api_route(
1170
+ path,
1171
+ endpoint=endpoint,
1172
+ response_model=response_model,
1173
+ status_code=status_code,
1174
+ tags=tags,
1175
+ dependencies=dependencies,
1176
+ summary=summary,
1177
+ description=description,
1178
+ response_description=response_description,
1179
+ responses=responses,
1180
+ deprecated=deprecated,
1181
+ methods=methods,
1182
+ operation_id=operation_id,
1183
+ response_model_include=response_model_include,
1184
+ response_model_exclude=response_model_exclude,
1185
+ response_model_by_alias=response_model_by_alias,
1186
+ response_model_exclude_unset=response_model_exclude_unset,
1187
+ response_model_exclude_defaults=response_model_exclude_defaults,
1188
+ response_model_exclude_none=response_model_exclude_none,
1189
+ include_in_schema=include_in_schema,
1190
+ response_class=response_class,
1191
+ name=name,
1192
+ openapi_extra=openapi_extra,
1193
+ generate_unique_id_function=generate_unique_id_function,
1194
+ )
1195
+
1196
+ def api_route(
1197
+ self,
1198
+ path: str,
1199
+ *,
1200
+ response_model: Any = Default(None),
1201
+ status_code: Optional[int] = None,
1202
+ tags: Optional[list[Union[str, Enum]]] = None,
1203
+ dependencies: Optional[Sequence[Depends]] = None,
1204
+ summary: Optional[str] = None,
1205
+ description: Optional[str] = None,
1206
+ response_description: str = "Successful Response",
1207
+ responses: Optional[dict[Union[int, str], dict[str, Any]]] = None,
1208
+ deprecated: Optional[bool] = None,
1209
+ methods: Optional[list[str]] = None,
1210
+ operation_id: Optional[str] = None,
1211
+ response_model_include: Optional[IncEx] = None,
1212
+ response_model_exclude: Optional[IncEx] = None,
1213
+ response_model_by_alias: bool = True,
1214
+ response_model_exclude_unset: bool = False,
1215
+ response_model_exclude_defaults: bool = False,
1216
+ response_model_exclude_none: bool = False,
1217
+ include_in_schema: bool = True,
1218
+ response_class: type[Response] = Default(JSONResponse),
1219
+ name: Optional[str] = None,
1220
+ openapi_extra: Optional[dict[str, Any]] = None,
1221
+ generate_unique_id_function: Callable[[routing.APIRoute], str] = Default(
1222
+ generate_unique_id
1223
+ ),
1224
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
1225
+ def decorator(func: DecoratedCallable) -> DecoratedCallable:
1226
+ self.router.add_api_route(
1227
+ path,
1228
+ func,
1229
+ response_model=response_model,
1230
+ status_code=status_code,
1231
+ tags=tags,
1232
+ dependencies=dependencies,
1233
+ summary=summary,
1234
+ description=description,
1235
+ response_description=response_description,
1236
+ responses=responses,
1237
+ deprecated=deprecated,
1238
+ methods=methods,
1239
+ operation_id=operation_id,
1240
+ response_model_include=response_model_include,
1241
+ response_model_exclude=response_model_exclude,
1242
+ response_model_by_alias=response_model_by_alias,
1243
+ response_model_exclude_unset=response_model_exclude_unset,
1244
+ response_model_exclude_defaults=response_model_exclude_defaults,
1245
+ response_model_exclude_none=response_model_exclude_none,
1246
+ include_in_schema=include_in_schema,
1247
+ response_class=response_class,
1248
+ name=name,
1249
+ openapi_extra=openapi_extra,
1250
+ generate_unique_id_function=generate_unique_id_function,
1251
+ )
1252
+ return func
1253
+
1254
+ return decorator
1255
+
1256
+ def add_api_websocket_route(
1257
+ self,
1258
+ path: str,
1259
+ endpoint: Callable[..., Any],
1260
+ name: Optional[str] = None,
1261
+ *,
1262
+ dependencies: Optional[Sequence[Depends]] = None,
1263
+ ) -> None:
1264
+ self.router.add_api_websocket_route(
1265
+ path,
1266
+ endpoint,
1267
+ name=name,
1268
+ dependencies=dependencies,
1269
+ )
1270
+
1271
+ def websocket(
1272
+ self,
1273
+ path: Annotated[
1274
+ str,
1275
+ Doc(
1276
+ """
1277
+ WebSocket path.
1278
+ """
1279
+ ),
1280
+ ],
1281
+ name: Annotated[
1282
+ Optional[str],
1283
+ Doc(
1284
+ """
1285
+ A name for the WebSocket. Only used internally.
1286
+ """
1287
+ ),
1288
+ ] = None,
1289
+ *,
1290
+ dependencies: Annotated[
1291
+ Optional[Sequence[Depends]],
1292
+ Doc(
1293
+ """
1294
+ A list of dependencies (using `Depends()`) to be used for this
1295
+ WebSocket.
1296
+
1297
+ Read more about it in the
1298
+ [FastAPI docs for WebSockets](https://fastapi.tiangolo.com/advanced/websockets/).
1299
+ """
1300
+ ),
1301
+ ] = None,
1302
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
1303
+ """
1304
+ Decorate a WebSocket function.
1305
+
1306
+ Read more about it in the
1307
+ [FastAPI docs for WebSockets](https://fastapi.tiangolo.com/advanced/websockets/).
1308
+
1309
+ **Example**
1310
+
1311
+ ```python
1312
+ from fastapi import FastAPI, WebSocket
1313
+
1314
+ app = FastAPI()
1315
+
1316
+ @app.websocket("/ws")
1317
+ async def websocket_endpoint(websocket: WebSocket):
1318
+ await websocket.accept()
1319
+ while True:
1320
+ data = await websocket.receive_text()
1321
+ await websocket.send_text(f"Message text was: {data}")
1322
+ ```
1323
+ """
1324
+
1325
+ def decorator(func: DecoratedCallable) -> DecoratedCallable:
1326
+ self.add_api_websocket_route(
1327
+ path,
1328
+ func,
1329
+ name=name,
1330
+ dependencies=dependencies,
1331
+ )
1332
+ return func
1333
+
1334
+ return decorator
1335
+
1336
+ def include_router(
1337
+ self,
1338
+ router: Annotated[routing.APIRouter, Doc("The `APIRouter` to include.")],
1339
+ *,
1340
+ prefix: Annotated[str, Doc("An optional path prefix for the router.")] = "",
1341
+ tags: Annotated[
1342
+ Optional[list[Union[str, Enum]]],
1343
+ Doc(
1344
+ """
1345
+ A list of tags to be applied to all the *path operations* in this
1346
+ router.
1347
+
1348
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1349
+
1350
+ Read more about it in the
1351
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
1352
+ """
1353
+ ),
1354
+ ] = None,
1355
+ dependencies: Annotated[
1356
+ Optional[Sequence[Depends]],
1357
+ Doc(
1358
+ """
1359
+ A list of dependencies (using `Depends()`) to be applied to all the
1360
+ *path operations* in this router.
1361
+
1362
+ Read more about it in the
1363
+ [FastAPI docs for Bigger Applications - Multiple Files](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies).
1364
+
1365
+ **Example**
1366
+
1367
+ ```python
1368
+ from fastapi import Depends, FastAPI
1369
+
1370
+ from .dependencies import get_token_header
1371
+ from .internal import admin
1372
+
1373
+ app = FastAPI()
1374
+
1375
+ app.include_router(
1376
+ admin.router,
1377
+ dependencies=[Depends(get_token_header)],
1378
+ )
1379
+ ```
1380
+ """
1381
+ ),
1382
+ ] = None,
1383
+ responses: Annotated[
1384
+ Optional[dict[Union[int, str], dict[str, Any]]],
1385
+ Doc(
1386
+ """
1387
+ Additional responses to be shown in OpenAPI.
1388
+
1389
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1390
+
1391
+ Read more about it in the
1392
+ [FastAPI docs for Additional Responses in OpenAPI](https://fastapi.tiangolo.com/advanced/additional-responses/).
1393
+
1394
+ And in the
1395
+ [FastAPI docs for Bigger Applications](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies).
1396
+ """
1397
+ ),
1398
+ ] = None,
1399
+ deprecated: Annotated[
1400
+ Optional[bool],
1401
+ Doc(
1402
+ """
1403
+ Mark all the *path operations* in this router as deprecated.
1404
+
1405
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1406
+
1407
+ **Example**
1408
+
1409
+ ```python
1410
+ from fastapi import FastAPI
1411
+
1412
+ from .internal import old_api
1413
+
1414
+ app = FastAPI()
1415
+
1416
+ app.include_router(
1417
+ old_api.router,
1418
+ deprecated=True,
1419
+ )
1420
+ ```
1421
+ """
1422
+ ),
1423
+ ] = None,
1424
+ include_in_schema: Annotated[
1425
+ bool,
1426
+ Doc(
1427
+ """
1428
+ Include (or not) all the *path operations* in this router in the
1429
+ generated OpenAPI schema.
1430
+
1431
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
1432
+
1433
+ **Example**
1434
+
1435
+ ```python
1436
+ from fastapi import FastAPI
1437
+
1438
+ from .internal import old_api
1439
+
1440
+ app = FastAPI()
1441
+
1442
+ app.include_router(
1443
+ old_api.router,
1444
+ include_in_schema=False,
1445
+ )
1446
+ ```
1447
+ """
1448
+ ),
1449
+ ] = True,
1450
+ default_response_class: Annotated[
1451
+ type[Response],
1452
+ Doc(
1453
+ """
1454
+ Default response class to be used for the *path operations* in this
1455
+ router.
1456
+
1457
+ Read more in the
1458
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class).
1459
+
1460
+ **Example**
1461
+
1462
+ ```python
1463
+ from fastapi import FastAPI
1464
+ from fastapi.responses import ORJSONResponse
1465
+
1466
+ from .internal import old_api
1467
+
1468
+ app = FastAPI()
1469
+
1470
+ app.include_router(
1471
+ old_api.router,
1472
+ default_response_class=ORJSONResponse,
1473
+ )
1474
+ ```
1475
+ """
1476
+ ),
1477
+ ] = Default(JSONResponse),
1478
+ callbacks: Annotated[
1479
+ Optional[list[BaseRoute]],
1480
+ Doc(
1481
+ """
1482
+ List of *path operations* that will be used as OpenAPI callbacks.
1483
+
1484
+ This is only for OpenAPI documentation, the callbacks won't be used
1485
+ directly.
1486
+
1487
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1488
+
1489
+ Read more about it in the
1490
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
1491
+ """
1492
+ ),
1493
+ ] = None,
1494
+ generate_unique_id_function: Annotated[
1495
+ Callable[[routing.APIRoute], str],
1496
+ Doc(
1497
+ """
1498
+ Customize the function used to generate unique IDs for the *path
1499
+ operations* shown in the generated OpenAPI.
1500
+
1501
+ This is particularly useful when automatically generating clients or
1502
+ SDKs for your API.
1503
+
1504
+ Read more about it in the
1505
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
1506
+ """
1507
+ ),
1508
+ ] = Default(generate_unique_id),
1509
+ ) -> None:
1510
+ """
1511
+ Include an `APIRouter` in the same app.
1512
+
1513
+ Read more about it in the
1514
+ [FastAPI docs for Bigger Applications](https://fastapi.tiangolo.com/tutorial/bigger-applications/).
1515
+
1516
+ ## Example
1517
+
1518
+ ```python
1519
+ from fastapi import FastAPI
1520
+
1521
+ from .users import users_router
1522
+
1523
+ app = FastAPI()
1524
+
1525
+ app.include_router(users_router)
1526
+ ```
1527
+ """
1528
+ self.router.include_router(
1529
+ router,
1530
+ prefix=prefix,
1531
+ tags=tags,
1532
+ dependencies=dependencies,
1533
+ responses=responses,
1534
+ deprecated=deprecated,
1535
+ include_in_schema=include_in_schema,
1536
+ default_response_class=default_response_class,
1537
+ callbacks=callbacks,
1538
+ generate_unique_id_function=generate_unique_id_function,
1539
+ )
1540
+
1541
+ def get(
1542
+ self,
1543
+ path: Annotated[
1544
+ str,
1545
+ Doc(
1546
+ """
1547
+ The URL path to be used for this *path operation*.
1548
+
1549
+ For example, in `http://example.com/items`, the path is `/items`.
1550
+ """
1551
+ ),
1552
+ ],
1553
+ *,
1554
+ response_model: Annotated[
1555
+ Any,
1556
+ Doc(
1557
+ """
1558
+ The type to use for the response.
1559
+
1560
+ It could be any valid Pydantic *field* type. So, it doesn't have to
1561
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
1562
+ etc.
1563
+
1564
+ It will be used for:
1565
+
1566
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
1567
+ show it as the response (JSON Schema).
1568
+ * Serialization: you could return an arbitrary object and the
1569
+ `response_model` would be used to serialize that object into the
1570
+ corresponding JSON.
1571
+ * Filtering: the JSON sent to the client will only contain the data
1572
+ (fields) defined in the `response_model`. If you returned an object
1573
+ that contains an attribute `password` but the `response_model` does
1574
+ not include that field, the JSON sent to the client would not have
1575
+ that `password`.
1576
+ * Validation: whatever you return will be serialized with the
1577
+ `response_model`, converting any data as necessary to generate the
1578
+ corresponding JSON. But if the data in the object returned is not
1579
+ valid, that would mean a violation of the contract with the client,
1580
+ so it's an error from the API developer. So, FastAPI will raise an
1581
+ error and return a 500 error code (Internal Server Error).
1582
+
1583
+ Read more about it in the
1584
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
1585
+ """
1586
+ ),
1587
+ ] = Default(None),
1588
+ status_code: Annotated[
1589
+ Optional[int],
1590
+ Doc(
1591
+ """
1592
+ The default status code to be used for the response.
1593
+
1594
+ You could override the status code by returning a response directly.
1595
+
1596
+ Read more about it in the
1597
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
1598
+ """
1599
+ ),
1600
+ ] = None,
1601
+ tags: Annotated[
1602
+ Optional[list[Union[str, Enum]]],
1603
+ Doc(
1604
+ """
1605
+ A list of tags to be applied to the *path operation*.
1606
+
1607
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1608
+
1609
+ Read more about it in the
1610
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
1611
+ """
1612
+ ),
1613
+ ] = None,
1614
+ dependencies: Annotated[
1615
+ Optional[Sequence[Depends]],
1616
+ Doc(
1617
+ """
1618
+ A list of dependencies (using `Depends()`) to be applied to the
1619
+ *path operation*.
1620
+
1621
+ Read more about it in the
1622
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
1623
+ """
1624
+ ),
1625
+ ] = None,
1626
+ summary: Annotated[
1627
+ Optional[str],
1628
+ Doc(
1629
+ """
1630
+ A summary for the *path operation*.
1631
+
1632
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1633
+
1634
+ Read more about it in the
1635
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
1636
+ """
1637
+ ),
1638
+ ] = None,
1639
+ description: Annotated[
1640
+ Optional[str],
1641
+ Doc(
1642
+ """
1643
+ A description for the *path operation*.
1644
+
1645
+ If not provided, it will be extracted automatically from the docstring
1646
+ of the *path operation function*.
1647
+
1648
+ It can contain Markdown.
1649
+
1650
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1651
+
1652
+ Read more about it in the
1653
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
1654
+ """
1655
+ ),
1656
+ ] = None,
1657
+ response_description: Annotated[
1658
+ str,
1659
+ Doc(
1660
+ """
1661
+ The description for the default response.
1662
+
1663
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1664
+ """
1665
+ ),
1666
+ ] = "Successful Response",
1667
+ responses: Annotated[
1668
+ Optional[dict[Union[int, str], dict[str, Any]]],
1669
+ Doc(
1670
+ """
1671
+ Additional responses that could be returned by this *path operation*.
1672
+
1673
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1674
+ """
1675
+ ),
1676
+ ] = None,
1677
+ deprecated: Annotated[
1678
+ Optional[bool],
1679
+ Doc(
1680
+ """
1681
+ Mark this *path operation* as deprecated.
1682
+
1683
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1684
+ """
1685
+ ),
1686
+ ] = None,
1687
+ operation_id: Annotated[
1688
+ Optional[str],
1689
+ Doc(
1690
+ """
1691
+ Custom operation ID to be used by this *path operation*.
1692
+
1693
+ By default, it is generated automatically.
1694
+
1695
+ If you provide a custom operation ID, you need to make sure it is
1696
+ unique for the whole API.
1697
+
1698
+ You can customize the
1699
+ operation ID generation with the parameter
1700
+ `generate_unique_id_function` in the `FastAPI` class.
1701
+
1702
+ Read more about it in the
1703
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
1704
+ """
1705
+ ),
1706
+ ] = None,
1707
+ response_model_include: Annotated[
1708
+ Optional[IncEx],
1709
+ Doc(
1710
+ """
1711
+ Configuration passed to Pydantic to include only certain fields in the
1712
+ response data.
1713
+
1714
+ Read more about it in the
1715
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
1716
+ """
1717
+ ),
1718
+ ] = None,
1719
+ response_model_exclude: Annotated[
1720
+ Optional[IncEx],
1721
+ Doc(
1722
+ """
1723
+ Configuration passed to Pydantic to exclude certain fields in the
1724
+ response data.
1725
+
1726
+ Read more about it in the
1727
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
1728
+ """
1729
+ ),
1730
+ ] = None,
1731
+ response_model_by_alias: Annotated[
1732
+ bool,
1733
+ Doc(
1734
+ """
1735
+ Configuration passed to Pydantic to define if the response model
1736
+ should be serialized by alias when an alias is used.
1737
+
1738
+ Read more about it in the
1739
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
1740
+ """
1741
+ ),
1742
+ ] = True,
1743
+ response_model_exclude_unset: Annotated[
1744
+ bool,
1745
+ Doc(
1746
+ """
1747
+ Configuration passed to Pydantic to define if the response data
1748
+ should have all the fields, including the ones that were not set and
1749
+ have their default values. This is different from
1750
+ `response_model_exclude_defaults` in that if the fields are set,
1751
+ they will be included in the response, even if the value is the same
1752
+ as the default.
1753
+
1754
+ When `True`, default values are omitted from the response.
1755
+
1756
+ Read more about it in the
1757
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
1758
+ """
1759
+ ),
1760
+ ] = False,
1761
+ response_model_exclude_defaults: Annotated[
1762
+ bool,
1763
+ Doc(
1764
+ """
1765
+ Configuration passed to Pydantic to define if the response data
1766
+ should have all the fields, including the ones that have the same value
1767
+ as the default. This is different from `response_model_exclude_unset`
1768
+ in that if the fields are set but contain the same default values,
1769
+ they will be excluded from the response.
1770
+
1771
+ When `True`, default values are omitted from the response.
1772
+
1773
+ Read more about it in the
1774
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
1775
+ """
1776
+ ),
1777
+ ] = False,
1778
+ response_model_exclude_none: Annotated[
1779
+ bool,
1780
+ Doc(
1781
+ """
1782
+ Configuration passed to Pydantic to define if the response data should
1783
+ exclude fields set to `None`.
1784
+
1785
+ This is much simpler (less smart) than `response_model_exclude_unset`
1786
+ and `response_model_exclude_defaults`. You probably want to use one of
1787
+ those two instead of this one, as those allow returning `None` values
1788
+ when it makes sense.
1789
+
1790
+ Read more about it in the
1791
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
1792
+ """
1793
+ ),
1794
+ ] = False,
1795
+ include_in_schema: Annotated[
1796
+ bool,
1797
+ Doc(
1798
+ """
1799
+ Include this *path operation* in the generated OpenAPI schema.
1800
+
1801
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
1802
+
1803
+ Read more about it in the
1804
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
1805
+ """
1806
+ ),
1807
+ ] = True,
1808
+ response_class: Annotated[
1809
+ type[Response],
1810
+ Doc(
1811
+ """
1812
+ Response class to be used for this *path operation*.
1813
+
1814
+ This will not be used if you return a response directly.
1815
+
1816
+ Read more about it in the
1817
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
1818
+ """
1819
+ ),
1820
+ ] = Default(JSONResponse),
1821
+ name: Annotated[
1822
+ Optional[str],
1823
+ Doc(
1824
+ """
1825
+ Name for this *path operation*. Only used internally.
1826
+ """
1827
+ ),
1828
+ ] = None,
1829
+ callbacks: Annotated[
1830
+ Optional[list[BaseRoute]],
1831
+ Doc(
1832
+ """
1833
+ List of *path operations* that will be used as OpenAPI callbacks.
1834
+
1835
+ This is only for OpenAPI documentation, the callbacks won't be used
1836
+ directly.
1837
+
1838
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1839
+
1840
+ Read more about it in the
1841
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
1842
+ """
1843
+ ),
1844
+ ] = None,
1845
+ openapi_extra: Annotated[
1846
+ Optional[dict[str, Any]],
1847
+ Doc(
1848
+ """
1849
+ Extra metadata to be included in the OpenAPI schema for this *path
1850
+ operation*.
1851
+
1852
+ Read more about it in the
1853
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
1854
+ """
1855
+ ),
1856
+ ] = None,
1857
+ generate_unique_id_function: Annotated[
1858
+ Callable[[routing.APIRoute], str],
1859
+ Doc(
1860
+ """
1861
+ Customize the function used to generate unique IDs for the *path
1862
+ operations* shown in the generated OpenAPI.
1863
+
1864
+ This is particularly useful when automatically generating clients or
1865
+ SDKs for your API.
1866
+
1867
+ Read more about it in the
1868
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
1869
+ """
1870
+ ),
1871
+ ] = Default(generate_unique_id),
1872
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
1873
+ """
1874
+ Add a *path operation* using an HTTP GET operation.
1875
+
1876
+ ## Example
1877
+
1878
+ ```python
1879
+ from fastapi import FastAPI
1880
+
1881
+ app = FastAPI()
1882
+
1883
+ @app.get("/items/")
1884
+ def read_items():
1885
+ return [{"name": "Empanada"}, {"name": "Arepa"}]
1886
+ ```
1887
+ """
1888
+ return self.router.get(
1889
+ path,
1890
+ response_model=response_model,
1891
+ status_code=status_code,
1892
+ tags=tags,
1893
+ dependencies=dependencies,
1894
+ summary=summary,
1895
+ description=description,
1896
+ response_description=response_description,
1897
+ responses=responses,
1898
+ deprecated=deprecated,
1899
+ operation_id=operation_id,
1900
+ response_model_include=response_model_include,
1901
+ response_model_exclude=response_model_exclude,
1902
+ response_model_by_alias=response_model_by_alias,
1903
+ response_model_exclude_unset=response_model_exclude_unset,
1904
+ response_model_exclude_defaults=response_model_exclude_defaults,
1905
+ response_model_exclude_none=response_model_exclude_none,
1906
+ include_in_schema=include_in_schema,
1907
+ response_class=response_class,
1908
+ name=name,
1909
+ callbacks=callbacks,
1910
+ openapi_extra=openapi_extra,
1911
+ generate_unique_id_function=generate_unique_id_function,
1912
+ )
1913
+
1914
+ def put(
1915
+ self,
1916
+ path: Annotated[
1917
+ str,
1918
+ Doc(
1919
+ """
1920
+ The URL path to be used for this *path operation*.
1921
+
1922
+ For example, in `http://example.com/items`, the path is `/items`.
1923
+ """
1924
+ ),
1925
+ ],
1926
+ *,
1927
+ response_model: Annotated[
1928
+ Any,
1929
+ Doc(
1930
+ """
1931
+ The type to use for the response.
1932
+
1933
+ It could be any valid Pydantic *field* type. So, it doesn't have to
1934
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
1935
+ etc.
1936
+
1937
+ It will be used for:
1938
+
1939
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
1940
+ show it as the response (JSON Schema).
1941
+ * Serialization: you could return an arbitrary object and the
1942
+ `response_model` would be used to serialize that object into the
1943
+ corresponding JSON.
1944
+ * Filtering: the JSON sent to the client will only contain the data
1945
+ (fields) defined in the `response_model`. If you returned an object
1946
+ that contains an attribute `password` but the `response_model` does
1947
+ not include that field, the JSON sent to the client would not have
1948
+ that `password`.
1949
+ * Validation: whatever you return will be serialized with the
1950
+ `response_model`, converting any data as necessary to generate the
1951
+ corresponding JSON. But if the data in the object returned is not
1952
+ valid, that would mean a violation of the contract with the client,
1953
+ so it's an error from the API developer. So, FastAPI will raise an
1954
+ error and return a 500 error code (Internal Server Error).
1955
+
1956
+ Read more about it in the
1957
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
1958
+ """
1959
+ ),
1960
+ ] = Default(None),
1961
+ status_code: Annotated[
1962
+ Optional[int],
1963
+ Doc(
1964
+ """
1965
+ The default status code to be used for the response.
1966
+
1967
+ You could override the status code by returning a response directly.
1968
+
1969
+ Read more about it in the
1970
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
1971
+ """
1972
+ ),
1973
+ ] = None,
1974
+ tags: Annotated[
1975
+ Optional[list[Union[str, Enum]]],
1976
+ Doc(
1977
+ """
1978
+ A list of tags to be applied to the *path operation*.
1979
+
1980
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
1981
+
1982
+ Read more about it in the
1983
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
1984
+ """
1985
+ ),
1986
+ ] = None,
1987
+ dependencies: Annotated[
1988
+ Optional[Sequence[Depends]],
1989
+ Doc(
1990
+ """
1991
+ A list of dependencies (using `Depends()`) to be applied to the
1992
+ *path operation*.
1993
+
1994
+ Read more about it in the
1995
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
1996
+ """
1997
+ ),
1998
+ ] = None,
1999
+ summary: Annotated[
2000
+ Optional[str],
2001
+ Doc(
2002
+ """
2003
+ A summary for the *path operation*.
2004
+
2005
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2006
+
2007
+ Read more about it in the
2008
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
2009
+ """
2010
+ ),
2011
+ ] = None,
2012
+ description: Annotated[
2013
+ Optional[str],
2014
+ Doc(
2015
+ """
2016
+ A description for the *path operation*.
2017
+
2018
+ If not provided, it will be extracted automatically from the docstring
2019
+ of the *path operation function*.
2020
+
2021
+ It can contain Markdown.
2022
+
2023
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2024
+
2025
+ Read more about it in the
2026
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
2027
+ """
2028
+ ),
2029
+ ] = None,
2030
+ response_description: Annotated[
2031
+ str,
2032
+ Doc(
2033
+ """
2034
+ The description for the default response.
2035
+
2036
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2037
+ """
2038
+ ),
2039
+ ] = "Successful Response",
2040
+ responses: Annotated[
2041
+ Optional[dict[Union[int, str], dict[str, Any]]],
2042
+ Doc(
2043
+ """
2044
+ Additional responses that could be returned by this *path operation*.
2045
+
2046
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2047
+ """
2048
+ ),
2049
+ ] = None,
2050
+ deprecated: Annotated[
2051
+ Optional[bool],
2052
+ Doc(
2053
+ """
2054
+ Mark this *path operation* as deprecated.
2055
+
2056
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2057
+ """
2058
+ ),
2059
+ ] = None,
2060
+ operation_id: Annotated[
2061
+ Optional[str],
2062
+ Doc(
2063
+ """
2064
+ Custom operation ID to be used by this *path operation*.
2065
+
2066
+ By default, it is generated automatically.
2067
+
2068
+ If you provide a custom operation ID, you need to make sure it is
2069
+ unique for the whole API.
2070
+
2071
+ You can customize the
2072
+ operation ID generation with the parameter
2073
+ `generate_unique_id_function` in the `FastAPI` class.
2074
+
2075
+ Read more about it in the
2076
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
2077
+ """
2078
+ ),
2079
+ ] = None,
2080
+ response_model_include: Annotated[
2081
+ Optional[IncEx],
2082
+ Doc(
2083
+ """
2084
+ Configuration passed to Pydantic to include only certain fields in the
2085
+ response data.
2086
+
2087
+ Read more about it in the
2088
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2089
+ """
2090
+ ),
2091
+ ] = None,
2092
+ response_model_exclude: Annotated[
2093
+ Optional[IncEx],
2094
+ Doc(
2095
+ """
2096
+ Configuration passed to Pydantic to exclude certain fields in the
2097
+ response data.
2098
+
2099
+ Read more about it in the
2100
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2101
+ """
2102
+ ),
2103
+ ] = None,
2104
+ response_model_by_alias: Annotated[
2105
+ bool,
2106
+ Doc(
2107
+ """
2108
+ Configuration passed to Pydantic to define if the response model
2109
+ should be serialized by alias when an alias is used.
2110
+
2111
+ Read more about it in the
2112
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2113
+ """
2114
+ ),
2115
+ ] = True,
2116
+ response_model_exclude_unset: Annotated[
2117
+ bool,
2118
+ Doc(
2119
+ """
2120
+ Configuration passed to Pydantic to define if the response data
2121
+ should have all the fields, including the ones that were not set and
2122
+ have their default values. This is different from
2123
+ `response_model_exclude_defaults` in that if the fields are set,
2124
+ they will be included in the response, even if the value is the same
2125
+ as the default.
2126
+
2127
+ When `True`, default values are omitted from the response.
2128
+
2129
+ Read more about it in the
2130
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
2131
+ """
2132
+ ),
2133
+ ] = False,
2134
+ response_model_exclude_defaults: Annotated[
2135
+ bool,
2136
+ Doc(
2137
+ """
2138
+ Configuration passed to Pydantic to define if the response data
2139
+ should have all the fields, including the ones that have the same value
2140
+ as the default. This is different from `response_model_exclude_unset`
2141
+ in that if the fields are set but contain the same default values,
2142
+ they will be excluded from the response.
2143
+
2144
+ When `True`, default values are omitted from the response.
2145
+
2146
+ Read more about it in the
2147
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
2148
+ """
2149
+ ),
2150
+ ] = False,
2151
+ response_model_exclude_none: Annotated[
2152
+ bool,
2153
+ Doc(
2154
+ """
2155
+ Configuration passed to Pydantic to define if the response data should
2156
+ exclude fields set to `None`.
2157
+
2158
+ This is much simpler (less smart) than `response_model_exclude_unset`
2159
+ and `response_model_exclude_defaults`. You probably want to use one of
2160
+ those two instead of this one, as those allow returning `None` values
2161
+ when it makes sense.
2162
+
2163
+ Read more about it in the
2164
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
2165
+ """
2166
+ ),
2167
+ ] = False,
2168
+ include_in_schema: Annotated[
2169
+ bool,
2170
+ Doc(
2171
+ """
2172
+ Include this *path operation* in the generated OpenAPI schema.
2173
+
2174
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
2175
+
2176
+ Read more about it in the
2177
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
2178
+ """
2179
+ ),
2180
+ ] = True,
2181
+ response_class: Annotated[
2182
+ type[Response],
2183
+ Doc(
2184
+ """
2185
+ Response class to be used for this *path operation*.
2186
+
2187
+ This will not be used if you return a response directly.
2188
+
2189
+ Read more about it in the
2190
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
2191
+ """
2192
+ ),
2193
+ ] = Default(JSONResponse),
2194
+ name: Annotated[
2195
+ Optional[str],
2196
+ Doc(
2197
+ """
2198
+ Name for this *path operation*. Only used internally.
2199
+ """
2200
+ ),
2201
+ ] = None,
2202
+ callbacks: Annotated[
2203
+ Optional[list[BaseRoute]],
2204
+ Doc(
2205
+ """
2206
+ List of *path operations* that will be used as OpenAPI callbacks.
2207
+
2208
+ This is only for OpenAPI documentation, the callbacks won't be used
2209
+ directly.
2210
+
2211
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2212
+
2213
+ Read more about it in the
2214
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
2215
+ """
2216
+ ),
2217
+ ] = None,
2218
+ openapi_extra: Annotated[
2219
+ Optional[dict[str, Any]],
2220
+ Doc(
2221
+ """
2222
+ Extra metadata to be included in the OpenAPI schema for this *path
2223
+ operation*.
2224
+
2225
+ Read more about it in the
2226
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
2227
+ """
2228
+ ),
2229
+ ] = None,
2230
+ generate_unique_id_function: Annotated[
2231
+ Callable[[routing.APIRoute], str],
2232
+ Doc(
2233
+ """
2234
+ Customize the function used to generate unique IDs for the *path
2235
+ operations* shown in the generated OpenAPI.
2236
+
2237
+ This is particularly useful when automatically generating clients or
2238
+ SDKs for your API.
2239
+
2240
+ Read more about it in the
2241
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
2242
+ """
2243
+ ),
2244
+ ] = Default(generate_unique_id),
2245
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
2246
+ """
2247
+ Add a *path operation* using an HTTP PUT operation.
2248
+
2249
+ ## Example
2250
+
2251
+ ```python
2252
+ from fastapi import FastAPI
2253
+ from pydantic import BaseModel
2254
+
2255
+ class Item(BaseModel):
2256
+ name: str
2257
+ description: str | None = None
2258
+
2259
+ app = FastAPI()
2260
+
2261
+ @app.put("/items/{item_id}")
2262
+ def replace_item(item_id: str, item: Item):
2263
+ return {"message": "Item replaced", "id": item_id}
2264
+ ```
2265
+ """
2266
+ return self.router.put(
2267
+ path,
2268
+ response_model=response_model,
2269
+ status_code=status_code,
2270
+ tags=tags,
2271
+ dependencies=dependencies,
2272
+ summary=summary,
2273
+ description=description,
2274
+ response_description=response_description,
2275
+ responses=responses,
2276
+ deprecated=deprecated,
2277
+ operation_id=operation_id,
2278
+ response_model_include=response_model_include,
2279
+ response_model_exclude=response_model_exclude,
2280
+ response_model_by_alias=response_model_by_alias,
2281
+ response_model_exclude_unset=response_model_exclude_unset,
2282
+ response_model_exclude_defaults=response_model_exclude_defaults,
2283
+ response_model_exclude_none=response_model_exclude_none,
2284
+ include_in_schema=include_in_schema,
2285
+ response_class=response_class,
2286
+ name=name,
2287
+ callbacks=callbacks,
2288
+ openapi_extra=openapi_extra,
2289
+ generate_unique_id_function=generate_unique_id_function,
2290
+ )
2291
+
2292
+ def post(
2293
+ self,
2294
+ path: Annotated[
2295
+ str,
2296
+ Doc(
2297
+ """
2298
+ The URL path to be used for this *path operation*.
2299
+
2300
+ For example, in `http://example.com/items`, the path is `/items`.
2301
+ """
2302
+ ),
2303
+ ],
2304
+ *,
2305
+ response_model: Annotated[
2306
+ Any,
2307
+ Doc(
2308
+ """
2309
+ The type to use for the response.
2310
+
2311
+ It could be any valid Pydantic *field* type. So, it doesn't have to
2312
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
2313
+ etc.
2314
+
2315
+ It will be used for:
2316
+
2317
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
2318
+ show it as the response (JSON Schema).
2319
+ * Serialization: you could return an arbitrary object and the
2320
+ `response_model` would be used to serialize that object into the
2321
+ corresponding JSON.
2322
+ * Filtering: the JSON sent to the client will only contain the data
2323
+ (fields) defined in the `response_model`. If you returned an object
2324
+ that contains an attribute `password` but the `response_model` does
2325
+ not include that field, the JSON sent to the client would not have
2326
+ that `password`.
2327
+ * Validation: whatever you return will be serialized with the
2328
+ `response_model`, converting any data as necessary to generate the
2329
+ corresponding JSON. But if the data in the object returned is not
2330
+ valid, that would mean a violation of the contract with the client,
2331
+ so it's an error from the API developer. So, FastAPI will raise an
2332
+ error and return a 500 error code (Internal Server Error).
2333
+
2334
+ Read more about it in the
2335
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
2336
+ """
2337
+ ),
2338
+ ] = Default(None),
2339
+ status_code: Annotated[
2340
+ Optional[int],
2341
+ Doc(
2342
+ """
2343
+ The default status code to be used for the response.
2344
+
2345
+ You could override the status code by returning a response directly.
2346
+
2347
+ Read more about it in the
2348
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
2349
+ """
2350
+ ),
2351
+ ] = None,
2352
+ tags: Annotated[
2353
+ Optional[list[Union[str, Enum]]],
2354
+ Doc(
2355
+ """
2356
+ A list of tags to be applied to the *path operation*.
2357
+
2358
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2359
+
2360
+ Read more about it in the
2361
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
2362
+ """
2363
+ ),
2364
+ ] = None,
2365
+ dependencies: Annotated[
2366
+ Optional[Sequence[Depends]],
2367
+ Doc(
2368
+ """
2369
+ A list of dependencies (using `Depends()`) to be applied to the
2370
+ *path operation*.
2371
+
2372
+ Read more about it in the
2373
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
2374
+ """
2375
+ ),
2376
+ ] = None,
2377
+ summary: Annotated[
2378
+ Optional[str],
2379
+ Doc(
2380
+ """
2381
+ A summary for the *path operation*.
2382
+
2383
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2384
+
2385
+ Read more about it in the
2386
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
2387
+ """
2388
+ ),
2389
+ ] = None,
2390
+ description: Annotated[
2391
+ Optional[str],
2392
+ Doc(
2393
+ """
2394
+ A description for the *path operation*.
2395
+
2396
+ If not provided, it will be extracted automatically from the docstring
2397
+ of the *path operation function*.
2398
+
2399
+ It can contain Markdown.
2400
+
2401
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2402
+
2403
+ Read more about it in the
2404
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
2405
+ """
2406
+ ),
2407
+ ] = None,
2408
+ response_description: Annotated[
2409
+ str,
2410
+ Doc(
2411
+ """
2412
+ The description for the default response.
2413
+
2414
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2415
+ """
2416
+ ),
2417
+ ] = "Successful Response",
2418
+ responses: Annotated[
2419
+ Optional[dict[Union[int, str], dict[str, Any]]],
2420
+ Doc(
2421
+ """
2422
+ Additional responses that could be returned by this *path operation*.
2423
+
2424
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2425
+ """
2426
+ ),
2427
+ ] = None,
2428
+ deprecated: Annotated[
2429
+ Optional[bool],
2430
+ Doc(
2431
+ """
2432
+ Mark this *path operation* as deprecated.
2433
+
2434
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2435
+ """
2436
+ ),
2437
+ ] = None,
2438
+ operation_id: Annotated[
2439
+ Optional[str],
2440
+ Doc(
2441
+ """
2442
+ Custom operation ID to be used by this *path operation*.
2443
+
2444
+ By default, it is generated automatically.
2445
+
2446
+ If you provide a custom operation ID, you need to make sure it is
2447
+ unique for the whole API.
2448
+
2449
+ You can customize the
2450
+ operation ID generation with the parameter
2451
+ `generate_unique_id_function` in the `FastAPI` class.
2452
+
2453
+ Read more about it in the
2454
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
2455
+ """
2456
+ ),
2457
+ ] = None,
2458
+ response_model_include: Annotated[
2459
+ Optional[IncEx],
2460
+ Doc(
2461
+ """
2462
+ Configuration passed to Pydantic to include only certain fields in the
2463
+ response data.
2464
+
2465
+ Read more about it in the
2466
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2467
+ """
2468
+ ),
2469
+ ] = None,
2470
+ response_model_exclude: Annotated[
2471
+ Optional[IncEx],
2472
+ Doc(
2473
+ """
2474
+ Configuration passed to Pydantic to exclude certain fields in the
2475
+ response data.
2476
+
2477
+ Read more about it in the
2478
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2479
+ """
2480
+ ),
2481
+ ] = None,
2482
+ response_model_by_alias: Annotated[
2483
+ bool,
2484
+ Doc(
2485
+ """
2486
+ Configuration passed to Pydantic to define if the response model
2487
+ should be serialized by alias when an alias is used.
2488
+
2489
+ Read more about it in the
2490
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2491
+ """
2492
+ ),
2493
+ ] = True,
2494
+ response_model_exclude_unset: Annotated[
2495
+ bool,
2496
+ Doc(
2497
+ """
2498
+ Configuration passed to Pydantic to define if the response data
2499
+ should have all the fields, including the ones that were not set and
2500
+ have their default values. This is different from
2501
+ `response_model_exclude_defaults` in that if the fields are set,
2502
+ they will be included in the response, even if the value is the same
2503
+ as the default.
2504
+
2505
+ When `True`, default values are omitted from the response.
2506
+
2507
+ Read more about it in the
2508
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
2509
+ """
2510
+ ),
2511
+ ] = False,
2512
+ response_model_exclude_defaults: Annotated[
2513
+ bool,
2514
+ Doc(
2515
+ """
2516
+ Configuration passed to Pydantic to define if the response data
2517
+ should have all the fields, including the ones that have the same value
2518
+ as the default. This is different from `response_model_exclude_unset`
2519
+ in that if the fields are set but contain the same default values,
2520
+ they will be excluded from the response.
2521
+
2522
+ When `True`, default values are omitted from the response.
2523
+
2524
+ Read more about it in the
2525
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
2526
+ """
2527
+ ),
2528
+ ] = False,
2529
+ response_model_exclude_none: Annotated[
2530
+ bool,
2531
+ Doc(
2532
+ """
2533
+ Configuration passed to Pydantic to define if the response data should
2534
+ exclude fields set to `None`.
2535
+
2536
+ This is much simpler (less smart) than `response_model_exclude_unset`
2537
+ and `response_model_exclude_defaults`. You probably want to use one of
2538
+ those two instead of this one, as those allow returning `None` values
2539
+ when it makes sense.
2540
+
2541
+ Read more about it in the
2542
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
2543
+ """
2544
+ ),
2545
+ ] = False,
2546
+ include_in_schema: Annotated[
2547
+ bool,
2548
+ Doc(
2549
+ """
2550
+ Include this *path operation* in the generated OpenAPI schema.
2551
+
2552
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
2553
+
2554
+ Read more about it in the
2555
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
2556
+ """
2557
+ ),
2558
+ ] = True,
2559
+ response_class: Annotated[
2560
+ type[Response],
2561
+ Doc(
2562
+ """
2563
+ Response class to be used for this *path operation*.
2564
+
2565
+ This will not be used if you return a response directly.
2566
+
2567
+ Read more about it in the
2568
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
2569
+ """
2570
+ ),
2571
+ ] = Default(JSONResponse),
2572
+ name: Annotated[
2573
+ Optional[str],
2574
+ Doc(
2575
+ """
2576
+ Name for this *path operation*. Only used internally.
2577
+ """
2578
+ ),
2579
+ ] = None,
2580
+ callbacks: Annotated[
2581
+ Optional[list[BaseRoute]],
2582
+ Doc(
2583
+ """
2584
+ List of *path operations* that will be used as OpenAPI callbacks.
2585
+
2586
+ This is only for OpenAPI documentation, the callbacks won't be used
2587
+ directly.
2588
+
2589
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2590
+
2591
+ Read more about it in the
2592
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
2593
+ """
2594
+ ),
2595
+ ] = None,
2596
+ openapi_extra: Annotated[
2597
+ Optional[dict[str, Any]],
2598
+ Doc(
2599
+ """
2600
+ Extra metadata to be included in the OpenAPI schema for this *path
2601
+ operation*.
2602
+
2603
+ Read more about it in the
2604
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
2605
+ """
2606
+ ),
2607
+ ] = None,
2608
+ generate_unique_id_function: Annotated[
2609
+ Callable[[routing.APIRoute], str],
2610
+ Doc(
2611
+ """
2612
+ Customize the function used to generate unique IDs for the *path
2613
+ operations* shown in the generated OpenAPI.
2614
+
2615
+ This is particularly useful when automatically generating clients or
2616
+ SDKs for your API.
2617
+
2618
+ Read more about it in the
2619
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
2620
+ """
2621
+ ),
2622
+ ] = Default(generate_unique_id),
2623
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
2624
+ """
2625
+ Add a *path operation* using an HTTP POST operation.
2626
+
2627
+ ## Example
2628
+
2629
+ ```python
2630
+ from fastapi import FastAPI
2631
+ from pydantic import BaseModel
2632
+
2633
+ class Item(BaseModel):
2634
+ name: str
2635
+ description: str | None = None
2636
+
2637
+ app = FastAPI()
2638
+
2639
+ @app.post("/items/")
2640
+ def create_item(item: Item):
2641
+ return {"message": "Item created"}
2642
+ ```
2643
+ """
2644
+ return self.router.post(
2645
+ path,
2646
+ response_model=response_model,
2647
+ status_code=status_code,
2648
+ tags=tags,
2649
+ dependencies=dependencies,
2650
+ summary=summary,
2651
+ description=description,
2652
+ response_description=response_description,
2653
+ responses=responses,
2654
+ deprecated=deprecated,
2655
+ operation_id=operation_id,
2656
+ response_model_include=response_model_include,
2657
+ response_model_exclude=response_model_exclude,
2658
+ response_model_by_alias=response_model_by_alias,
2659
+ response_model_exclude_unset=response_model_exclude_unset,
2660
+ response_model_exclude_defaults=response_model_exclude_defaults,
2661
+ response_model_exclude_none=response_model_exclude_none,
2662
+ include_in_schema=include_in_schema,
2663
+ response_class=response_class,
2664
+ name=name,
2665
+ callbacks=callbacks,
2666
+ openapi_extra=openapi_extra,
2667
+ generate_unique_id_function=generate_unique_id_function,
2668
+ )
2669
+
2670
+ def delete(
2671
+ self,
2672
+ path: Annotated[
2673
+ str,
2674
+ Doc(
2675
+ """
2676
+ The URL path to be used for this *path operation*.
2677
+
2678
+ For example, in `http://example.com/items`, the path is `/items`.
2679
+ """
2680
+ ),
2681
+ ],
2682
+ *,
2683
+ response_model: Annotated[
2684
+ Any,
2685
+ Doc(
2686
+ """
2687
+ The type to use for the response.
2688
+
2689
+ It could be any valid Pydantic *field* type. So, it doesn't have to
2690
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
2691
+ etc.
2692
+
2693
+ It will be used for:
2694
+
2695
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
2696
+ show it as the response (JSON Schema).
2697
+ * Serialization: you could return an arbitrary object and the
2698
+ `response_model` would be used to serialize that object into the
2699
+ corresponding JSON.
2700
+ * Filtering: the JSON sent to the client will only contain the data
2701
+ (fields) defined in the `response_model`. If you returned an object
2702
+ that contains an attribute `password` but the `response_model` does
2703
+ not include that field, the JSON sent to the client would not have
2704
+ that `password`.
2705
+ * Validation: whatever you return will be serialized with the
2706
+ `response_model`, converting any data as necessary to generate the
2707
+ corresponding JSON. But if the data in the object returned is not
2708
+ valid, that would mean a violation of the contract with the client,
2709
+ so it's an error from the API developer. So, FastAPI will raise an
2710
+ error and return a 500 error code (Internal Server Error).
2711
+
2712
+ Read more about it in the
2713
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
2714
+ """
2715
+ ),
2716
+ ] = Default(None),
2717
+ status_code: Annotated[
2718
+ Optional[int],
2719
+ Doc(
2720
+ """
2721
+ The default status code to be used for the response.
2722
+
2723
+ You could override the status code by returning a response directly.
2724
+
2725
+ Read more about it in the
2726
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
2727
+ """
2728
+ ),
2729
+ ] = None,
2730
+ tags: Annotated[
2731
+ Optional[list[Union[str, Enum]]],
2732
+ Doc(
2733
+ """
2734
+ A list of tags to be applied to the *path operation*.
2735
+
2736
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2737
+
2738
+ Read more about it in the
2739
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
2740
+ """
2741
+ ),
2742
+ ] = None,
2743
+ dependencies: Annotated[
2744
+ Optional[Sequence[Depends]],
2745
+ Doc(
2746
+ """
2747
+ A list of dependencies (using `Depends()`) to be applied to the
2748
+ *path operation*.
2749
+
2750
+ Read more about it in the
2751
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
2752
+ """
2753
+ ),
2754
+ ] = None,
2755
+ summary: Annotated[
2756
+ Optional[str],
2757
+ Doc(
2758
+ """
2759
+ A summary for the *path operation*.
2760
+
2761
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2762
+
2763
+ Read more about it in the
2764
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
2765
+ """
2766
+ ),
2767
+ ] = None,
2768
+ description: Annotated[
2769
+ Optional[str],
2770
+ Doc(
2771
+ """
2772
+ A description for the *path operation*.
2773
+
2774
+ If not provided, it will be extracted automatically from the docstring
2775
+ of the *path operation function*.
2776
+
2777
+ It can contain Markdown.
2778
+
2779
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2780
+
2781
+ Read more about it in the
2782
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
2783
+ """
2784
+ ),
2785
+ ] = None,
2786
+ response_description: Annotated[
2787
+ str,
2788
+ Doc(
2789
+ """
2790
+ The description for the default response.
2791
+
2792
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2793
+ """
2794
+ ),
2795
+ ] = "Successful Response",
2796
+ responses: Annotated[
2797
+ Optional[dict[Union[int, str], dict[str, Any]]],
2798
+ Doc(
2799
+ """
2800
+ Additional responses that could be returned by this *path operation*.
2801
+
2802
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2803
+ """
2804
+ ),
2805
+ ] = None,
2806
+ deprecated: Annotated[
2807
+ Optional[bool],
2808
+ Doc(
2809
+ """
2810
+ Mark this *path operation* as deprecated.
2811
+
2812
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2813
+ """
2814
+ ),
2815
+ ] = None,
2816
+ operation_id: Annotated[
2817
+ Optional[str],
2818
+ Doc(
2819
+ """
2820
+ Custom operation ID to be used by this *path operation*.
2821
+
2822
+ By default, it is generated automatically.
2823
+
2824
+ If you provide a custom operation ID, you need to make sure it is
2825
+ unique for the whole API.
2826
+
2827
+ You can customize the
2828
+ operation ID generation with the parameter
2829
+ `generate_unique_id_function` in the `FastAPI` class.
2830
+
2831
+ Read more about it in the
2832
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
2833
+ """
2834
+ ),
2835
+ ] = None,
2836
+ response_model_include: Annotated[
2837
+ Optional[IncEx],
2838
+ Doc(
2839
+ """
2840
+ Configuration passed to Pydantic to include only certain fields in the
2841
+ response data.
2842
+
2843
+ Read more about it in the
2844
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2845
+ """
2846
+ ),
2847
+ ] = None,
2848
+ response_model_exclude: Annotated[
2849
+ Optional[IncEx],
2850
+ Doc(
2851
+ """
2852
+ Configuration passed to Pydantic to exclude certain fields in the
2853
+ response data.
2854
+
2855
+ Read more about it in the
2856
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2857
+ """
2858
+ ),
2859
+ ] = None,
2860
+ response_model_by_alias: Annotated[
2861
+ bool,
2862
+ Doc(
2863
+ """
2864
+ Configuration passed to Pydantic to define if the response model
2865
+ should be serialized by alias when an alias is used.
2866
+
2867
+ Read more about it in the
2868
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
2869
+ """
2870
+ ),
2871
+ ] = True,
2872
+ response_model_exclude_unset: Annotated[
2873
+ bool,
2874
+ Doc(
2875
+ """
2876
+ Configuration passed to Pydantic to define if the response data
2877
+ should have all the fields, including the ones that were not set and
2878
+ have their default values. This is different from
2879
+ `response_model_exclude_defaults` in that if the fields are set,
2880
+ they will be included in the response, even if the value is the same
2881
+ as the default.
2882
+
2883
+ When `True`, default values are omitted from the response.
2884
+
2885
+ Read more about it in the
2886
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
2887
+ """
2888
+ ),
2889
+ ] = False,
2890
+ response_model_exclude_defaults: Annotated[
2891
+ bool,
2892
+ Doc(
2893
+ """
2894
+ Configuration passed to Pydantic to define if the response data
2895
+ should have all the fields, including the ones that have the same value
2896
+ as the default. This is different from `response_model_exclude_unset`
2897
+ in that if the fields are set but contain the same default values,
2898
+ they will be excluded from the response.
2899
+
2900
+ When `True`, default values are omitted from the response.
2901
+
2902
+ Read more about it in the
2903
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
2904
+ """
2905
+ ),
2906
+ ] = False,
2907
+ response_model_exclude_none: Annotated[
2908
+ bool,
2909
+ Doc(
2910
+ """
2911
+ Configuration passed to Pydantic to define if the response data should
2912
+ exclude fields set to `None`.
2913
+
2914
+ This is much simpler (less smart) than `response_model_exclude_unset`
2915
+ and `response_model_exclude_defaults`. You probably want to use one of
2916
+ those two instead of this one, as those allow returning `None` values
2917
+ when it makes sense.
2918
+
2919
+ Read more about it in the
2920
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
2921
+ """
2922
+ ),
2923
+ ] = False,
2924
+ include_in_schema: Annotated[
2925
+ bool,
2926
+ Doc(
2927
+ """
2928
+ Include this *path operation* in the generated OpenAPI schema.
2929
+
2930
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
2931
+
2932
+ Read more about it in the
2933
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
2934
+ """
2935
+ ),
2936
+ ] = True,
2937
+ response_class: Annotated[
2938
+ type[Response],
2939
+ Doc(
2940
+ """
2941
+ Response class to be used for this *path operation*.
2942
+
2943
+ This will not be used if you return a response directly.
2944
+
2945
+ Read more about it in the
2946
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
2947
+ """
2948
+ ),
2949
+ ] = Default(JSONResponse),
2950
+ name: Annotated[
2951
+ Optional[str],
2952
+ Doc(
2953
+ """
2954
+ Name for this *path operation*. Only used internally.
2955
+ """
2956
+ ),
2957
+ ] = None,
2958
+ callbacks: Annotated[
2959
+ Optional[list[BaseRoute]],
2960
+ Doc(
2961
+ """
2962
+ List of *path operations* that will be used as OpenAPI callbacks.
2963
+
2964
+ This is only for OpenAPI documentation, the callbacks won't be used
2965
+ directly.
2966
+
2967
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
2968
+
2969
+ Read more about it in the
2970
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
2971
+ """
2972
+ ),
2973
+ ] = None,
2974
+ openapi_extra: Annotated[
2975
+ Optional[dict[str, Any]],
2976
+ Doc(
2977
+ """
2978
+ Extra metadata to be included in the OpenAPI schema for this *path
2979
+ operation*.
2980
+
2981
+ Read more about it in the
2982
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
2983
+ """
2984
+ ),
2985
+ ] = None,
2986
+ generate_unique_id_function: Annotated[
2987
+ Callable[[routing.APIRoute], str],
2988
+ Doc(
2989
+ """
2990
+ Customize the function used to generate unique IDs for the *path
2991
+ operations* shown in the generated OpenAPI.
2992
+
2993
+ This is particularly useful when automatically generating clients or
2994
+ SDKs for your API.
2995
+
2996
+ Read more about it in the
2997
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
2998
+ """
2999
+ ),
3000
+ ] = Default(generate_unique_id),
3001
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
3002
+ """
3003
+ Add a *path operation* using an HTTP DELETE operation.
3004
+
3005
+ ## Example
3006
+
3007
+ ```python
3008
+ from fastapi import FastAPI
3009
+
3010
+ app = FastAPI()
3011
+
3012
+ @app.delete("/items/{item_id}")
3013
+ def delete_item(item_id: str):
3014
+ return {"message": "Item deleted"}
3015
+ ```
3016
+ """
3017
+ return self.router.delete(
3018
+ path,
3019
+ response_model=response_model,
3020
+ status_code=status_code,
3021
+ tags=tags,
3022
+ dependencies=dependencies,
3023
+ summary=summary,
3024
+ description=description,
3025
+ response_description=response_description,
3026
+ responses=responses,
3027
+ deprecated=deprecated,
3028
+ operation_id=operation_id,
3029
+ response_model_include=response_model_include,
3030
+ response_model_exclude=response_model_exclude,
3031
+ response_model_by_alias=response_model_by_alias,
3032
+ response_model_exclude_unset=response_model_exclude_unset,
3033
+ response_model_exclude_defaults=response_model_exclude_defaults,
3034
+ response_model_exclude_none=response_model_exclude_none,
3035
+ include_in_schema=include_in_schema,
3036
+ response_class=response_class,
3037
+ name=name,
3038
+ callbacks=callbacks,
3039
+ openapi_extra=openapi_extra,
3040
+ generate_unique_id_function=generate_unique_id_function,
3041
+ )
3042
+
3043
+ def options(
3044
+ self,
3045
+ path: Annotated[
3046
+ str,
3047
+ Doc(
3048
+ """
3049
+ The URL path to be used for this *path operation*.
3050
+
3051
+ For example, in `http://example.com/items`, the path is `/items`.
3052
+ """
3053
+ ),
3054
+ ],
3055
+ *,
3056
+ response_model: Annotated[
3057
+ Any,
3058
+ Doc(
3059
+ """
3060
+ The type to use for the response.
3061
+
3062
+ It could be any valid Pydantic *field* type. So, it doesn't have to
3063
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
3064
+ etc.
3065
+
3066
+ It will be used for:
3067
+
3068
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
3069
+ show it as the response (JSON Schema).
3070
+ * Serialization: you could return an arbitrary object and the
3071
+ `response_model` would be used to serialize that object into the
3072
+ corresponding JSON.
3073
+ * Filtering: the JSON sent to the client will only contain the data
3074
+ (fields) defined in the `response_model`. If you returned an object
3075
+ that contains an attribute `password` but the `response_model` does
3076
+ not include that field, the JSON sent to the client would not have
3077
+ that `password`.
3078
+ * Validation: whatever you return will be serialized with the
3079
+ `response_model`, converting any data as necessary to generate the
3080
+ corresponding JSON. But if the data in the object returned is not
3081
+ valid, that would mean a violation of the contract with the client,
3082
+ so it's an error from the API developer. So, FastAPI will raise an
3083
+ error and return a 500 error code (Internal Server Error).
3084
+
3085
+ Read more about it in the
3086
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
3087
+ """
3088
+ ),
3089
+ ] = Default(None),
3090
+ status_code: Annotated[
3091
+ Optional[int],
3092
+ Doc(
3093
+ """
3094
+ The default status code to be used for the response.
3095
+
3096
+ You could override the status code by returning a response directly.
3097
+
3098
+ Read more about it in the
3099
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
3100
+ """
3101
+ ),
3102
+ ] = None,
3103
+ tags: Annotated[
3104
+ Optional[list[Union[str, Enum]]],
3105
+ Doc(
3106
+ """
3107
+ A list of tags to be applied to the *path operation*.
3108
+
3109
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3110
+
3111
+ Read more about it in the
3112
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
3113
+ """
3114
+ ),
3115
+ ] = None,
3116
+ dependencies: Annotated[
3117
+ Optional[Sequence[Depends]],
3118
+ Doc(
3119
+ """
3120
+ A list of dependencies (using `Depends()`) to be applied to the
3121
+ *path operation*.
3122
+
3123
+ Read more about it in the
3124
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
3125
+ """
3126
+ ),
3127
+ ] = None,
3128
+ summary: Annotated[
3129
+ Optional[str],
3130
+ Doc(
3131
+ """
3132
+ A summary for the *path operation*.
3133
+
3134
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3135
+
3136
+ Read more about it in the
3137
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
3138
+ """
3139
+ ),
3140
+ ] = None,
3141
+ description: Annotated[
3142
+ Optional[str],
3143
+ Doc(
3144
+ """
3145
+ A description for the *path operation*.
3146
+
3147
+ If not provided, it will be extracted automatically from the docstring
3148
+ of the *path operation function*.
3149
+
3150
+ It can contain Markdown.
3151
+
3152
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3153
+
3154
+ Read more about it in the
3155
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
3156
+ """
3157
+ ),
3158
+ ] = None,
3159
+ response_description: Annotated[
3160
+ str,
3161
+ Doc(
3162
+ """
3163
+ The description for the default response.
3164
+
3165
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3166
+ """
3167
+ ),
3168
+ ] = "Successful Response",
3169
+ responses: Annotated[
3170
+ Optional[dict[Union[int, str], dict[str, Any]]],
3171
+ Doc(
3172
+ """
3173
+ Additional responses that could be returned by this *path operation*.
3174
+
3175
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3176
+ """
3177
+ ),
3178
+ ] = None,
3179
+ deprecated: Annotated[
3180
+ Optional[bool],
3181
+ Doc(
3182
+ """
3183
+ Mark this *path operation* as deprecated.
3184
+
3185
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3186
+ """
3187
+ ),
3188
+ ] = None,
3189
+ operation_id: Annotated[
3190
+ Optional[str],
3191
+ Doc(
3192
+ """
3193
+ Custom operation ID to be used by this *path operation*.
3194
+
3195
+ By default, it is generated automatically.
3196
+
3197
+ If you provide a custom operation ID, you need to make sure it is
3198
+ unique for the whole API.
3199
+
3200
+ You can customize the
3201
+ operation ID generation with the parameter
3202
+ `generate_unique_id_function` in the `FastAPI` class.
3203
+
3204
+ Read more about it in the
3205
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
3206
+ """
3207
+ ),
3208
+ ] = None,
3209
+ response_model_include: Annotated[
3210
+ Optional[IncEx],
3211
+ Doc(
3212
+ """
3213
+ Configuration passed to Pydantic to include only certain fields in the
3214
+ response data.
3215
+
3216
+ Read more about it in the
3217
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3218
+ """
3219
+ ),
3220
+ ] = None,
3221
+ response_model_exclude: Annotated[
3222
+ Optional[IncEx],
3223
+ Doc(
3224
+ """
3225
+ Configuration passed to Pydantic to exclude certain fields in the
3226
+ response data.
3227
+
3228
+ Read more about it in the
3229
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3230
+ """
3231
+ ),
3232
+ ] = None,
3233
+ response_model_by_alias: Annotated[
3234
+ bool,
3235
+ Doc(
3236
+ """
3237
+ Configuration passed to Pydantic to define if the response model
3238
+ should be serialized by alias when an alias is used.
3239
+
3240
+ Read more about it in the
3241
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3242
+ """
3243
+ ),
3244
+ ] = True,
3245
+ response_model_exclude_unset: Annotated[
3246
+ bool,
3247
+ Doc(
3248
+ """
3249
+ Configuration passed to Pydantic to define if the response data
3250
+ should have all the fields, including the ones that were not set and
3251
+ have their default values. This is different from
3252
+ `response_model_exclude_defaults` in that if the fields are set,
3253
+ they will be included in the response, even if the value is the same
3254
+ as the default.
3255
+
3256
+ When `True`, default values are omitted from the response.
3257
+
3258
+ Read more about it in the
3259
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
3260
+ """
3261
+ ),
3262
+ ] = False,
3263
+ response_model_exclude_defaults: Annotated[
3264
+ bool,
3265
+ Doc(
3266
+ """
3267
+ Configuration passed to Pydantic to define if the response data
3268
+ should have all the fields, including the ones that have the same value
3269
+ as the default. This is different from `response_model_exclude_unset`
3270
+ in that if the fields are set but contain the same default values,
3271
+ they will be excluded from the response.
3272
+
3273
+ When `True`, default values are omitted from the response.
3274
+
3275
+ Read more about it in the
3276
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
3277
+ """
3278
+ ),
3279
+ ] = False,
3280
+ response_model_exclude_none: Annotated[
3281
+ bool,
3282
+ Doc(
3283
+ """
3284
+ Configuration passed to Pydantic to define if the response data should
3285
+ exclude fields set to `None`.
3286
+
3287
+ This is much simpler (less smart) than `response_model_exclude_unset`
3288
+ and `response_model_exclude_defaults`. You probably want to use one of
3289
+ those two instead of this one, as those allow returning `None` values
3290
+ when it makes sense.
3291
+
3292
+ Read more about it in the
3293
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
3294
+ """
3295
+ ),
3296
+ ] = False,
3297
+ include_in_schema: Annotated[
3298
+ bool,
3299
+ Doc(
3300
+ """
3301
+ Include this *path operation* in the generated OpenAPI schema.
3302
+
3303
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
3304
+
3305
+ Read more about it in the
3306
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
3307
+ """
3308
+ ),
3309
+ ] = True,
3310
+ response_class: Annotated[
3311
+ type[Response],
3312
+ Doc(
3313
+ """
3314
+ Response class to be used for this *path operation*.
3315
+
3316
+ This will not be used if you return a response directly.
3317
+
3318
+ Read more about it in the
3319
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
3320
+ """
3321
+ ),
3322
+ ] = Default(JSONResponse),
3323
+ name: Annotated[
3324
+ Optional[str],
3325
+ Doc(
3326
+ """
3327
+ Name for this *path operation*. Only used internally.
3328
+ """
3329
+ ),
3330
+ ] = None,
3331
+ callbacks: Annotated[
3332
+ Optional[list[BaseRoute]],
3333
+ Doc(
3334
+ """
3335
+ List of *path operations* that will be used as OpenAPI callbacks.
3336
+
3337
+ This is only for OpenAPI documentation, the callbacks won't be used
3338
+ directly.
3339
+
3340
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3341
+
3342
+ Read more about it in the
3343
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
3344
+ """
3345
+ ),
3346
+ ] = None,
3347
+ openapi_extra: Annotated[
3348
+ Optional[dict[str, Any]],
3349
+ Doc(
3350
+ """
3351
+ Extra metadata to be included in the OpenAPI schema for this *path
3352
+ operation*.
3353
+
3354
+ Read more about it in the
3355
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
3356
+ """
3357
+ ),
3358
+ ] = None,
3359
+ generate_unique_id_function: Annotated[
3360
+ Callable[[routing.APIRoute], str],
3361
+ Doc(
3362
+ """
3363
+ Customize the function used to generate unique IDs for the *path
3364
+ operations* shown in the generated OpenAPI.
3365
+
3366
+ This is particularly useful when automatically generating clients or
3367
+ SDKs for your API.
3368
+
3369
+ Read more about it in the
3370
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
3371
+ """
3372
+ ),
3373
+ ] = Default(generate_unique_id),
3374
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
3375
+ """
3376
+ Add a *path operation* using an HTTP OPTIONS operation.
3377
+
3378
+ ## Example
3379
+
3380
+ ```python
3381
+ from fastapi import FastAPI
3382
+
3383
+ app = FastAPI()
3384
+
3385
+ @app.options("/items/")
3386
+ def get_item_options():
3387
+ return {"additions": ["Aji", "Guacamole"]}
3388
+ ```
3389
+ """
3390
+ return self.router.options(
3391
+ path,
3392
+ response_model=response_model,
3393
+ status_code=status_code,
3394
+ tags=tags,
3395
+ dependencies=dependencies,
3396
+ summary=summary,
3397
+ description=description,
3398
+ response_description=response_description,
3399
+ responses=responses,
3400
+ deprecated=deprecated,
3401
+ operation_id=operation_id,
3402
+ response_model_include=response_model_include,
3403
+ response_model_exclude=response_model_exclude,
3404
+ response_model_by_alias=response_model_by_alias,
3405
+ response_model_exclude_unset=response_model_exclude_unset,
3406
+ response_model_exclude_defaults=response_model_exclude_defaults,
3407
+ response_model_exclude_none=response_model_exclude_none,
3408
+ include_in_schema=include_in_schema,
3409
+ response_class=response_class,
3410
+ name=name,
3411
+ callbacks=callbacks,
3412
+ openapi_extra=openapi_extra,
3413
+ generate_unique_id_function=generate_unique_id_function,
3414
+ )
3415
+
3416
+ def head(
3417
+ self,
3418
+ path: Annotated[
3419
+ str,
3420
+ Doc(
3421
+ """
3422
+ The URL path to be used for this *path operation*.
3423
+
3424
+ For example, in `http://example.com/items`, the path is `/items`.
3425
+ """
3426
+ ),
3427
+ ],
3428
+ *,
3429
+ response_model: Annotated[
3430
+ Any,
3431
+ Doc(
3432
+ """
3433
+ The type to use for the response.
3434
+
3435
+ It could be any valid Pydantic *field* type. So, it doesn't have to
3436
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
3437
+ etc.
3438
+
3439
+ It will be used for:
3440
+
3441
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
3442
+ show it as the response (JSON Schema).
3443
+ * Serialization: you could return an arbitrary object and the
3444
+ `response_model` would be used to serialize that object into the
3445
+ corresponding JSON.
3446
+ * Filtering: the JSON sent to the client will only contain the data
3447
+ (fields) defined in the `response_model`. If you returned an object
3448
+ that contains an attribute `password` but the `response_model` does
3449
+ not include that field, the JSON sent to the client would not have
3450
+ that `password`.
3451
+ * Validation: whatever you return will be serialized with the
3452
+ `response_model`, converting any data as necessary to generate the
3453
+ corresponding JSON. But if the data in the object returned is not
3454
+ valid, that would mean a violation of the contract with the client,
3455
+ so it's an error from the API developer. So, FastAPI will raise an
3456
+ error and return a 500 error code (Internal Server Error).
3457
+
3458
+ Read more about it in the
3459
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
3460
+ """
3461
+ ),
3462
+ ] = Default(None),
3463
+ status_code: Annotated[
3464
+ Optional[int],
3465
+ Doc(
3466
+ """
3467
+ The default status code to be used for the response.
3468
+
3469
+ You could override the status code by returning a response directly.
3470
+
3471
+ Read more about it in the
3472
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
3473
+ """
3474
+ ),
3475
+ ] = None,
3476
+ tags: Annotated[
3477
+ Optional[list[Union[str, Enum]]],
3478
+ Doc(
3479
+ """
3480
+ A list of tags to be applied to the *path operation*.
3481
+
3482
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3483
+
3484
+ Read more about it in the
3485
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
3486
+ """
3487
+ ),
3488
+ ] = None,
3489
+ dependencies: Annotated[
3490
+ Optional[Sequence[Depends]],
3491
+ Doc(
3492
+ """
3493
+ A list of dependencies (using `Depends()`) to be applied to the
3494
+ *path operation*.
3495
+
3496
+ Read more about it in the
3497
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
3498
+ """
3499
+ ),
3500
+ ] = None,
3501
+ summary: Annotated[
3502
+ Optional[str],
3503
+ Doc(
3504
+ """
3505
+ A summary for the *path operation*.
3506
+
3507
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3508
+
3509
+ Read more about it in the
3510
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
3511
+ """
3512
+ ),
3513
+ ] = None,
3514
+ description: Annotated[
3515
+ Optional[str],
3516
+ Doc(
3517
+ """
3518
+ A description for the *path operation*.
3519
+
3520
+ If not provided, it will be extracted automatically from the docstring
3521
+ of the *path operation function*.
3522
+
3523
+ It can contain Markdown.
3524
+
3525
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3526
+
3527
+ Read more about it in the
3528
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
3529
+ """
3530
+ ),
3531
+ ] = None,
3532
+ response_description: Annotated[
3533
+ str,
3534
+ Doc(
3535
+ """
3536
+ The description for the default response.
3537
+
3538
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3539
+ """
3540
+ ),
3541
+ ] = "Successful Response",
3542
+ responses: Annotated[
3543
+ Optional[dict[Union[int, str], dict[str, Any]]],
3544
+ Doc(
3545
+ """
3546
+ Additional responses that could be returned by this *path operation*.
3547
+
3548
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3549
+ """
3550
+ ),
3551
+ ] = None,
3552
+ deprecated: Annotated[
3553
+ Optional[bool],
3554
+ Doc(
3555
+ """
3556
+ Mark this *path operation* as deprecated.
3557
+
3558
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3559
+ """
3560
+ ),
3561
+ ] = None,
3562
+ operation_id: Annotated[
3563
+ Optional[str],
3564
+ Doc(
3565
+ """
3566
+ Custom operation ID to be used by this *path operation*.
3567
+
3568
+ By default, it is generated automatically.
3569
+
3570
+ If you provide a custom operation ID, you need to make sure it is
3571
+ unique for the whole API.
3572
+
3573
+ You can customize the
3574
+ operation ID generation with the parameter
3575
+ `generate_unique_id_function` in the `FastAPI` class.
3576
+
3577
+ Read more about it in the
3578
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
3579
+ """
3580
+ ),
3581
+ ] = None,
3582
+ response_model_include: Annotated[
3583
+ Optional[IncEx],
3584
+ Doc(
3585
+ """
3586
+ Configuration passed to Pydantic to include only certain fields in the
3587
+ response data.
3588
+
3589
+ Read more about it in the
3590
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3591
+ """
3592
+ ),
3593
+ ] = None,
3594
+ response_model_exclude: Annotated[
3595
+ Optional[IncEx],
3596
+ Doc(
3597
+ """
3598
+ Configuration passed to Pydantic to exclude certain fields in the
3599
+ response data.
3600
+
3601
+ Read more about it in the
3602
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3603
+ """
3604
+ ),
3605
+ ] = None,
3606
+ response_model_by_alias: Annotated[
3607
+ bool,
3608
+ Doc(
3609
+ """
3610
+ Configuration passed to Pydantic to define if the response model
3611
+ should be serialized by alias when an alias is used.
3612
+
3613
+ Read more about it in the
3614
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3615
+ """
3616
+ ),
3617
+ ] = True,
3618
+ response_model_exclude_unset: Annotated[
3619
+ bool,
3620
+ Doc(
3621
+ """
3622
+ Configuration passed to Pydantic to define if the response data
3623
+ should have all the fields, including the ones that were not set and
3624
+ have their default values. This is different from
3625
+ `response_model_exclude_defaults` in that if the fields are set,
3626
+ they will be included in the response, even if the value is the same
3627
+ as the default.
3628
+
3629
+ When `True`, default values are omitted from the response.
3630
+
3631
+ Read more about it in the
3632
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
3633
+ """
3634
+ ),
3635
+ ] = False,
3636
+ response_model_exclude_defaults: Annotated[
3637
+ bool,
3638
+ Doc(
3639
+ """
3640
+ Configuration passed to Pydantic to define if the response data
3641
+ should have all the fields, including the ones that have the same value
3642
+ as the default. This is different from `response_model_exclude_unset`
3643
+ in that if the fields are set but contain the same default values,
3644
+ they will be excluded from the response.
3645
+
3646
+ When `True`, default values are omitted from the response.
3647
+
3648
+ Read more about it in the
3649
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
3650
+ """
3651
+ ),
3652
+ ] = False,
3653
+ response_model_exclude_none: Annotated[
3654
+ bool,
3655
+ Doc(
3656
+ """
3657
+ Configuration passed to Pydantic to define if the response data should
3658
+ exclude fields set to `None`.
3659
+
3660
+ This is much simpler (less smart) than `response_model_exclude_unset`
3661
+ and `response_model_exclude_defaults`. You probably want to use one of
3662
+ those two instead of this one, as those allow returning `None` values
3663
+ when it makes sense.
3664
+
3665
+ Read more about it in the
3666
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
3667
+ """
3668
+ ),
3669
+ ] = False,
3670
+ include_in_schema: Annotated[
3671
+ bool,
3672
+ Doc(
3673
+ """
3674
+ Include this *path operation* in the generated OpenAPI schema.
3675
+
3676
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
3677
+
3678
+ Read more about it in the
3679
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
3680
+ """
3681
+ ),
3682
+ ] = True,
3683
+ response_class: Annotated[
3684
+ type[Response],
3685
+ Doc(
3686
+ """
3687
+ Response class to be used for this *path operation*.
3688
+
3689
+ This will not be used if you return a response directly.
3690
+
3691
+ Read more about it in the
3692
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
3693
+ """
3694
+ ),
3695
+ ] = Default(JSONResponse),
3696
+ name: Annotated[
3697
+ Optional[str],
3698
+ Doc(
3699
+ """
3700
+ Name for this *path operation*. Only used internally.
3701
+ """
3702
+ ),
3703
+ ] = None,
3704
+ callbacks: Annotated[
3705
+ Optional[list[BaseRoute]],
3706
+ Doc(
3707
+ """
3708
+ List of *path operations* that will be used as OpenAPI callbacks.
3709
+
3710
+ This is only for OpenAPI documentation, the callbacks won't be used
3711
+ directly.
3712
+
3713
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3714
+
3715
+ Read more about it in the
3716
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
3717
+ """
3718
+ ),
3719
+ ] = None,
3720
+ openapi_extra: Annotated[
3721
+ Optional[dict[str, Any]],
3722
+ Doc(
3723
+ """
3724
+ Extra metadata to be included in the OpenAPI schema for this *path
3725
+ operation*.
3726
+
3727
+ Read more about it in the
3728
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
3729
+ """
3730
+ ),
3731
+ ] = None,
3732
+ generate_unique_id_function: Annotated[
3733
+ Callable[[routing.APIRoute], str],
3734
+ Doc(
3735
+ """
3736
+ Customize the function used to generate unique IDs for the *path
3737
+ operations* shown in the generated OpenAPI.
3738
+
3739
+ This is particularly useful when automatically generating clients or
3740
+ SDKs for your API.
3741
+
3742
+ Read more about it in the
3743
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
3744
+ """
3745
+ ),
3746
+ ] = Default(generate_unique_id),
3747
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
3748
+ """
3749
+ Add a *path operation* using an HTTP HEAD operation.
3750
+
3751
+ ## Example
3752
+
3753
+ ```python
3754
+ from fastapi import FastAPI, Response
3755
+
3756
+ app = FastAPI()
3757
+
3758
+ @app.head("/items/", status_code=204)
3759
+ def get_items_headers(response: Response):
3760
+ response.headers["X-Cat-Dog"] = "Alone in the world"
3761
+ ```
3762
+ """
3763
+ return self.router.head(
3764
+ path,
3765
+ response_model=response_model,
3766
+ status_code=status_code,
3767
+ tags=tags,
3768
+ dependencies=dependencies,
3769
+ summary=summary,
3770
+ description=description,
3771
+ response_description=response_description,
3772
+ responses=responses,
3773
+ deprecated=deprecated,
3774
+ operation_id=operation_id,
3775
+ response_model_include=response_model_include,
3776
+ response_model_exclude=response_model_exclude,
3777
+ response_model_by_alias=response_model_by_alias,
3778
+ response_model_exclude_unset=response_model_exclude_unset,
3779
+ response_model_exclude_defaults=response_model_exclude_defaults,
3780
+ response_model_exclude_none=response_model_exclude_none,
3781
+ include_in_schema=include_in_schema,
3782
+ response_class=response_class,
3783
+ name=name,
3784
+ callbacks=callbacks,
3785
+ openapi_extra=openapi_extra,
3786
+ generate_unique_id_function=generate_unique_id_function,
3787
+ )
3788
+
3789
+ def patch(
3790
+ self,
3791
+ path: Annotated[
3792
+ str,
3793
+ Doc(
3794
+ """
3795
+ The URL path to be used for this *path operation*.
3796
+
3797
+ For example, in `http://example.com/items`, the path is `/items`.
3798
+ """
3799
+ ),
3800
+ ],
3801
+ *,
3802
+ response_model: Annotated[
3803
+ Any,
3804
+ Doc(
3805
+ """
3806
+ The type to use for the response.
3807
+
3808
+ It could be any valid Pydantic *field* type. So, it doesn't have to
3809
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
3810
+ etc.
3811
+
3812
+ It will be used for:
3813
+
3814
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
3815
+ show it as the response (JSON Schema).
3816
+ * Serialization: you could return an arbitrary object and the
3817
+ `response_model` would be used to serialize that object into the
3818
+ corresponding JSON.
3819
+ * Filtering: the JSON sent to the client will only contain the data
3820
+ (fields) defined in the `response_model`. If you returned an object
3821
+ that contains an attribute `password` but the `response_model` does
3822
+ not include that field, the JSON sent to the client would not have
3823
+ that `password`.
3824
+ * Validation: whatever you return will be serialized with the
3825
+ `response_model`, converting any data as necessary to generate the
3826
+ corresponding JSON. But if the data in the object returned is not
3827
+ valid, that would mean a violation of the contract with the client,
3828
+ so it's an error from the API developer. So, FastAPI will raise an
3829
+ error and return a 500 error code (Internal Server Error).
3830
+
3831
+ Read more about it in the
3832
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
3833
+ """
3834
+ ),
3835
+ ] = Default(None),
3836
+ status_code: Annotated[
3837
+ Optional[int],
3838
+ Doc(
3839
+ """
3840
+ The default status code to be used for the response.
3841
+
3842
+ You could override the status code by returning a response directly.
3843
+
3844
+ Read more about it in the
3845
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
3846
+ """
3847
+ ),
3848
+ ] = None,
3849
+ tags: Annotated[
3850
+ Optional[list[Union[str, Enum]]],
3851
+ Doc(
3852
+ """
3853
+ A list of tags to be applied to the *path operation*.
3854
+
3855
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3856
+
3857
+ Read more about it in the
3858
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
3859
+ """
3860
+ ),
3861
+ ] = None,
3862
+ dependencies: Annotated[
3863
+ Optional[Sequence[Depends]],
3864
+ Doc(
3865
+ """
3866
+ A list of dependencies (using `Depends()`) to be applied to the
3867
+ *path operation*.
3868
+
3869
+ Read more about it in the
3870
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
3871
+ """
3872
+ ),
3873
+ ] = None,
3874
+ summary: Annotated[
3875
+ Optional[str],
3876
+ Doc(
3877
+ """
3878
+ A summary for the *path operation*.
3879
+
3880
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3881
+
3882
+ Read more about it in the
3883
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
3884
+ """
3885
+ ),
3886
+ ] = None,
3887
+ description: Annotated[
3888
+ Optional[str],
3889
+ Doc(
3890
+ """
3891
+ A description for the *path operation*.
3892
+
3893
+ If not provided, it will be extracted automatically from the docstring
3894
+ of the *path operation function*.
3895
+
3896
+ It can contain Markdown.
3897
+
3898
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3899
+
3900
+ Read more about it in the
3901
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
3902
+ """
3903
+ ),
3904
+ ] = None,
3905
+ response_description: Annotated[
3906
+ str,
3907
+ Doc(
3908
+ """
3909
+ The description for the default response.
3910
+
3911
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3912
+ """
3913
+ ),
3914
+ ] = "Successful Response",
3915
+ responses: Annotated[
3916
+ Optional[dict[Union[int, str], dict[str, Any]]],
3917
+ Doc(
3918
+ """
3919
+ Additional responses that could be returned by this *path operation*.
3920
+
3921
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3922
+ """
3923
+ ),
3924
+ ] = None,
3925
+ deprecated: Annotated[
3926
+ Optional[bool],
3927
+ Doc(
3928
+ """
3929
+ Mark this *path operation* as deprecated.
3930
+
3931
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
3932
+ """
3933
+ ),
3934
+ ] = None,
3935
+ operation_id: Annotated[
3936
+ Optional[str],
3937
+ Doc(
3938
+ """
3939
+ Custom operation ID to be used by this *path operation*.
3940
+
3941
+ By default, it is generated automatically.
3942
+
3943
+ If you provide a custom operation ID, you need to make sure it is
3944
+ unique for the whole API.
3945
+
3946
+ You can customize the
3947
+ operation ID generation with the parameter
3948
+ `generate_unique_id_function` in the `FastAPI` class.
3949
+
3950
+ Read more about it in the
3951
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
3952
+ """
3953
+ ),
3954
+ ] = None,
3955
+ response_model_include: Annotated[
3956
+ Optional[IncEx],
3957
+ Doc(
3958
+ """
3959
+ Configuration passed to Pydantic to include only certain fields in the
3960
+ response data.
3961
+
3962
+ Read more about it in the
3963
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3964
+ """
3965
+ ),
3966
+ ] = None,
3967
+ response_model_exclude: Annotated[
3968
+ Optional[IncEx],
3969
+ Doc(
3970
+ """
3971
+ Configuration passed to Pydantic to exclude certain fields in the
3972
+ response data.
3973
+
3974
+ Read more about it in the
3975
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3976
+ """
3977
+ ),
3978
+ ] = None,
3979
+ response_model_by_alias: Annotated[
3980
+ bool,
3981
+ Doc(
3982
+ """
3983
+ Configuration passed to Pydantic to define if the response model
3984
+ should be serialized by alias when an alias is used.
3985
+
3986
+ Read more about it in the
3987
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
3988
+ """
3989
+ ),
3990
+ ] = True,
3991
+ response_model_exclude_unset: Annotated[
3992
+ bool,
3993
+ Doc(
3994
+ """
3995
+ Configuration passed to Pydantic to define if the response data
3996
+ should have all the fields, including the ones that were not set and
3997
+ have their default values. This is different from
3998
+ `response_model_exclude_defaults` in that if the fields are set,
3999
+ they will be included in the response, even if the value is the same
4000
+ as the default.
4001
+
4002
+ When `True`, default values are omitted from the response.
4003
+
4004
+ Read more about it in the
4005
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
4006
+ """
4007
+ ),
4008
+ ] = False,
4009
+ response_model_exclude_defaults: Annotated[
4010
+ bool,
4011
+ Doc(
4012
+ """
4013
+ Configuration passed to Pydantic to define if the response data
4014
+ should have all the fields, including the ones that have the same value
4015
+ as the default. This is different from `response_model_exclude_unset`
4016
+ in that if the fields are set but contain the same default values,
4017
+ they will be excluded from the response.
4018
+
4019
+ When `True`, default values are omitted from the response.
4020
+
4021
+ Read more about it in the
4022
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
4023
+ """
4024
+ ),
4025
+ ] = False,
4026
+ response_model_exclude_none: Annotated[
4027
+ bool,
4028
+ Doc(
4029
+ """
4030
+ Configuration passed to Pydantic to define if the response data should
4031
+ exclude fields set to `None`.
4032
+
4033
+ This is much simpler (less smart) than `response_model_exclude_unset`
4034
+ and `response_model_exclude_defaults`. You probably want to use one of
4035
+ those two instead of this one, as those allow returning `None` values
4036
+ when it makes sense.
4037
+
4038
+ Read more about it in the
4039
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
4040
+ """
4041
+ ),
4042
+ ] = False,
4043
+ include_in_schema: Annotated[
4044
+ bool,
4045
+ Doc(
4046
+ """
4047
+ Include this *path operation* in the generated OpenAPI schema.
4048
+
4049
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
4050
+
4051
+ Read more about it in the
4052
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
4053
+ """
4054
+ ),
4055
+ ] = True,
4056
+ response_class: Annotated[
4057
+ type[Response],
4058
+ Doc(
4059
+ """
4060
+ Response class to be used for this *path operation*.
4061
+
4062
+ This will not be used if you return a response directly.
4063
+
4064
+ Read more about it in the
4065
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
4066
+ """
4067
+ ),
4068
+ ] = Default(JSONResponse),
4069
+ name: Annotated[
4070
+ Optional[str],
4071
+ Doc(
4072
+ """
4073
+ Name for this *path operation*. Only used internally.
4074
+ """
4075
+ ),
4076
+ ] = None,
4077
+ callbacks: Annotated[
4078
+ Optional[list[BaseRoute]],
4079
+ Doc(
4080
+ """
4081
+ List of *path operations* that will be used as OpenAPI callbacks.
4082
+
4083
+ This is only for OpenAPI documentation, the callbacks won't be used
4084
+ directly.
4085
+
4086
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4087
+
4088
+ Read more about it in the
4089
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
4090
+ """
4091
+ ),
4092
+ ] = None,
4093
+ openapi_extra: Annotated[
4094
+ Optional[dict[str, Any]],
4095
+ Doc(
4096
+ """
4097
+ Extra metadata to be included in the OpenAPI schema for this *path
4098
+ operation*.
4099
+
4100
+ Read more about it in the
4101
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
4102
+ """
4103
+ ),
4104
+ ] = None,
4105
+ generate_unique_id_function: Annotated[
4106
+ Callable[[routing.APIRoute], str],
4107
+ Doc(
4108
+ """
4109
+ Customize the function used to generate unique IDs for the *path
4110
+ operations* shown in the generated OpenAPI.
4111
+
4112
+ This is particularly useful when automatically generating clients or
4113
+ SDKs for your API.
4114
+
4115
+ Read more about it in the
4116
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
4117
+ """
4118
+ ),
4119
+ ] = Default(generate_unique_id),
4120
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
4121
+ """
4122
+ Add a *path operation* using an HTTP PATCH operation.
4123
+
4124
+ ## Example
4125
+
4126
+ ```python
4127
+ from fastapi import FastAPI
4128
+ from pydantic import BaseModel
4129
+
4130
+ class Item(BaseModel):
4131
+ name: str
4132
+ description: str | None = None
4133
+
4134
+ app = FastAPI()
4135
+
4136
+ @app.patch("/items/")
4137
+ def update_item(item: Item):
4138
+ return {"message": "Item updated in place"}
4139
+ ```
4140
+ """
4141
+ return self.router.patch(
4142
+ path,
4143
+ response_model=response_model,
4144
+ status_code=status_code,
4145
+ tags=tags,
4146
+ dependencies=dependencies,
4147
+ summary=summary,
4148
+ description=description,
4149
+ response_description=response_description,
4150
+ responses=responses,
4151
+ deprecated=deprecated,
4152
+ operation_id=operation_id,
4153
+ response_model_include=response_model_include,
4154
+ response_model_exclude=response_model_exclude,
4155
+ response_model_by_alias=response_model_by_alias,
4156
+ response_model_exclude_unset=response_model_exclude_unset,
4157
+ response_model_exclude_defaults=response_model_exclude_defaults,
4158
+ response_model_exclude_none=response_model_exclude_none,
4159
+ include_in_schema=include_in_schema,
4160
+ response_class=response_class,
4161
+ name=name,
4162
+ callbacks=callbacks,
4163
+ openapi_extra=openapi_extra,
4164
+ generate_unique_id_function=generate_unique_id_function,
4165
+ )
4166
+
4167
+ def trace(
4168
+ self,
4169
+ path: Annotated[
4170
+ str,
4171
+ Doc(
4172
+ """
4173
+ The URL path to be used for this *path operation*.
4174
+
4175
+ For example, in `http://example.com/items`, the path is `/items`.
4176
+ """
4177
+ ),
4178
+ ],
4179
+ *,
4180
+ response_model: Annotated[
4181
+ Any,
4182
+ Doc(
4183
+ """
4184
+ The type to use for the response.
4185
+
4186
+ It could be any valid Pydantic *field* type. So, it doesn't have to
4187
+ be a Pydantic model, it could be other things, like a `list`, `dict`,
4188
+ etc.
4189
+
4190
+ It will be used for:
4191
+
4192
+ * Documentation: the generated OpenAPI (and the UI at `/docs`) will
4193
+ show it as the response (JSON Schema).
4194
+ * Serialization: you could return an arbitrary object and the
4195
+ `response_model` would be used to serialize that object into the
4196
+ corresponding JSON.
4197
+ * Filtering: the JSON sent to the client will only contain the data
4198
+ (fields) defined in the `response_model`. If you returned an object
4199
+ that contains an attribute `password` but the `response_model` does
4200
+ not include that field, the JSON sent to the client would not have
4201
+ that `password`.
4202
+ * Validation: whatever you return will be serialized with the
4203
+ `response_model`, converting any data as necessary to generate the
4204
+ corresponding JSON. But if the data in the object returned is not
4205
+ valid, that would mean a violation of the contract with the client,
4206
+ so it's an error from the API developer. So, FastAPI will raise an
4207
+ error and return a 500 error code (Internal Server Error).
4208
+
4209
+ Read more about it in the
4210
+ [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
4211
+ """
4212
+ ),
4213
+ ] = Default(None),
4214
+ status_code: Annotated[
4215
+ Optional[int],
4216
+ Doc(
4217
+ """
4218
+ The default status code to be used for the response.
4219
+
4220
+ You could override the status code by returning a response directly.
4221
+
4222
+ Read more about it in the
4223
+ [FastAPI docs for Response Status Code](https://fastapi.tiangolo.com/tutorial/response-status-code/).
4224
+ """
4225
+ ),
4226
+ ] = None,
4227
+ tags: Annotated[
4228
+ Optional[list[Union[str, Enum]]],
4229
+ Doc(
4230
+ """
4231
+ A list of tags to be applied to the *path operation*.
4232
+
4233
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4234
+
4235
+ Read more about it in the
4236
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/#tags).
4237
+ """
4238
+ ),
4239
+ ] = None,
4240
+ dependencies: Annotated[
4241
+ Optional[Sequence[Depends]],
4242
+ Doc(
4243
+ """
4244
+ A list of dependencies (using `Depends()`) to be applied to the
4245
+ *path operation*.
4246
+
4247
+ Read more about it in the
4248
+ [FastAPI docs for Dependencies in path operation decorators](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).
4249
+ """
4250
+ ),
4251
+ ] = None,
4252
+ summary: Annotated[
4253
+ Optional[str],
4254
+ Doc(
4255
+ """
4256
+ A summary for the *path operation*.
4257
+
4258
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4259
+
4260
+ Read more about it in the
4261
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
4262
+ """
4263
+ ),
4264
+ ] = None,
4265
+ description: Annotated[
4266
+ Optional[str],
4267
+ Doc(
4268
+ """
4269
+ A description for the *path operation*.
4270
+
4271
+ If not provided, it will be extracted automatically from the docstring
4272
+ of the *path operation function*.
4273
+
4274
+ It can contain Markdown.
4275
+
4276
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4277
+
4278
+ Read more about it in the
4279
+ [FastAPI docs for Path Operation Configuration](https://fastapi.tiangolo.com/tutorial/path-operation-configuration/).
4280
+ """
4281
+ ),
4282
+ ] = None,
4283
+ response_description: Annotated[
4284
+ str,
4285
+ Doc(
4286
+ """
4287
+ The description for the default response.
4288
+
4289
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4290
+ """
4291
+ ),
4292
+ ] = "Successful Response",
4293
+ responses: Annotated[
4294
+ Optional[dict[Union[int, str], dict[str, Any]]],
4295
+ Doc(
4296
+ """
4297
+ Additional responses that could be returned by this *path operation*.
4298
+
4299
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4300
+ """
4301
+ ),
4302
+ ] = None,
4303
+ deprecated: Annotated[
4304
+ Optional[bool],
4305
+ Doc(
4306
+ """
4307
+ Mark this *path operation* as deprecated.
4308
+
4309
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4310
+ """
4311
+ ),
4312
+ ] = None,
4313
+ operation_id: Annotated[
4314
+ Optional[str],
4315
+ Doc(
4316
+ """
4317
+ Custom operation ID to be used by this *path operation*.
4318
+
4319
+ By default, it is generated automatically.
4320
+
4321
+ If you provide a custom operation ID, you need to make sure it is
4322
+ unique for the whole API.
4323
+
4324
+ You can customize the
4325
+ operation ID generation with the parameter
4326
+ `generate_unique_id_function` in the `FastAPI` class.
4327
+
4328
+ Read more about it in the
4329
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
4330
+ """
4331
+ ),
4332
+ ] = None,
4333
+ response_model_include: Annotated[
4334
+ Optional[IncEx],
4335
+ Doc(
4336
+ """
4337
+ Configuration passed to Pydantic to include only certain fields in the
4338
+ response data.
4339
+
4340
+ Read more about it in the
4341
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
4342
+ """
4343
+ ),
4344
+ ] = None,
4345
+ response_model_exclude: Annotated[
4346
+ Optional[IncEx],
4347
+ Doc(
4348
+ """
4349
+ Configuration passed to Pydantic to exclude certain fields in the
4350
+ response data.
4351
+
4352
+ Read more about it in the
4353
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
4354
+ """
4355
+ ),
4356
+ ] = None,
4357
+ response_model_by_alias: Annotated[
4358
+ bool,
4359
+ Doc(
4360
+ """
4361
+ Configuration passed to Pydantic to define if the response model
4362
+ should be serialized by alias when an alias is used.
4363
+
4364
+ Read more about it in the
4365
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude).
4366
+ """
4367
+ ),
4368
+ ] = True,
4369
+ response_model_exclude_unset: Annotated[
4370
+ bool,
4371
+ Doc(
4372
+ """
4373
+ Configuration passed to Pydantic to define if the response data
4374
+ should have all the fields, including the ones that were not set and
4375
+ have their default values. This is different from
4376
+ `response_model_exclude_defaults` in that if the fields are set,
4377
+ they will be included in the response, even if the value is the same
4378
+ as the default.
4379
+
4380
+ When `True`, default values are omitted from the response.
4381
+
4382
+ Read more about it in the
4383
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
4384
+ """
4385
+ ),
4386
+ ] = False,
4387
+ response_model_exclude_defaults: Annotated[
4388
+ bool,
4389
+ Doc(
4390
+ """
4391
+ Configuration passed to Pydantic to define if the response data
4392
+ should have all the fields, including the ones that have the same value
4393
+ as the default. This is different from `response_model_exclude_unset`
4394
+ in that if the fields are set but contain the same default values,
4395
+ they will be excluded from the response.
4396
+
4397
+ When `True`, default values are omitted from the response.
4398
+
4399
+ Read more about it in the
4400
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter).
4401
+ """
4402
+ ),
4403
+ ] = False,
4404
+ response_model_exclude_none: Annotated[
4405
+ bool,
4406
+ Doc(
4407
+ """
4408
+ Configuration passed to Pydantic to define if the response data should
4409
+ exclude fields set to `None`.
4410
+
4411
+ This is much simpler (less smart) than `response_model_exclude_unset`
4412
+ and `response_model_exclude_defaults`. You probably want to use one of
4413
+ those two instead of this one, as those allow returning `None` values
4414
+ when it makes sense.
4415
+
4416
+ Read more about it in the
4417
+ [FastAPI docs for Response Model - Return Type](https://fastapi.tiangolo.com/tutorial/response-model/#response_model_exclude_none).
4418
+ """
4419
+ ),
4420
+ ] = False,
4421
+ include_in_schema: Annotated[
4422
+ bool,
4423
+ Doc(
4424
+ """
4425
+ Include this *path operation* in the generated OpenAPI schema.
4426
+
4427
+ This affects the generated OpenAPI (e.g. visible at `/docs`).
4428
+
4429
+ Read more about it in the
4430
+ [FastAPI docs for Query Parameters and String Validations](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#exclude-parameters-from-openapi).
4431
+ """
4432
+ ),
4433
+ ] = True,
4434
+ response_class: Annotated[
4435
+ type[Response],
4436
+ Doc(
4437
+ """
4438
+ Response class to be used for this *path operation*.
4439
+
4440
+ This will not be used if you return a response directly.
4441
+
4442
+ Read more about it in the
4443
+ [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse).
4444
+ """
4445
+ ),
4446
+ ] = Default(JSONResponse),
4447
+ name: Annotated[
4448
+ Optional[str],
4449
+ Doc(
4450
+ """
4451
+ Name for this *path operation*. Only used internally.
4452
+ """
4453
+ ),
4454
+ ] = None,
4455
+ callbacks: Annotated[
4456
+ Optional[list[BaseRoute]],
4457
+ Doc(
4458
+ """
4459
+ List of *path operations* that will be used as OpenAPI callbacks.
4460
+
4461
+ This is only for OpenAPI documentation, the callbacks won't be used
4462
+ directly.
4463
+
4464
+ It will be added to the generated OpenAPI (e.g. visible at `/docs`).
4465
+
4466
+ Read more about it in the
4467
+ [FastAPI docs for OpenAPI Callbacks](https://fastapi.tiangolo.com/advanced/openapi-callbacks/).
4468
+ """
4469
+ ),
4470
+ ] = None,
4471
+ openapi_extra: Annotated[
4472
+ Optional[dict[str, Any]],
4473
+ Doc(
4474
+ """
4475
+ Extra metadata to be included in the OpenAPI schema for this *path
4476
+ operation*.
4477
+
4478
+ Read more about it in the
4479
+ [FastAPI docs for Path Operation Advanced Configuration](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/#custom-openapi-path-operation-schema).
4480
+ """
4481
+ ),
4482
+ ] = None,
4483
+ generate_unique_id_function: Annotated[
4484
+ Callable[[routing.APIRoute], str],
4485
+ Doc(
4486
+ """
4487
+ Customize the function used to generate unique IDs for the *path
4488
+ operations* shown in the generated OpenAPI.
4489
+
4490
+ This is particularly useful when automatically generating clients or
4491
+ SDKs for your API.
4492
+
4493
+ Read more about it in the
4494
+ [FastAPI docs about how to Generate Clients](https://fastapi.tiangolo.com/advanced/generate-clients/#custom-generate-unique-id-function).
4495
+ """
4496
+ ),
4497
+ ] = Default(generate_unique_id),
4498
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
4499
+ """
4500
+ Add a *path operation* using an HTTP TRACE operation.
4501
+
4502
+ ## Example
4503
+
4504
+ ```python
4505
+ from fastapi import FastAPI
4506
+
4507
+ app = FastAPI()
4508
+
4509
+ @app.trace("/items/{item_id}")
4510
+ def trace_item(item_id: str):
4511
+ return None
4512
+ ```
4513
+ """
4514
+ return self.router.trace(
4515
+ path,
4516
+ response_model=response_model,
4517
+ status_code=status_code,
4518
+ tags=tags,
4519
+ dependencies=dependencies,
4520
+ summary=summary,
4521
+ description=description,
4522
+ response_description=response_description,
4523
+ responses=responses,
4524
+ deprecated=deprecated,
4525
+ operation_id=operation_id,
4526
+ response_model_include=response_model_include,
4527
+ response_model_exclude=response_model_exclude,
4528
+ response_model_by_alias=response_model_by_alias,
4529
+ response_model_exclude_unset=response_model_exclude_unset,
4530
+ response_model_exclude_defaults=response_model_exclude_defaults,
4531
+ response_model_exclude_none=response_model_exclude_none,
4532
+ include_in_schema=include_in_schema,
4533
+ response_class=response_class,
4534
+ name=name,
4535
+ callbacks=callbacks,
4536
+ openapi_extra=openapi_extra,
4537
+ generate_unique_id_function=generate_unique_id_function,
4538
+ )
4539
+
4540
+ def websocket_route(
4541
+ self, path: str, name: Union[str, None] = None
4542
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
4543
+ def decorator(func: DecoratedCallable) -> DecoratedCallable:
4544
+ self.router.add_websocket_route(path, func, name=name)
4545
+ return func
4546
+
4547
+ return decorator
4548
+
4549
+ @deprecated(
4550
+ """
4551
+ on_event is deprecated, use lifespan event handlers instead.
4552
+
4553
+ Read more about it in the
4554
+ [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
4555
+ """
4556
+ )
4557
+ def on_event(
4558
+ self,
4559
+ event_type: Annotated[
4560
+ str,
4561
+ Doc(
4562
+ """
4563
+ The type of event. `startup` or `shutdown`.
4564
+ """
4565
+ ),
4566
+ ],
4567
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
4568
+ """
4569
+ Add an event handler for the application.
4570
+
4571
+ `on_event` is deprecated, use `lifespan` event handlers instead.
4572
+
4573
+ Read more about it in the
4574
+ [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/#alternative-events-deprecated).
4575
+ """
4576
+ return self.router.on_event(event_type)
4577
+
4578
+ def middleware(
4579
+ self,
4580
+ middleware_type: Annotated[
4581
+ str,
4582
+ Doc(
4583
+ """
4584
+ The type of middleware. Currently only supports `http`.
4585
+ """
4586
+ ),
4587
+ ],
4588
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
4589
+ """
4590
+ Add a middleware to the application.
4591
+
4592
+ Read more about it in the
4593
+ [FastAPI docs for Middleware](https://fastapi.tiangolo.com/tutorial/middleware/).
4594
+
4595
+ ## Example
4596
+
4597
+ ```python
4598
+ import time
4599
+ from typing import Awaitable, Callable
4600
+
4601
+ from fastapi import FastAPI, Request, Response
4602
+
4603
+ app = FastAPI()
4604
+
4605
+
4606
+ @app.middleware("http")
4607
+ async def add_process_time_header(
4608
+ request: Request, call_next: Callable[[Request], Awaitable[Response]]
4609
+ ) -> Response:
4610
+ start_time = time.time()
4611
+ response = await call_next(request)
4612
+ process_time = time.time() - start_time
4613
+ response.headers["X-Process-Time"] = str(process_time)
4614
+ return response
4615
+ ```
4616
+ """
4617
+
4618
+ def decorator(func: DecoratedCallable) -> DecoratedCallable:
4619
+ self.add_middleware(BaseHTTPMiddleware, dispatch=func)
4620
+ return func
4621
+
4622
+ return decorator
4623
+
4624
+ def exception_handler(
4625
+ self,
4626
+ exc_class_or_status_code: Annotated[
4627
+ Union[int, type[Exception]],
4628
+ Doc(
4629
+ """
4630
+ The Exception class this would handle, or a status code.
4631
+ """
4632
+ ),
4633
+ ],
4634
+ ) -> Callable[[DecoratedCallable], DecoratedCallable]:
4635
+ """
4636
+ Add an exception handler to the app.
4637
+
4638
+ Read more about it in the
4639
+ [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).
4640
+
4641
+ ## Example
4642
+
4643
+ ```python
4644
+ from fastapi import FastAPI, Request
4645
+ from fastapi.responses import JSONResponse
4646
+
4647
+
4648
+ class UnicornException(Exception):
4649
+ def __init__(self, name: str):
4650
+ self.name = name
4651
+
4652
+
4653
+ app = FastAPI()
4654
+
4655
+
4656
+ @app.exception_handler(UnicornException)
4657
+ async def unicorn_exception_handler(request: Request, exc: UnicornException):
4658
+ return JSONResponse(
4659
+ status_code=418,
4660
+ content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."},
4661
+ )
4662
+ ```
4663
+ """
4664
+
4665
+ def decorator(func: DecoratedCallable) -> DecoratedCallable:
4666
+ self.add_exception_handler(exc_class_or_status_code, func)
4667
+ return func
4668
+
4669
+ return decorator