Browse Source

Remove code examples for Python 3.8 in `custom_response`

pull/14510/head
Yurii Motov 7 months ago
parent
commit
67275dc45f
  1. 30
      docs/en/docs/advanced/custom-response.md
  2. 0
      docs_src/custom_response/tutorial001_py39.py
  3. 0
      docs_src/custom_response/tutorial001b_py39.py
  4. 0
      docs_src/custom_response/tutorial002_py39.py
  5. 0
      docs_src/custom_response/tutorial003_py39.py
  6. 0
      docs_src/custom_response/tutorial004_py39.py
  7. 0
      docs_src/custom_response/tutorial005_py39.py
  8. 0
      docs_src/custom_response/tutorial006_py39.py
  9. 0
      docs_src/custom_response/tutorial006b_py39.py
  10. 0
      docs_src/custom_response/tutorial006c_py39.py
  11. 0
      docs_src/custom_response/tutorial007_py39.py
  12. 0
      docs_src/custom_response/tutorial008_py39.py
  13. 0
      docs_src/custom_response/tutorial009_py39.py
  14. 0
      docs_src/custom_response/tutorial009b_py39.py
  15. 0
      docs_src/custom_response/tutorial009c_py39.py
  16. 0
      docs_src/custom_response/tutorial010_py39.py
  17. 2
      tests/test_tutorial/test_custom_response/test_tutorial001.py
  18. 2
      tests/test_tutorial/test_custom_response/test_tutorial001b.py
  19. 2
      tests/test_tutorial/test_custom_response/test_tutorial004.py
  20. 2
      tests/test_tutorial/test_custom_response/test_tutorial005.py
  21. 2
      tests/test_tutorial/test_custom_response/test_tutorial006.py
  22. 2
      tests/test_tutorial/test_custom_response/test_tutorial006b.py
  23. 2
      tests/test_tutorial/test_custom_response/test_tutorial006c.py
  24. 2
      tests/test_tutorial/test_custom_response/test_tutorial007.py
  25. 6
      tests/test_tutorial/test_custom_response/test_tutorial008.py
  26. 6
      tests/test_tutorial/test_custom_response/test_tutorial009.py
  27. 6
      tests/test_tutorial/test_custom_response/test_tutorial009b.py
  28. 2
      tests/test_tutorial/test_custom_response/test_tutorial009c.py

30
docs/en/docs/advanced/custom-response.md

@ -30,7 +30,7 @@ This is because by default, FastAPI will inspect every item inside and make sure
But if you are certain that the content that you are returning is **serializable with JSON**, you can pass it directly to the response class and avoid the extra overhead that FastAPI would have by passing your return content through the `jsonable_encoder` before passing it to the response class.
{* ../../docs_src/custom_response/tutorial001b.py hl[2,7] *}
{* ../../docs_src/custom_response/tutorial001b_py39.py hl[2,7] *}
/// info
@ -55,7 +55,7 @@ To return a response with HTML directly from **FastAPI**, use `HTMLResponse`.
* Import `HTMLResponse`.
* Pass `HTMLResponse` as the parameter `response_class` of your *path operation decorator*.
{* ../../docs_src/custom_response/tutorial002.py hl[2,7] *}
{* ../../docs_src/custom_response/tutorial002_py39.py hl[2,7] *}
/// info
@ -73,7 +73,7 @@ As seen in [Return a Response directly](response-directly.md){.internal-link tar
The same example from above, returning an `HTMLResponse`, could look like:
{* ../../docs_src/custom_response/tutorial003.py hl[2,7,19] *}
{* ../../docs_src/custom_response/tutorial003_py39.py hl[2,7,19] *}
/// warning
@ -97,7 +97,7 @@ The `response_class` will then be used only to document the OpenAPI *path operat
For example, it could be something like:
{* ../../docs_src/custom_response/tutorial004.py hl[7,21,23] *}
{* ../../docs_src/custom_response/tutorial004_py39.py hl[7,21,23] *}
In this example, the function `generate_html_response()` already generates and returns a `Response` instead of returning the HTML in a `str`.
@ -146,7 +146,7 @@ Takes some text or bytes and returns an HTML response, as you read above.
Takes some text or bytes and returns a plain text response.
{* ../../docs_src/custom_response/tutorial005.py hl[2,7,9] *}
{* ../../docs_src/custom_response/tutorial005_py39.py hl[2,7,9] *}
### `JSONResponse` { #jsonresponse }
@ -180,7 +180,7 @@ This requires installing `ujson` for example with `pip install ujson`.
///
{* ../../docs_src/custom_response/tutorial001.py hl[2,7] *}
{* ../../docs_src/custom_response/tutorial001_py39.py hl[2,7] *}
/// tip
@ -194,14 +194,14 @@ Returns an HTTP redirect. Uses a 307 status code (Temporary Redirect) by default
You can return a `RedirectResponse` directly:
{* ../../docs_src/custom_response/tutorial006.py hl[2,9] *}
{* ../../docs_src/custom_response/tutorial006_py39.py hl[2,9] *}
---
Or you can use it in the `response_class` parameter:
{* ../../docs_src/custom_response/tutorial006b.py hl[2,7,9] *}
{* ../../docs_src/custom_response/tutorial006b_py39.py hl[2,7,9] *}
If you do that, then you can return the URL directly from your *path operation* function.
@ -211,13 +211,13 @@ In this case, the `status_code` used will be the default one for the `RedirectRe
You can also use the `status_code` parameter combined with the `response_class` parameter:
{* ../../docs_src/custom_response/tutorial006c.py hl[2,7,9] *}
{* ../../docs_src/custom_response/tutorial006c_py39.py hl[2,7,9] *}
### `StreamingResponse` { #streamingresponse }
Takes an async generator or a normal generator/iterator and streams the response body.
{* ../../docs_src/custom_response/tutorial007.py hl[2,14] *}
{* ../../docs_src/custom_response/tutorial007_py39.py hl[2,14] *}
#### Using `StreamingResponse` with file-like objects { #using-streamingresponse-with-file-like-objects }
@ -227,7 +227,7 @@ That way, you don't have to read it all first in memory, and you can pass that g
This includes many libraries to interact with cloud storage, video processing, and others.
{* ../../docs_src/custom_response/tutorial008.py hl[2,10:12,14] *}
{* ../../docs_src/custom_response/tutorial008_py39.py hl[2,10:12,14] *}
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.
@ -256,11 +256,11 @@ Takes a different set of arguments to instantiate than the other response types:
File responses will include appropriate `Content-Length`, `Last-Modified` and `ETag` headers.
{* ../../docs_src/custom_response/tutorial009.py hl[2,10] *}
{* ../../docs_src/custom_response/tutorial009_py39.py hl[2,10] *}
You can also use the `response_class` parameter:
{* ../../docs_src/custom_response/tutorial009b.py hl[2,8,10] *}
{* ../../docs_src/custom_response/tutorial009b_py39.py hl[2,8,10] *}
In this case, you can return the file path directly from your *path operation* function.
@ -274,7 +274,7 @@ Let's say you want it to return indented and formatted JSON, so you want to use
You could create a `CustomORJSONResponse`. The main thing you have to do is create a `Response.render(content)` method that returns the content as `bytes`:
{* ../../docs_src/custom_response/tutorial009c.py hl[9:14,17] *}
{* ../../docs_src/custom_response/tutorial009c_py39.py hl[9:14,17] *}
Now instead of returning:
@ -300,7 +300,7 @@ The parameter that defines this is `default_response_class`.
In the example below, **FastAPI** will use `ORJSONResponse` by default, in all *path operations*, instead of `JSONResponse`.
{* ../../docs_src/custom_response/tutorial010.py hl[2,4] *}
{* ../../docs_src/custom_response/tutorial010_py39.py hl[2,4] *}
/// tip

0
docs_src/custom_response/tutorial001.py → docs_src/custom_response/tutorial001_py39.py

0
docs_src/custom_response/tutorial001b.py → docs_src/custom_response/tutorial001b_py39.py

0
docs_src/custom_response/tutorial002.py → docs_src/custom_response/tutorial002_py39.py

0
docs_src/custom_response/tutorial003.py → docs_src/custom_response/tutorial003_py39.py

0
docs_src/custom_response/tutorial004.py → docs_src/custom_response/tutorial004_py39.py

0
docs_src/custom_response/tutorial005.py → docs_src/custom_response/tutorial005_py39.py

0
docs_src/custom_response/tutorial006.py → docs_src/custom_response/tutorial006_py39.py

0
docs_src/custom_response/tutorial006b.py → docs_src/custom_response/tutorial006b_py39.py

0
docs_src/custom_response/tutorial006c.py → docs_src/custom_response/tutorial006c_py39.py

0
docs_src/custom_response/tutorial007.py → docs_src/custom_response/tutorial007_py39.py

0
docs_src/custom_response/tutorial008.py → docs_src/custom_response/tutorial008_py39.py

0
docs_src/custom_response/tutorial009.py → docs_src/custom_response/tutorial009_py39.py

0
docs_src/custom_response/tutorial009b.py → docs_src/custom_response/tutorial009b_py39.py

0
docs_src/custom_response/tutorial009c.py → docs_src/custom_response/tutorial009c_py39.py

0
docs_src/custom_response/tutorial010.py → docs_src/custom_response/tutorial010_py39.py

2
tests/test_tutorial/test_custom_response/test_tutorial001.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial001 import app
from docs_src.custom_response.tutorial001_py39 import app
client = TestClient(app)

2
tests/test_tutorial/test_custom_response/test_tutorial001b.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial001b import app
from docs_src.custom_response.tutorial001b_py39 import app
client = TestClient(app)

2
tests/test_tutorial/test_custom_response/test_tutorial004.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial004 import app
from docs_src.custom_response.tutorial004_py39 import app
client = TestClient(app)

2
tests/test_tutorial/test_custom_response/test_tutorial005.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial005 import app
from docs_src.custom_response.tutorial005_py39 import app
client = TestClient(app)

2
tests/test_tutorial/test_custom_response/test_tutorial006.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial006 import app
from docs_src.custom_response.tutorial006_py39 import app
client = TestClient(app)

2
tests/test_tutorial/test_custom_response/test_tutorial006b.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial006b import app
from docs_src.custom_response.tutorial006b_py39 import app
client = TestClient(app)

2
tests/test_tutorial/test_custom_response/test_tutorial006c.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial006c import app
from docs_src.custom_response.tutorial006c_py39 import app
client = TestClient(app)

2
tests/test_tutorial/test_custom_response/test_tutorial007.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial007 import app
from docs_src.custom_response.tutorial007_py39 import app
client = TestClient(app)

6
tests/test_tutorial/test_custom_response/test_tutorial008.py

@ -2,15 +2,15 @@ from pathlib import Path
from fastapi.testclient import TestClient
from docs_src.custom_response import tutorial008
from docs_src.custom_response.tutorial008 import app
from docs_src.custom_response import tutorial008_py39
from docs_src.custom_response.tutorial008_py39 import app
client = TestClient(app)
def test_get(tmp_path: Path):
file_path: Path = tmp_path / "large-video-file.mp4"
tutorial008.some_file_path = str(file_path)
tutorial008_py39.some_file_path = str(file_path)
test_content = b"Fake video bytes"
file_path.write_bytes(test_content)
response = client.get("/")

6
tests/test_tutorial/test_custom_response/test_tutorial009.py

@ -2,15 +2,15 @@ from pathlib import Path
from fastapi.testclient import TestClient
from docs_src.custom_response import tutorial009
from docs_src.custom_response.tutorial009 import app
from docs_src.custom_response import tutorial009_py39
from docs_src.custom_response.tutorial009_py39 import app
client = TestClient(app)
def test_get(tmp_path: Path):
file_path: Path = tmp_path / "large-video-file.mp4"
tutorial009.some_file_path = str(file_path)
tutorial009_py39.some_file_path = str(file_path)
test_content = b"Fake video bytes"
file_path.write_bytes(test_content)
response = client.get("/")

6
tests/test_tutorial/test_custom_response/test_tutorial009b.py

@ -2,15 +2,15 @@ from pathlib import Path
from fastapi.testclient import TestClient
from docs_src.custom_response import tutorial009b
from docs_src.custom_response.tutorial009b import app
from docs_src.custom_response import tutorial009b_py39
from docs_src.custom_response.tutorial009b_py39 import app
client = TestClient(app)
def test_get(tmp_path: Path):
file_path: Path = tmp_path / "large-video-file.mp4"
tutorial009b.some_file_path = str(file_path)
tutorial009b_py39.some_file_path = str(file_path)
test_content = b"Fake video bytes"
file_path.write_bytes(test_content)
response = client.get("/")

2
tests/test_tutorial/test_custom_response/test_tutorial009c.py

@ -1,6 +1,6 @@
from fastapi.testclient import TestClient
from docs_src.custom_response.tutorial009c import app
from docs_src.custom_response.tutorial009c_py39 import app
client = TestClient(app)

Loading…
Cancel
Save