From 1017bca3fdbe0fa0b04eec240a6eecf2d7f14697 Mon Sep 17 00:00:00 2001 From: Nimitha J <58389915+Nimitha-jagadeesha@users.noreply.github.com> Date: Sun, 27 Oct 2024 22:10:17 +0530 Subject: [PATCH] Update session-with-dependency.md --- .../fastapi/session-with-dependency.md | 436 +----------------- 1 file changed, 20 insertions(+), 416 deletions(-) diff --git a/docs/tutorial/fastapi/session-with-dependency.md b/docs/tutorial/fastapi/session-with-dependency.md index e148452dcb..087b1c8457 100644 --- a/docs/tutorial/fastapi/session-with-dependency.md +++ b/docs/tutorial/fastapi/session-with-dependency.md @@ -6,69 +6,11 @@ Before we keep adding things, let's change a bit how we get the session for each Up to now, we have been creating a session in each *path operation*, in a `with` block. -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/delete/tutorial001_py310.py[ln:48-55]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/delete/tutorial001_py39.py[ln:50-57]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/delete/tutorial001.py[ln:50-57]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/delete/tutorial001_py310.py ln[48:55] hl[5] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/delete/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/delete/tutorial001_py310.py *} That's perfectly fine, but in many use cases we would want to use FastAPI Dependencies, for example to **verify** that the client is **logged in** and get the **current user** before executing any other code in the *path operation*. @@ -82,69 +24,11 @@ A **FastAPI** dependency is very simple, it's just a function that returns a val It could use `yield` instead of `return`, and in that case **FastAPI** will make sure it executes all the code **after** the `yield`, once it is done with the request. -//// tab | Python 3.10+ - -```Python hl_lines="3-5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3-5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-5" -# Code above omitted 👆 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code below omitted 👇 -``` - -//// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[40:42] hl[3:5] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py *} ## Use the Dependency @@ -152,87 +36,18 @@ Now let's make FastAPI execute a dependency and get its value in the *path opera We import `Depends()` from `fastapi`. Then we use it in the *path operation function* in a **parameter**, the same way we declared parameters to get JSON bodies, path parameters, etc. -//// tab | Python 3.10+ - -```Python hl_lines="1 13" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-59]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="3 15" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3 15" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2] hl[1,13] *} -# Code here omitted 👈 +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[40:42] hl[1,13] *} -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-61]!} -# Code below omitted 👇 -``` +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[53:59] hl[1,13] *} //// /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py *} /// tip @@ -259,174 +74,29 @@ And because dependencies can use `yield`, FastAPI will make sure to run the code ## The `with` Block This means that in the main code of the *path operation function*, it will work equivalently to the previous version with the explicit `with` block. +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2] hl[14:18] *} -//// tab | Python 3.10+ +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[40:42] *} -```Python hl_lines="14-18" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-59]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="16-20" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="16-20" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// +{*./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[53:59] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py *} In fact, you could think that all that block of code inside of the `create_hero()` function is still inside a `with` block for the **session**, because this is more or less what's happening behind the scenes. But now, the `with` block is not explicitly in the function, but in the dependency above: -//// tab | Python 3.10+ - -```Python hl_lines="7-8" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-59]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="9-10" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-61]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9-10" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-61]!} +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2] hl[7:8] *} -# Code below omitted 👇 -``` +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[40:42] hl[7:8] *} -//// +{*./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[53:59] hl[7:8] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py *} We will see how this is very useful when testing the code later. ✅ @@ -442,81 +112,15 @@ session: Session = Depends(get_session) And then we remove the previous `with` block with the old **session**. -//// tab | Python 3.10+ - -```Python hl_lines="13 24 33 42 57" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:1-2]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:40-42]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py[ln:53-104]!} -``` - -//// - -//// tab | Python 3.9+ - -```Python hl_lines="15 26 35 44 59" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:1-4]!} - -# Code here omitted 👈 +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2] hl[13,24,33,42,57] *} -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:42-44]!} +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[40:42] hl[13,24,33,42,57] *} -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py[ln:55-106]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="15 26 35 44 59" -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:1-4]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:42-44]!} - -# Code here omitted 👈 - -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py[ln:55-106]!} -``` - -//// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[53:104] hl[13,24,33,42,57] *} /// details | 👀 Full file preview -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.9+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py *} ## Recap