API ๊ตฌํ์ด ๋๋ฌ๋ค๋ฉด ํ๋ก ํธ๋ก ๊ฐ API์ ๋ํ ์ ๋ณด๋ฅผ ๋๊ฒจ์ค์ผ ์์ ์ด ๊ฐ๋ฅํ๋ค.
์ด๊ฒ์ ์ฐ๋ฆฌ๋ Swagger๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ๋ค. ์ฅ๊ณ REST Framework์์ swagger๋ฅผ ํตํด API๋ฅผ ๋ฌธ์ํ ํด๋ณด์.
๋จผ์ drf-ysag์ ๋ํ ๋ฌธ์๋ ์๋์ ๋งํฌ๋ฅผ ํตํด ์ด๋ํ ์ ์๋ค. ์์ธํ ์ ๋ณด๋ฅผ ์ํ๋ค๋ฉด ๊ณต์ ๋ฌธ์๋ฅผ ํ์ฉํ์.
https://drf-yasg.readthedocs.io/en/stable/
install
๊ฐ์ํ๊ฒฝ์ ํ์ฑํ ํ๊ณ install ํ์.
pip install -U drf-yasg
settings.py
settings.py์ ๋ค์์ด ์ถ๊ฐ๋์ด์ผ ํ๋ค.
INSTALLED_APPS = [
...
'django.contrib.staticfiles', # required for serving swagger ui's css/js files
'drf_yasg',
...
]
urls.py
์ด 4๊ฐ์ง์ ์๋ํฌ์ธํธ๋ฅผ ์ถ๊ฐํ ๊ฒ์ด๋ค.
- A JSON view of your API specification at /swagger.json
- A YAML view of your API specification at /swagger.yaml
- A swagger-ui view of your API specification at /swagger/
- A ReDoc view of your API specification at /redoc/
ํ๋ก์ ํธ์ urls.py์ ๋ค์์ ์ถ๊ฐํ๋ค.
...
from django.urls import re_path
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
...
schema_view = get_schema_view(
openapi.Info(
title="Snippets API",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact@snippets.local"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
path('swagger<format>/', schema_view.without_ui(cache_timeout=0), name='schema-json'),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
...
]
ํ์ธ
runserver ํด์ฃผ๊ณ , /swagger/์ ์ ๊ทผํ๋ค.
๊ทธ๋ผ ๋ค์์ฒ๋ผ api๋ฅผ ํ์ธํ ์ ์๋ค.

'๐ฅProject > [Project] Threepark' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Threepark] 3. ๋ชจ๋ธ ์๋น - (1) Flask ์์ฑ (0) | 2024.05.25 |
---|---|
[Threepark] ๋ฐฑ์๋ ์๋ฒ์ ํต์ฌ ๊ธฐ๋ฅ ๊ตฌํ (0) | 2024.05.21 |
[Capstone Design] 3. ๋ฐฑ์๋ ๊ตฌํ - (7) DRF ๊ฐ๋ฐ | VIEW (0) | 2024.05.21 |
[Capstone Design] 3. ๋ฐฑ์๋ ๊ตฌํ - (6) DRF ๊ฐ๋ฐ | PERMISSION (0) | 2024.05.21 |
[Capstone Design] 3. ๋ฐฑ์๋ ๊ตฌํ - (5) DRF ๊ฐ๋ฐ | SERIALIZER (0) | 2024.05.21 |