Browse Source
Co-authored-by: Nina Hwang <[email protected]> Co-authored-by: Joona Yoon <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>pull/9394/head
committed by
GitHub
2 changed files with 246 additions and 0 deletions
@ -0,0 +1,244 @@ |
|||||
|
# ์์กด์ฑ์ผ๋ก์์ ํด๋์ค |
||||
|
|
||||
|
**์์กด์ฑ ์ฃผ์
** ์์คํ
์ ๋ํด ์์ธํ ์ดํด๋ณด๊ธฐ ์ ์ ์ด์ ์์ ๋ฅผ ์
๊ทธ๋ ์ด๋ ํด๋ณด๊ฒ ์ต๋๋ค. |
||||
|
|
||||
|
## ์ด์ ์์ ์ `๋์
๋๋ฆฌ` |
||||
|
|
||||
|
์ด์ ์์ ์์, ์ฐ๋ฆฌ๋ ์์กด์ฑ(์์กด ๊ฐ๋ฅํ) ํจ์์์ `๋์
๋๋ฆฌ`๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ณ ์์์ต๋๋ค: |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.6 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="9" |
||||
|
{!> ../../../docs_src/dependencies/tutorial001.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.10 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="7" |
||||
|
{!> ../../../docs_src/dependencies/tutorial001_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
์ฐ๋ฆฌ๋ *๊ฒฝ๋ก ์๋ ํจ์*์ ๋งค๊ฐ๋ณ์ `commons`์์ `๋์
๋๋ฆฌ` ๊ฐ์ฒด๋ฅผ ์ป์ต๋๋ค. |
||||
|
|
||||
|
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ ์๋ํฐ๋ค์ด `๋์
๋๋ฆฌ` ๊ฐ์ฒด์ ํค๋ ๋ฐธ๋ฅ์ ์๋ฃํ์ ์ ์ ์๊ธฐ ๋๋ฌธ์ ์๋ ์์ฑ๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ ๊ณตํด ์ค ์ ์๋ค๋ ๊ฒ์ ์๊ณ ์์ต๋๋ค. |
||||
|
|
||||
|
๋ ๋์ ๋ฐฉ๋ฒ์ด ์์ ๊ฒ ๊ฐ์ต๋๋ค... |
||||
|
|
||||
|
## ์์กด์ฑ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฒ |
||||
|
|
||||
|
์ง๊ธ๊น์ง ํจ์๋ก ์ ์ธ๋ ์์กด์ฑ์ ๋ด์์ต๋๋ค. |
||||
|
|
||||
|
์๋ง๋ ๋ ์ผ๋ฐ์ ์ด๊ธฐ๋ ํ๊ฒ ์ง๋ง ์์กด์ฑ์ ์ ์ธํ๋ ์ ์ผํ ๋ฐฉ๋ฒ์ ์๋๋๋ค. |
||||
|
|
||||
|
ํต์ฌ ์์๋ ์์กด์ฑ์ด "ํธ์ถ ๊ฐ๋ฅ"ํด์ผ ํ๋ค๋ ๊ฒ์
๋๋ค |
||||
|
|
||||
|
ํ์ด์ฌ์์์ "**ํธ์ถ ๊ฐ๋ฅ**"์ ํ์ด์ฌ์ด ํจ์์ฒ๋ผ "ํธ์ถ"ํ ์ ์๋ ๋ชจ๋ ๊ฒ์
๋๋ค. |
||||
|
|
||||
|
๋ฐ๋ผ์, ๋ง์ฝ ๋น์ ์ด `something`(ํจ์๊ฐ ์๋ ์๋ ์์) ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , |
||||
|
|
||||
|
```Python |
||||
|
something() |
||||
|
``` |
||||
|
|
||||
|
๋๋ |
||||
|
|
||||
|
```Python |
||||
|
something(some_argument, some_keyword_argument="foo") |
||||
|
``` |
||||
|
|
||||
|
์๊ธฐ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก "ํธ์ถ(์คํ)" ํ ์ ์๋ค๋ฉด "ํธ์ถ ๊ฐ๋ฅ"์ด ๋ฉ๋๋ค. |
||||
|
|
||||
|
## ์์กด์ฑ์ผ๋ก์์ ํด๋์ค |
||||
|
|
||||
|
ํ์ด์ฌ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋์ผํ ๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ค๋ ๊ฑธ ์ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
์๋ฅผ ๋ค์ด: |
||||
|
|
||||
|
```Python |
||||
|
class Cat: |
||||
|
def __init__(self, name: str): |
||||
|
self.name = name |
||||
|
|
||||
|
|
||||
|
fluffy = Cat(name="Mr Fluffy") |
||||
|
``` |
||||
|
|
||||
|
์ด ๊ฒฝ์ฐ์ `fluffy`๋ ํด๋์ค `Cat`์ ์ธ์คํด์ค์
๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ `fluffy`๋ฅผ ๋ง๋ค๊ธฐ ์ํด์ `Cat`์ "ํธ์ถ"ํ์ต๋๋ค. |
||||
|
|
||||
|
๋ฐ๋ผ์, ํ์ด์ฌ ํด๋์ค๋ **ํธ์ถ ๊ฐ๋ฅ**ํฉ๋๋ค. |
||||
|
|
||||
|
๊ทธ๋์ **FastAPI**์์๋ ํ์ด์ฌ ํด๋์ค๋ฅผ ์์กด์ฑ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
FastAPI๊ฐ ์ค์ง์ ์ผ๋ก ํ์ธํ๋ ๊ฒ์ "ํธ์ถ ๊ฐ๋ฅ์ฑ"(ํจ์, ํด๋์ค ๋๋ ๋ค๋ฅธ ๋ชจ๋ ๊ฒ)๊ณผ ์ ์๋ ๋งค๊ฐ๋ณ์๋ค์
๋๋ค. |
||||
|
|
||||
|
"ํธ์ถ ๊ฐ๋ฅ"ํ ๊ฒ์ ์์กด์ฑ์ผ๋ก์ **FastAPI**์ ์ ๋ฌํ๋ฉด, ๊ทธ "ํธ์ถ ๊ฐ๋ฅ"ํ ๊ฒ์ ๋งค๊ฐ๋ณ์๋ค์ ๋ถ์ํ ํ ์ด๋ฅผ *๊ฒฝ๋ก ๋์ ํจ์*๋ฅผ ์ํ ๋งค๊ฐ๋ณ์์ ๋์ผํ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค. ํ์-์์กด์ฑ ๋ํ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค. |
||||
|
|
||||
|
๋งค๊ฐ๋ณ์๊ฐ ์๋ "ํธ์ถ ๊ฐ๋ฅ"ํ ๊ฒ ์ญ์ ๋งค๊ฐ๋ณ์๊ฐ ์๋ *๊ฒฝ๋ก ๋์ ํจ์*์ ๋์ผํ ๋ฐฉ์์ผ๋ก ์ ์ฉ๋ฉ๋๋ค. |
||||
|
|
||||
|
๊ทธ๋์, ์ฐ๋ฆฌ๋ ์ ์์ ์์์ `common_paramenters` ์์กด์ฑ์ ํด๋์ค `CommonQueryParams`๋ก ๋ฐ๊ฟ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.6 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="11-15" |
||||
|
{!> ../../../docs_src/dependencies/tutorial002.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.10 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="9-13" |
||||
|
{!> ../../../docs_src/dependencies/tutorial002_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ `__init__` ๋ฉ์๋์ ์ฃผ๋ชฉํ๊ธฐ ๋ฐ๋๋๋ค: |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.6 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="12" |
||||
|
{!> ../../../docs_src/dependencies/tutorial002.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.10 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="10" |
||||
|
{!> ../../../docs_src/dependencies/tutorial002_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
...์ด์ `common_parameters`์ ๋์ผํ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง๋๋ค: |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.6 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="9" |
||||
|
{!> ../../../docs_src/dependencies/tutorial001.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.10 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="6" |
||||
|
{!> ../../../docs_src/dependencies/tutorial001_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
์ด ๋งค๊ฐ๋ณ์๋ค์ **FastAPI**๊ฐ ์์กด์ฑ์ "ํด๊ฒฐ"ํ๊ธฐ ์ํด ์ฌ์ฉํ ๊ฒ์
๋๋ค |
||||
|
|
||||
|
ํจ์์ ํด๋์ค ๋ ๊ฐ์ง ๋ฐฉ์ ๋ชจ๋, ์๋ ์์๋ฅผ ๊ฐ์ต๋๋ค: |
||||
|
|
||||
|
* `๋ฌธ์์ด`์ด๋ฉด์ ์ ํ์ฌํญ์ธ ์ฟผ๋ฆฌ ๋งค๊ฐ๋ณ์ `q`. |
||||
|
* ๊ธฐ๋ณธ๊ฐ์ด `0`์ด๋ฉด์ `์ ์ํ`์ธ ์ฟผ๋ฆฌ ๋งค๊ฐ๋ณ์ `skip` |
||||
|
* ๊ธฐ๋ณธ๊ฐ์ด `100`์ด๋ฉด์ `์ ์ํ`์ธ ์ฟผ๋ฆฌ ๋งค๊ฐ๋ณ์ `limit` |
||||
|
|
||||
|
๋ ๊ฐ์ง ๋ฐฉ์ ๋ชจ๋, ๋ฐ์ดํฐ๋ ๋ณํ, ๊ฒ์ฆ๋๊ณ OpenAPI ์คํค๋ง์ ๋ฌธ์ํ๋ฉ๋๋ค. |
||||
|
|
||||
|
## ์ฌ์ฉํด๋ด
์๋ค! |
||||
|
|
||||
|
์ด์ ์๋์ ํด๋์ค๋ฅผ ์ด์ฉํด์ ์์กด์ฑ์ ์ ์ํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.6 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="19" |
||||
|
{!> ../../../docs_src/dependencies/tutorial002.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.10 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="17" |
||||
|
{!> ../../../docs_src/dependencies/tutorial002_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
**FastAPI**๋ `CommonQueryParams` ํด๋์ค๋ฅผ ํธ์ถํฉ๋๋ค. ์ด๊ฒ์ ํด๋น ํด๋์ค์ "์ธ์คํด์ค"๋ฅผ ์์ฑํ๊ณ ๊ทธ ์ธ์คํด์ค๋ ํจ์์ ๋งค๊ฐ๋ณ์ `commons`๋ก ์ ๋ฌ๋ฉ๋๋ค. |
||||
|
|
||||
|
## ํ์
ํํ
vs `Depends` |
||||
|
|
||||
|
์ ์ฝ๋์์ `CommonQueryParams`๋ฅผ ๋ ๋ฒ ์์ฑํ ๋ฐฉ์์ ์ฃผ๋ชฉํ์ญ์์ค: |
||||
|
|
||||
|
```Python |
||||
|
commons: CommonQueryParams = Depends(CommonQueryParams) |
||||
|
``` |
||||
|
|
||||
|
๋ง์ง๋ง `CommonQueryParams` ๋ณ์๋ฅผ ๋ณด๋ฉด: |
||||
|
|
||||
|
```Python |
||||
|
... = Depends(CommonQueryParams) |
||||
|
``` |
||||
|
|
||||
|
... **FastAPI**๊ฐ ์ค์ ๋ก ์ด๋ค ๊ฒ์ด ์์กด์ฑ์ธ์ง ์๊ธฐ ์ํด์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์
๋๋ค. |
||||
|
FastAPI๋ ์ ์ธ๋ ๋งค๊ฐ๋ณ์๋ค์ ์ถ์ถํ ๊ฒ์ด๊ณ ์ค์ ๋ก ์ด ๋ณ์๋ค์ ํธ์ถํ ๊ฒ์
๋๋ค. |
||||
|
|
||||
|
--- |
||||
|
|
||||
|
์ด ๊ฒฝ์ฐ์, ์ฒซ๋ฒ์งธ `CommonQueryParams` ๋ณ์๋ฅผ ๋ณด๋ฉด: |
||||
|
|
||||
|
```Python |
||||
|
commons: CommonQueryParams ... |
||||
|
``` |
||||
|
|
||||
|
... **FastAPI**๋ `CommonQueryParams` ๋ณ์์ ์ด๋ ํ ํน๋ณํ ์๋ฏธ๋ ๋ถ์ฌํ์ง ์์ต๋๋ค. FastAPI๋ ์ด ๋ณ์๋ฅผ ๋ฐ์ดํฐ ๋ณํ, ๊ฒ์ฆ ๋ฑ์ ํ์ฉํ์ง ์์ต๋๋ค. (ํ์ฉํ๋ ค๋ฉด `= Depends(CommonQueryParams)`๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.) |
||||
|
|
||||
|
์ฌ์ค ์๋์ ๊ฐ์ด ์์ฑํด๋ ๋ฌด๊ดํฉ๋๋ค: |
||||
|
|
||||
|
```Python |
||||
|
commons = Depends(CommonQueryParams) |
||||
|
``` |
||||
|
|
||||
|
..์ ์ฒด์ ์ธ ์ฝ๋๋ ์๋์ ๊ฐ์ต๋๋ค: |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.6 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="19" |
||||
|
{!> ../../../docs_src/dependencies/tutorial003.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.10 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="17" |
||||
|
{!> ../../../docs_src/dependencies/tutorial003_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
๊ทธ๋ฌ๋ ์๋ฃํ์ ์ ์ธํ๋ฉด ์๋ํฐ๊ฐ ๋งค๊ฐ๋ณ์ `commons`๋ก ์ ๋ฌ๋ ๊ฒ์ด ๋ฌด์์ธ์ง ์๊ฒ ๋๊ณ , ์ด๋ฅผ ํตํด ์ฝ๋ ์์ฑ, ์๋ฃํ ํ์ธ ๋ฑ์ ๋์์ด ๋ ์ ์์ผ๋ฏ๋ก ๊ถ์ฅ๋ฉ๋๋ค. |
||||
|
|
||||
|
<!-- <img src="/img/tutorial/dependencies/image02.png"> --> |
||||
|
|
||||
|
## ์ฝ๋ ๋จ์ถ |
||||
|
|
||||
|
๊ทธ๋ฌ๋ ์ฌ๊ธฐ `CommonQueryParams`๋ฅผ ๋ ๋ฒ์ด๋ ์์ฑํ๋, ์ฝ๋ ๋ฐ๋ณต์ด ์๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
```Python |
||||
|
commons: CommonQueryParams = Depends(CommonQueryParams) |
||||
|
``` |
||||
|
|
||||
|
**FastAPI**๋ *ํนํ* ์์กด์ฑ์ด **FastAPI**๊ฐ ํด๋์ค ์์ฒด์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ธฐ ์ํด "ํธ์ถ"ํ๋ ํด๋์ค์ธ ๊ฒฝ์ฐ, ์กฐ๊ธ ๋ ์ฌ์ด ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. |
||||
|
|
||||
|
์ด๋ฌํ ํน์ ํ ๊ฒฝ์ฐ์๋ ์๋์ฒ๋ผ ์ฌ์ฉํ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
์ด๋ ๊ฒ ์ฐ๋ ๊ฒ ๋์ : |
||||
|
|
||||
|
```Python |
||||
|
commons: CommonQueryParams = Depends(CommonQueryParams) |
||||
|
``` |
||||
|
|
||||
|
...์ด๋ ๊ฒ ์ธ ์ ์์ต๋๋ค.: |
||||
|
|
||||
|
```Python |
||||
|
commons: CommonQueryParams = Depends() |
||||
|
``` |
||||
|
|
||||
|
์์กด์ฑ์ ๋งค๊ฐ๋ณ์์ ํ์
์ผ๋ก ์ ์ธํ๋ ๊ฒฝ์ฐ `Depends(CommonQueryParams)`์ฒ๋ผ ํด๋์ค ์ด๋ฆ ์ ์ฒด๋ฅผ *๋ค์* ์์ฑํ๋ ๋์ , ๋งค๊ฐ๋ณ์๋ฅผ ๋ฃ์ง ์์ `Depends()`์ ํํ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
์๋์ ๊ฐ์ ์์ ๊ฐ ์์ต๋๋ค: |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.6 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="19" |
||||
|
{!> ../../../docs_src/dependencies/tutorial004.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "ํ์ด์ฌ 3.10 ์ด์" |
||||
|
|
||||
|
```Python hl_lines="17" |
||||
|
{!> ../../../docs_src/dependencies/tutorial004_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
...์ด๋ ๊ฒ ์ฝ๋๋ฅผ ๋จ์ถํ์ฌ๋ **FastAPI**๋ ๋ฌด์์ ํด์ผํ๋์ง ์๊ณ ์์ต๋๋ค. |
||||
|
|
||||
|
!!! tip "ํ" |
||||
|
๋ง์ฝ ์ด๊ฒ์ด ๋์์ด ๋๊ธฐ๋ณด๋ค ๋ ํท๊ฐ๋ฆฌ๊ฒ ๋ง๋ ๋ค๋ฉด, ์์ด๋ฒ๋ฆฌ์ญ์์ค. ์ด๊ฒ์ด ๋ฐ๋์ ํ์ํ ๊ฒ์ ์๋๋๋ค. |
||||
|
|
||||
|
์ด๊ฒ์ ๋จ์ง ์์ฌ์ด ๋ฐฉ๋ฒ์ผ ๋ฟ์
๋๋ค. ์๋ํ๋ฉด **FastAPI**๋ ์ฝ๋ ๋ฐ๋ณต์ ์ต์ํํ ์ ์๋ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํ๊ธฐ ๋๋ฌธ์
๋๋ค. |
Loadingโฆ
Reference in new issue