Browse Source

๐ŸŒ Add Korean translation for `docs/ko/docs/tutorial/dependencies/index.md` (#10989)

pull/11038/head
Kani Kim 1 year ago
committed by GitHub
parent
commit
00395f3eeb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 353
      docs/ko/docs/tutorial/dependencies/index.md

353
docs/ko/docs/tutorial/dependencies/index.md

@ -0,0 +1,353 @@
# ์˜์กด์„ฑ
**FastAPI**๋Š” ์•„์ฃผ ๊ฐ•๋ ฅํ•˜์ง€๋งŒ ์ง๊ด€์ ์ธ **<abbr title="์ปดํฌ๋„ŒํŠธ, ์ž์›, ์ œ๊ณต์ž, ์„œ๋น„์Šค, ์ธ์ ํ„ฐ๋ธ”๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค">์˜์กด์„ฑ ์ฃผ์ž…</abbr>** ์‹œ์Šคํ…œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ์•„์ฃผ ์‰ฝ๊ฒŒ ์„ค๊ณ„ํ–ˆ์œผ๋ฉฐ, ์–ด๋Š ๊ฐœ๋ฐœ์ž๋‚˜ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์™€ **FastAPI**๋ฅผ ์‰ฝ๊ฒŒ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
## "์˜์กด์„ฑ ์ฃผ์ž…"์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
**"์˜์กด์„ฑ ์ฃผ์ž…"**์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ(์ด ๊ฒฝ์šฐ, ๊ฒฝ๋กœ ๋™์ž‘ ํ•จ์ˆ˜)๊ฐ€ ์ž‘๋™ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฒƒ, ์ฆ‰ "์˜์กด์„ฑ"์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
๊ทธ ํ›„์—, ์‹œ์Šคํ…œ(์ด ๊ฒฝ์šฐ FastAPI)์€ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ์˜์กด์„ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ชจ๋“  ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.(์˜์กด์„ฑ์„ "์ฃผ์ž…"ํ•ฉ๋‹ˆ๋‹ค)
์ด๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‚ฌํ•ญ์„ ํ•„์š”๋กœ ํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค:
* ๊ณต์šฉ๋œ ๋กœ์ง์„ ๊ฐ€์กŒ์„ ๊ฒฝ์šฐ (๊ฐ™์€ ์ฝ”๋“œ ๋กœ์ง์ด ๊ณ„์† ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ).
* ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ์„ ๊ณต์œ ํ•˜๋Š” ๊ฒฝ์šฐ.
* ๋ณด์•ˆ, ์ธ์ฆ, ์—ญํ•  ์š”๊ตฌ ์‚ฌํ•ญ ๋“ฑ์„ ๊ฐ•์ œํ•˜๋Š” ๊ฒฝ์šฐ.
* ๊ทธ๋ฆฌ๊ณ  ๋งŽ์€ ๋‹ค๋ฅธ ์‚ฌํ•ญ...
์ด ๋ชจ๋“  ์‚ฌํ•ญ์„ ํ•  ๋•Œ ์ฝ”๋“œ ๋ฐ˜๋ณต์„ ์ตœ์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค.
## ์ฒซ๋ฒˆ์งธ ๋‹จ๊ณ„
์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋ด…์‹œ๋‹ค. ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•  ๊ฒƒ์ด๊ธฐ์— ์ง€๊ธˆ ๋‹น์žฅ์€ ์œ ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์ด๋ฅผ ํ†ตํ•ด **์˜์กด์„ฑ ์ฃผ์ž…** ์‹œ์Šคํ…œ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€์— ์ค‘์ ์„ ๋‘˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
### ์˜์กด์„ฑ ํ˜น์€ "๋””ํŽœ๋”๋ธ”" ๋งŒ๋“ค๊ธฐ
์˜์กด์„ฑ์— ์ง‘์ค‘ํ•ด ๋ด…์‹œ๋‹ค.
*๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๋Š” ๋‹จ์ˆœํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค:
=== "Python 3.10+"
```Python hl_lines="8-9"
{!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
=== "Python 3.9+"
```Python hl_lines="8-11"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
=== "Python 3.8+"
```Python hl_lines="9-12"
{!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
=== "Python 3.10+ Annotated๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ"
!!! ํŒ
๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด `Annotated`๊ฐ€ ๋‹ฌ๋ฆฐ ๋ฒ„์ „์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
```Python hl_lines="6-7"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
=== "Python 3.8+ Annotated๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ"
!!! ํŒ
๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด `Annotated`๊ฐ€ ๋‹ฌ๋ฆฐ ๋ฒ„์ „์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
```Python hl_lines="8-11"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
์ด๊ฒŒ ๋‹ค์ž…๋‹ˆ๋‹ค.
**๋‹จ ๋‘ ์ค„์ž…๋‹ˆ๋‹ค**.
๊ทธ๋ฆฌ๊ณ , ์ด ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ชจ๋“  *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์™€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์—ฌ๋Ÿฌ๋ถ„์€ ์ด๋ฅผ "๋ฐ์ฝ”๋ ˆ์ดํ„ฐ"๊ฐ€ ์—†๋Š” (`@app.get("/some-path")`๊ฐ€ ์—†๋Š”) *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์—ฌ๋Ÿฌ๋ถ„์ด ์›ํ•˜๋Š” ๋ฌด์—‡์ด๋“  ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด ๊ฒฝ์šฐ, ์ด ์˜์กด์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค:
* ์„ ํƒ์ ์ธ ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜ `q`, `str`์„ ์ž๋ฃŒํ˜•์œผ๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
* ์„ ํƒ์ ์ธ ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜ `skip`, `int`๋ฅผ ์ž๋ฃŒํ˜•์œผ๋กœ ๊ฐ€์ง€๋ฉฐ ๊ธฐ๋ณธ ๊ฐ’์€ `0`์ž…๋‹ˆ๋‹ค.
* ์„ ํƒ์ ์ธ ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜ `limit`,`int`๋ฅผ ์ž๋ฃŒํ˜•์œผ๋กœ ๊ฐ€์ง€๋ฉฐ ๊ธฐ๋ณธ ๊ฐ’์€ `100`์ž…๋‹ˆ๋‹ค.
๊ทธ ํ›„ ์œ„์˜ ๊ฐ’์„ ํฌํ•จํ•œ `dict` ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ๋ฟ์ž…๋‹ˆ๋‹ค.
!!! ์ •๋ณด
FastAPI๋Š” 0.95.0 ๋ฒ„์ „๋ถ€ํ„ฐ `Annotated`์— ๋Œ€ํ•œ ์ง€์›์„ (๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค) ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
์˜›๋‚  ๋ฒ„์ „์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ, `Annotated`๋ฅผ ์‚ฌ์šฉํ•˜๋ ค ํ•˜๋ฉด ์—๋Ÿฌ๋ฅผ ๋งž์ดํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
`Annotated`๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์ตœ์†Œ 0.95.1๋กœ [FastAPI ๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ](../../deployment/versions.md#upgrading-the-fastapi-versions){.internal-link target=_blank}๋ฅผ ํ™•์‹คํ•˜๊ฒŒ ํ•˜์„ธ์š”.
### `Depends` ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
=== "Python 3.10+"
```Python hl_lines="3"
{!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
=== "Python 3.9+"
```Python hl_lines="3"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
=== "Python 3.8+"
```Python hl_lines="3"
{!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
=== "Python 3.10+ Annotated๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ"
!!! ํŒ
๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด `Annotated`๊ฐ€ ๋‹ฌ๋ฆฐ ๋ฒ„์ „์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
```Python hl_lines="1"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
=== "Python 3.8+ Annotated๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ"
!!! ํŒ
๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด `Annotated`๊ฐ€ ๋‹ฌ๋ฆฐ ๋ฒ„์ „์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
```Python hl_lines="3"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
### "์˜์กด์ž"์— ์˜์กด์„ฑ ๋ช…์‹œํ•˜๊ธฐ
*๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ `Body`, `Query` ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๊ฐ™์ด ์ƒˆ๋กœ์šด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ `Depends`๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค:
=== "Python 3.10+"
```Python hl_lines="13 18"
{!> ../../../docs_src/dependencies/tutorial001_an_py310.py!}
```
=== "Python 3.9+"
```Python hl_lines="15 20"
{!> ../../../docs_src/dependencies/tutorial001_an_py39.py!}
```
=== "Python 3.8+"
```Python hl_lines="16 21"
{!> ../../../docs_src/dependencies/tutorial001_an.py!}
```
=== "Python 3.10+ Annotated๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ"
!!! ํŒ
๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด `Annotated`๊ฐ€ ๋‹ฌ๋ฆฐ ๋ฒ„์ „์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
```Python hl_lines="11 16"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
=== "Python 3.8+ Annotated๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ"
!!! ํŒ
๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด `Annotated`๊ฐ€ ๋‹ฌ๋ฆฐ ๋ฒ„์ „์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
```Python hl_lines="15 20"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
๋น„๋ก `Body`, `Query` ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์žˆ๋Š” `Depends`๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, `Depends`๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
`Depends`์— ๋‹จ์ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์ „๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค.
์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ™์€ ๊ฒƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๋Ÿฌ๋ถ„์€ ์ง์ ‘ **ํ˜ธ์ถœํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค** (๋์— ๊ด„ํ˜ธ๋ฅผ ์น˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค), ๋‹จ์ง€ `Depends()`์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ ์คฌ์„ ๋ฟ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ•จ์ˆ˜๋Š” *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.
!!! ํŒ
์—ฌ๋Ÿฌ๋ถ„์€ ๋‹ค์Œ ์žฅ์—์„œ ํ•จ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ์„œ, "๋‹ค๋ฅธ ๊ฒƒ๋“ค"์ด ์–ด๋–ป๊ฒŒ ์˜์กด์„ฑ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š”์ง€ ์•Œ๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ƒˆ๋กœ์šด ์š”์ฒญ์ด ๋„์ฐฉํ•  ๋•Œ๋งˆ๋‹ค, **FastAPI**๋Š” ๋‹ค์Œ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค:
* ์˜ฌ๋ฐ”๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ์˜์กด์„ฑ("๋””ํŽœ๋”๋ธ”") ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
* ํ•จ์ˆ˜์—์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค.
* *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*์— ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค
```mermaid
graph TB
common_parameters(["common_parameters"])
read_items["/items/"]
read_users["/users/"]
common_parameters --> read_items
common_parameters --> read_users
```
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ณต์šฉ ์ฝ”๋“œ๋ฅผ ํ•œ๋ฒˆ๋งŒ ์ ์–ด๋„ ๋˜๋ฉฐ, **FastAPI**๋Š” *๊ฒฝ๋กœ ์ž‘๋™*์„ ์œ„ํ•ด ์ด์— ๋Œ€ํ•œ ํ˜ธ์ถœ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
!!! ํ™•์ธ
ํŠน๋ณ„ํ•œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋˜๋ฉฐ, ์ด๋Ÿฌํ•œ ๊ฒƒ ํ˜น์€ ๋น„์Šทํ•œ ์ข…๋ฅ˜๋ฅผ **FastAPI**์— "๋“ฑ๋ก"ํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๊ณณ์— ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.
๋‹จ์ˆœํžˆ `Depends`์— ๋„˜๊ฒจ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋ฉฐ, **FastAPI**๋Š” ๋‚˜๋จธ์ง€๋ฅผ ์–ด์ฐŒํ• ์ง€ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
## `Annotated`์ธ ์˜์กด์„ฑ ๊ณต์œ ํ•˜๊ธฐ
์œ„์˜ ์˜ˆ์ œ์—์„œ ๋ช‡๋ช‡ ์ž‘์€ **์ฝ”๋“œ ์ค‘๋ณต**์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์•˜์„ ๊ฒ๋‹ˆ๋‹ค.
`common_parameters()`์˜์กด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ํƒ€์ž… ๋ช…์‹œ์™€ `Depends()`์™€ ํ•จ๊ป˜ ์ „์ฒด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:
```Python
commons: Annotated[dict, Depends(common_parameters)]
```
ํ•˜์ง€๋งŒ `Annotated`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ์—, `Annotated` ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ  ์—ฌ๋Ÿฌ ์žฅ์†Œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
=== "Python 3.10+"
```Python hl_lines="12 16 21"
{!> ../../../docs_src/dependencies/tutorial001_02_an_py310.py!}
```
=== "Python 3.9+"
```Python hl_lines="14 18 23"
{!> ../../../docs_src/dependencies/tutorial001_02_an_py39.py!}
```
=== "Python 3.8+"
```Python hl_lines="15 19 24"
{!> ../../../docs_src/dependencies/tutorial001_02_an.py!}
```
!!! ํŒ
์ด๋Š” ๊ทธ์ € ํ‘œ์ค€ ํŒŒ์ด์ฌ์ด๊ณ  "type alias"๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ์‚ฌ์‹ค **FastAPI**์— ๊ตญํ•œ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, `Annotated`๋ฅผ ํฌํ•จํ•˜์—ฌ, **FastAPI**๊ฐ€ ํŒŒ์ด์ฌ ํ‘œ์ค€์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ๊ธฐ์—, ์ด๋ฅผ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ ํŠธ๋ฆญ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Ž
์ด ์˜์กด์„ฑ์€ ๊ณ„์†ํ•ด์„œ ์˜ˆ์ƒํ•œ๋Œ€๋กœ ์ž‘๋™ํ•  ๊ฒƒ์ด๋ฉฐ, **์ œ์ผ ์ข‹์€ ๋ถ€๋ถ„**์€ **ํƒ€์ž… ์ •๋ณด๊ฐ€ ๋ณด์กด๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค**. ์ฆ‰ ์—ฌ๋Ÿฌ๋ถ„์˜ ํŽธ์ง‘๊ธฐ๊ฐ€ **์ž๋™ ์™„์„ฑ**, **์ธ๋ผ์ธ ์—๋Ÿฌ** ๋“ฑ์„ ๊ณ„์†ํ•ด์„œ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. `mypy`๊ฐ™์€ ๋‹ค๋ฅธ ๋„๊ตฌ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.
์ด๋Š” ํŠนํžˆ **๋งŽ์€ *๊ฒฝ๋กœ ์ž‘๋™***์—์„œ **๊ฐ™์€ ์˜์กด์„ฑ**์„ ๊ณ„์†ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” **๊ฑฐ๋Œ€ ์ฝ”๋“œ ๊ธฐ๋ฐ˜**์•ˆ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
## `async`ํ•˜๊ฒŒ, ํ˜น์€ `async`ํ•˜์ง€ ์•Š๊ฒŒ
์˜์กด์„ฑ์ด (*๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*์—์„œ ์ฒ˜๋Ÿผ ๋˜‘๊ฐ™์ด) **FastAPI**์— ์˜ํ•ด ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๋™์ผํ•œ ๊ทœ์น™์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
`async def`์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ˜น์€ ์ผ๋ฐ˜์ ์ธ `def`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ผ๋ฐ˜์ ์ธ `def` *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜* ์•ˆ์— `async def`๋กœ ์˜์กด์„ฑ์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, `async def` *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜* ์•ˆ์— `def`๋กœ ์˜์กด์„ฑ์„ ์„ ์–ธํ•˜๋Š” ๋“ฑ์˜ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
์•„๋ฌด ๋ฌธ์ œ ์—†์Šต๋‹ˆ๋‹ค. **FastAPI**๋Š” ๋ฌด์—‡์„ ํ• ์ง€ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
!!! ์ฐธ๊ณ 
์ž˜ ๋ชจ๋ฅด์‹œ๊ฒ ๋‹ค๋ฉด, [Async: *"In a hurry?"*](../../async.md){.internal-link target=_blank} ๋ฌธ์„œ์—์„œ `async`์™€ `await`์— ๋Œ€ํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
## OpenAPI์™€ ํ†ตํ•ฉ
๋ชจ๋“  ์š”์ฒญ ์„ ์–ธ, ๊ฒ€์ฆ๊ณผ ์˜์กด์„ฑ(๋ฐ ํ•˜์œ„ ์˜์กด์„ฑ)์— ๋Œ€ํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์€ ๋™์ผํ•œ OpenAPI ์Šคํ‚ค๋งˆ์— ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ๋Œ€ํ™”ํ˜• ๋ฌธ์„œ์— ์ด๋Ÿฌํ•œ ์˜์กด์„ฑ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ •๋ณด ์—ญ์‹œ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค:
<img src="/img/tutorial/dependencies/image01.png">
## ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฒ•
์ด๋ฅผ ๋ณด๋ฉด, *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*๋Š” *๊ฒฝ๋กœ*์™€ *์ž‘๋™*์ด ๋งค์นญ๋˜๋ฉด ์–ธ์ œ๋“ ์ง€ ์‚ฌ์šฉ๋˜๋„๋ก ์ •์˜๋˜์—ˆ์œผ๋ฉฐ, **FastAPI**๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํ•ด๋‹น ์š”์ฒญ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
์‚ฌ์‹ค, ๋ชจ๋“  (ํ˜น์€ ๋Œ€๋ถ€๋ถ„์˜) ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๋Ÿฌ๋ถ„์€ ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋“ค์„ ์ ˆ๋Œ€ ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ”„๋ ˆ์ž„์›Œํฌ(์ด ๊ฒฝ์šฐ **FastAPI**)์— ์˜ํ•ด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
์˜์กด์„ฑ ์ฃผ์ž… ์‹œ์Šคํ…œ๊ณผ ํ•จ๊ป˜๋ผ๋ฉด **FastAPI**์—๊ฒŒ ์—ฌ๋Ÿฌ๋ถ„์˜ *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๋ฌด์–ธ๊ฐ€์— ์—ฌ๋Ÿฌ๋ถ„์˜ *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜* ๋˜ํ•œ "์˜์กด"ํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉฐ, **FastAPI**๋Š” ์ด๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ "์ฃผ์ž…"ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
"์˜์กด์„ฑ ์ฃผ์ž…"์ด๋ผ๋Š” ๋™์ผํ•œ ์•„์ด๋””์–ด์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ์ผ๋ฐ˜์ ์ธ ์šฉ์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
* ๋ฆฌ์†Œ์Šค
* ์ œ๊ณต์ž
* ์„œ๋น„์Šค
* ์ธ์ ํ„ฐ๋ธ”
* ์ปดํฌ๋„ŒํŠธ
## **FastAPI** ํ”Œ๋Ÿฌ๊ทธ์ธ
ํ†ตํ•ฉ๊ณผ "ํ”Œ๋Ÿฌ๊ทธ์ธ"์€ **์˜์กด์„ฑ ์ฃผ์ž…** ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ **"ํ”Œ๋Ÿฌ๊ทธ์ธ"์„ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค**, ์™œ๋ƒํ•˜๋ฉด ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์—ฌ๋Ÿฌ๋ถ„์˜ *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*์— ํ†ตํ•ฉ๊ณผ ์ƒํ˜ธ ์ž‘์šฉ์„ ๋ฌดํ•œ๋Œ€๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  "๋ง ๊ทธ๋Œ€๋กœ", ๊ทธ์ € ํ•„์š”๋กœ ํ•˜๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์ž„ํฌํŠธํ•˜๊ณ  ๋‹จ ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋กœ ์—ฌ๋Ÿฌ๋ถ„์˜ API ํ•จ์ˆ˜์™€ ํ†ตํ•ฉํ•จ์œผ๋กœ์จ, ์˜์กด์„ฑ์„ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ณ  ์ง๊ด€์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ด€๊ณ„ํ˜• ๋ฐ NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ๋ณด์•ˆ ๋“ฑ, ์ด์— ๋Œ€ํ•œ ์˜ˆ์‹œ๋ฅผ ๋‹ค์Œ ์žฅ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
## **FastAPI** ํ˜ธํ™˜์„ฑ
์˜์กด์„ฑ ์ฃผ์ž… ์‹œ์Šคํ…œ์˜ ๋‹จ์ˆœํ•จ์€ **FastAPI**๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์š”์†Œ๋“ค๊ณผ ํ˜ธํ™˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค:
* ๋ชจ๋“  ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
* NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
* ์™ธ๋ถ€ ํŒจํ‚ค์ง€
* ์™ธ๋ถ€ API
* ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๋ถ€์—ฌ ์‹œ์Šคํ…œ
* API ์‚ฌ์šฉ ๋ชจ๋‹ˆํ„ฐ๋ง ์‹œ์Šคํ…œ
* ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ฃผ์ž… ์‹œ์Šคํ…œ
* ๊ธฐํƒ€ ๋“ฑ๋“ฑ.
## ๊ฐ„ํŽธํ•˜๊ณ  ๊ฐ•๋ ฅํ•˜๋‹ค
๊ณ„์ธต์ ์ธ ์˜์กด์„ฑ ์ฃผ์ž… ์‹œ์Šคํ…œ์€ ์ •์˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ์ง€๋งŒ, ์—ฌ์ „ํžˆ ๋งค์šฐ ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๋Ÿฌ๋ถ„์€ ์Šค์Šค๋กœ๋ฅผ ์˜์กดํ•˜๋Š” ์˜์กด์„ฑ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋์—๋Š”, ๊ณ„์ธต์ ์ธ ๋‚˜๋ฌด๋กœ ๋œ ์˜์กด์„ฑ์ด ๋งŒ๋“ค์–ด์ง€๋ฉฐ, ๊ทธ๋ฆฌ๊ณ  **์˜์กด์„ฑ ์ฃผ์ž…** ์‹œ์Šคํ…œ์€ (ํ•˜์œ„ ์˜์กด์„ฑ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ) ์ด๋Ÿฌํ•œ ์˜์กด์„ฑ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค(์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค).
์˜ˆ๋ฅผ ๋“ค๋ฉด, ์—ฌ๋Ÿฌ๋ถ„์ด 4๊ฐœ์˜ API ์—”๋“œํฌ์ธํŠธ(*๊ฒฝ๋กœ ์ž‘๋™*)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค:
* `/items/public/`
* `/items/private/`
* `/users/{user_id}/activate`
* `/items/pro/`
๊ทธ ๋‹ค์Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๊ทธ์ € ์˜์กด์„ฑ๊ณผ ํ•˜์œ„ ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ๊ถŒํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค:
```mermaid
graph TB
current_user(["current_user"])
active_user(["active_user"])
admin_user(["admin_user"])
paying_user(["paying_user"])
public["/items/public/"]
private["/items/private/"]
activate_user["/users/{user_id}/activate"]
pro_items["/items/pro/"]
current_user --> active_user
active_user --> admin_user
active_user --> paying_user
current_user --> public
active_user --> private
admin_user --> activate_user
paying_user --> pro_items
```
## **OpenAPI**์™€์˜ ํ†ตํ•ฉ
์ด ๋ชจ๋“  ์˜์กด์„ฑ์€ ๊ฐ๊ฐ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์„ ์–ธํ•˜๋Š” ๋™์‹œ์—, *๊ฒฝ๋กœ ์ž‘๋™*์— ๋งค๊ฐœ๋ณ€์ˆ˜, ๊ฒ€์ฆ ๋“ฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
**FastAPI**๋Š” ์ด ๋ชจ๋“  ๊ฒƒ์„ OpenAPI ์Šคํ‚ค๋งˆ์— ์ถ”๊ฐ€ํ•  ๊ฒƒ์ด๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋Œ€ํ™”ํ˜• ๋ฌธ์„œ ์‹œ์Šคํ…œ์— ๋‚˜ํƒ€๋‚  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
Loadingโ€ฆ
Cancel
Save