mirror of https://github.com/tiangolo/fastapi.git
Better wording
This commit is contained in:
parent
6d0c283cef
commit
bdc929a92a
|
|
@ -11,7 +11,7 @@ hide:
|
|||
|
||||
### Basiert auf offenen Standards
|
||||
|
||||
* <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>OpenAPI</strong></a> für die Erstellung von APIs, inklusive Deklarationen von <abbr title="auch genannt Endpunkte, Routen">Pfad</abbr>-<abbr title="gemeint sind HTTP-Methoden wie POST, GET, PUT, DELETE">Operationen</abbr>, Parametern, Body-Anfragen, Sicherheit, usw.
|
||||
* <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>OpenAPI</strong></a> für die Erstellung von APIs, inklusive Deklarationen von <abbr title="auch genannt Endpunkte, Routen">Pfad</abbr>-<abbr title="gemeint sind HTTP-Methoden wie POST, GET, PUT, DELETE">Operationen</abbr>, Parametern, Requestbodys, Sicherheit, usw.
|
||||
* Automatische Dokumentation der Datenmodelle mit <a href="https://json-schema.org/" class="external-link" target="_blank"><strong>JSON Schema</strong></a> (da OpenAPI selbst auf JSON Schema basiert).
|
||||
* Um diese Standards herum entworfen, nach sorgfältigem Studium. Statt einer nachträglichen Schicht darüber.
|
||||
* Dies ermöglicht auch automatische **Client-Code-Generierung** in vielen Sprachen.
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ For example, to declare another response with a status code `404` and a Pydantic
|
|||
!!! info
|
||||
The `model` key is not part of OpenAPI.
|
||||
|
||||
**FastAPI** will take the Pydantic model from there, generate the `JSON Schema`, and put it in the correct place.
|
||||
**FastAPI** will take the Pydantic model from there, generate the JSON Schema, and put it in the correct place.
|
||||
|
||||
The correct place is:
|
||||
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ Now create that other file `routes.toml`:
|
|||
|
||||
This file configures Traefik to use the path prefix `/api/v1`.
|
||||
|
||||
And then it will redirect its requests to your Uvicorn running on `http://127.0.0.1:8000`.
|
||||
And then Traefik will redirect its requests to your Uvicorn running on `http://127.0.0.1:8000`.
|
||||
|
||||
Now start Traefik:
|
||||
|
||||
|
|
|
|||
|
|
@ -216,11 +216,11 @@ This includes many libraries to interact with cloud storage, video processing, a
|
|||
|
||||
1. This is the generator function. It's a "generator function" because it contains `yield` statements inside.
|
||||
2. By using a `with` block, we make sure that the file-like object is closed after the generator function is done. So, after it finishes sending the response.
|
||||
3. This `yield from` tells the function to iterate over that thing named `file_like`. And then, for each part iterated, yield that part as coming from this generator function.
|
||||
3. This `yield from` tells the function to iterate over that thing named `file_like`. And then, for each part iterated, yield that part as coming from this generator function (`iterfile`).
|
||||
|
||||
So, it is a generator function that transfers the "generating" work to something else internally.
|
||||
|
||||
By doing it this way, we can put it in a `with` block, and that way, ensure that it is closed after finishing.
|
||||
By doing it this way, we can put it in a `with` block, and that way, ensure that the file-like object is closed after finishing.
|
||||
|
||||
!!! tip
|
||||
Notice that here as we are using standard `open()` that doesn't support `async` and `await`, we declare the path operation with normal `def`.
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ Now, let's see how you could use that to return a custom response.
|
|||
|
||||
Let's say that you want to return an <a href="https://en.wikipedia.org/wiki/XML" class="external-link" target="_blank">XML</a> response.
|
||||
|
||||
You could put your XML content in a string, put it in a `Response`, and return it:
|
||||
You could put your XML content in a string, put that in a `Response`, and return it:
|
||||
|
||||
```Python hl_lines="1 18"
|
||||
{!../../../docs_src/response_directly/tutorial002.py!}
|
||||
|
|
|
|||
|
|
@ -298,7 +298,7 @@ As it is based on the previous standard for synchronous Python web frameworks (W
|
|||
|
||||
Hug inspired **FastAPI** to declare a `response` parameter in functions to set headers and cookies.
|
||||
|
||||
### <a href="https://github.com/encode/apistar" class="external-link" target="_blank">APIStar</a> (<= 0.5)
|
||||
### <a href="https://github.com/encode/apistar" class="external-link" target="_blank">APIStar</a> (≦ 0.5)
|
||||
|
||||
Right before deciding to build **FastAPI** I found **APIStar** server. It had almost everything I was looking for and had a great design.
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ hide:
|
|||
|
||||
### Based on open standards
|
||||
|
||||
* <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>OpenAPI</strong></a> for API creation, including declarations of <abbr title="also known as: endpoints, routes">path</abbr> <abbr title="also known as HTTP methods, as POST, GET, PUT, DELETE">operations</abbr>, parameters, body requests, security, etc.
|
||||
* <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>OpenAPI</strong></a> for API creation, including declarations of <abbr title="also known as: endpoints, routes">path</abbr> <abbr title="also known as HTTP methods, as POST, GET, PUT, DELETE">operations</abbr>, parameters, request bodies, security, etc.
|
||||
* Automatic data model documentation with <a href="https://json-schema.org/" class="external-link" target="_blank"><strong>JSON Schema</strong></a> (as OpenAPI itself is based on JSON Schema).
|
||||
* Designed around these standards, after a meticulous study. Instead of an afterthought layer on top.
|
||||
* This also allows using automatic **client code generation** in many languages.
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ This includes, for example:
|
|||
* Email notifications sent after performing an action:
|
||||
* As connecting to an email server and sending an email tends to be "slow" (several seconds), you can return the response right away and send the email notification in the background.
|
||||
* Processing data:
|
||||
* For example, let's say you receive a file that must go through a slow process, you can return a response of "Accepted" (HTTP 202) and process it in the background.
|
||||
* For example, let's say you receive a file that must go through a slow process, you can return a response of "Accepted" (HTTP 202) and process the file in the background.
|
||||
|
||||
## Using `BackgroundTasks`
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ When you need to send data from a client (let's say, a browser) to your API, you
|
|||
|
||||
A **request** body is data sent by the client to your API. A **response** body is the data your API sends to the client.
|
||||
|
||||
Your API almost always has to send a **response** body. But clients don't necessarily need to send **request** bodies all the time.
|
||||
Your API almost always has to send a **response** body. But clients don't necessarily need to send **request** bodies all the time. (Just metadata instead)
|
||||
|
||||
To declare a **request** body, you use <a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic</a> models with all their power and benefits.
|
||||
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ Even if they are all in `localhost`, they use different protocols or ports, so,
|
|||
|
||||
So, let's say you have a frontend running in your browser at `http://localhost:8080`, and its JavaScript is trying to communicate with a backend running at `http://localhost` (because we don't specify a port, the browser will assume the default port `80`).
|
||||
|
||||
Then, the browser will send an HTTP `OPTIONS` request to the backend, and if the backend sends the appropriate headers authorizing the communication from this different origin (`http://localhost:8080`) then the browser will let the JavaScript in the frontend send its request to the backend.
|
||||
Then, the browser will send an HTTP `OPTIONS` request to the `:80`-backend, and if the backend sends the appropriate headers authorizing the communication from this different origin (`http://localhost:8080`) then the `:8080`-browser will let the JavaScript in the frontend send its request to the `:80`-backend.
|
||||
|
||||
To achieve this, the backend must have a list of "allowed origins".
|
||||
To achieve this, the `:80`-backend must have a list of "allowed origins".
|
||||
|
||||
In this case, it would have to include `http://localhost:8080` for the frontend to work correctly.
|
||||
In this case, the list would have to include `http://localhost:8080` for the `:8080`-frontend to work correctly.
|
||||
|
||||
## Wildcards
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ FastAPI supports dependencies that do some <abbr title='sometimes also called "e
|
|||
To do this, use `yield` instead of `return`, and write the extra steps (code) after.
|
||||
|
||||
!!! tip
|
||||
Make sure to use `yield` one single time.
|
||||
Make sure to use `yield` one single time per dependency.
|
||||
|
||||
!!! note "Technical Details"
|
||||
Any function that is valid to use with:
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ UserInDB(
|
|||
```
|
||||
|
||||
!!! warning
|
||||
The supporting additional functions are just to demo a possible flow of the data, but they of course are not providing any real security.
|
||||
The supporting additional functions `fake_password_hasher` and `fake_save_user` are just to demo a possible flow of the data, but they of course are not providing any real security.
|
||||
|
||||
## Reduce duplication
|
||||
|
||||
|
|
@ -177,7 +177,7 @@ That way, we can declare just the differences between the models (with plaintext
|
|||
|
||||
## `Union` or `anyOf`
|
||||
|
||||
You can declare a response to be the `Union` of two types, that means, that the response would be any of the two.
|
||||
You can declare a response to be the `Union` of two or more types, that means, that the response would be any of them.
|
||||
|
||||
It will be defined in OpenAPI with `anyOf`.
|
||||
|
||||
|
|
@ -210,7 +210,7 @@ If it was in a type annotation we could have used the vertical bar, as:
|
|||
some_variable: PlaneItem | CarItem
|
||||
```
|
||||
|
||||
But if we put that in `response_model=PlaneItem | CarItem` we would get an error, because Python would try to perform an **invalid operation** between `PlaneItem` and `CarItem` instead of interpreting that as a type annotation.
|
||||
But if we put that in the assignment `response_model=PlaneItem | CarItem` we would get an error, because Python would try to perform an **invalid operation** between `PlaneItem` and `CarItem` instead of interpreting that as a type annotation.
|
||||
|
||||
## List of models
|
||||
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ FastAPI will now:
|
|||
* Show a **clear error** for the client when the data is not valid
|
||||
* **Document** the parameter in the OpenAPI schema *path operation* (so it will show up in the **automatic docs UI**)
|
||||
|
||||
## Alternative (old) `Query` as the default value
|
||||
## Alternative (old): `Query` as the default value
|
||||
|
||||
Previous versions of FastAPI (before <abbr title="before 2023-03">0.95.0</abbr>) required you to use `Query` as the default value of your parameter, instead of putting it in `Annotated`, there's a high chance that you will see code using it around, so I'll explain it to you.
|
||||
|
||||
|
|
@ -170,7 +170,7 @@ q: str | None = Query(default=None)
|
|||
q: str | None = None
|
||||
```
|
||||
|
||||
But it declares it explicitly as being a query parameter.
|
||||
But the `Query` versions declare it explicitly as being a query parameter.
|
||||
|
||||
!!! info
|
||||
Keep in mind that the most important part to make a parameter optional is the part:
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ Using `UploadFile` has several advantages over `bytes`:
|
|||
|
||||
* `filename`: A `str` with the original file name that was uploaded (e.g. `myimage.jpg`).
|
||||
* `content_type`: A `str` with the content type (MIME type / media type) (e.g. `image/jpeg`).
|
||||
* `file`: A <a href="https://docs.python.org/3/library/tempfile.html#tempfile.SpooledTemporaryFile" class="external-link" target="_blank">`SpooledTemporaryFile`</a> (a <a href="https://docs.python.org/3/glossary.html#term-file-like-object" class="external-link" target="_blank">file-like</a> object). This is the actual Python file that you can pass directly to other functions or libraries that expect a "file-like" object.
|
||||
* `file`: A <a href="https://docs.python.org/3/library/tempfile.html#tempfile.SpooledTemporaryFile" class="external-link" target="_blank">`SpooledTemporaryFile`</a> (a <a href="https://docs.python.org/3/glossary.html#term-file-like-object" class="external-link" target="_blank">file-like</a> object). This is the actual Python file object that you can pass directly to other functions or libraries that expect a "file-like" object.
|
||||
|
||||
`UploadFile` has the following `async` methods. They all call the corresponding file methods underneath (using the internal `SpooledTemporaryFile`).
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue