์ค๋์ ์ฅ๊ณ REST Framework์์ swagger๋ฅผ ํตํด API๋ฅผ ๋ฌธ์ํ ํด๋ณด์.
๋จผ์ drf-ysag์ ๋ํ ๋ฌธ์๋ ์๋์ ๋งํฌ๋ฅผ ํตํด ์ด๋ํ ์ ์๋ค. ์์ธํ ์ ๋ณด๋ฅผ ์ํ๋ค๋ฉด ๊ณต์ ๋ฌธ์๋ฅผ ํ์ฉํ์.
https://drf-yasg.readthedocs.io/en/stable/
drf-yasg โ drf-yasg 1.21.7 documentation
ยฉ Copyright 2018, Cristi V. Revision db42d356.
drf-yasg.readthedocs.io
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/์ ์ ๊ทผํ๋ค.
ํ๋ก์ ํธ์ urls.py์ ๋ค์์ ์ถ๊ฐํ๋ค.
๊ทธ๋ผ ๋ค์์ฒ๋ผ api๋ฅผ ํ์ธํ ์ ์๋ค.

'๐ฅProject > Team22' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Team22]django .env ํ๊ฒฝ๋ณ์ ํ์ฉํ๊ธฐ (0) | 2024.02.15 |
---|---|
[Team 22] DRF - Mixins (0) | 2024.02.08 |
[Team 22] DRF - Generic API View (0) | 2024.02.08 |
[Team 22] DRF Authentication - dj-rest-auth (0) | 2024.02.08 |
[Team 22] Django REST framework (3) - Viewset (0) | 2024.02.03 |