1 ๋ถ„ ์†Œ์š”

Swagger?

Swagger ๋ž€, ๊ฐœ๋ฐœํ•œ REST API๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ฌธ์„œํ™” ํ•ด์ฃผ๊ณ , ์ด๋ฅผ ํ†ตํ•ด์„œ ๊ด€๋ฆฌ ๋ฐ ์ œ 3์˜ ์‚ฌ์šฉ์ž๊ฐ€ ํŽธ๋ฆฌํ•˜๊ฒŒ API๋ฅผ ํ˜ธ์ถœํ•ด๋ณด๊ณ  ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ์ž„.

DRF ์—์„œ Swagger๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด drf-yasg ๋ฅผ ์„ค์น˜ํ•ด์ค˜์•ผํ•œ๋‹ค.

drf_yasg ๋Š” Django๋กœ ์ •์˜๋œ API๋ฅผ ๋ฌธ์„œํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€๋กœ, Django Rest Framework_Yet Another Swagger Generator ์˜ ์•ฝ์ž์ž„.

์„ธํŒ… ์‹œ์ž‘

drf_yasg ์„ค์น˜

$ pip install drf_yasg

settings.py ์ถ”๊ฐ€

INSTALLED_APPS = [
		...
		"rest_framework",
		"drf_yasg",
]

urls.py ์ถ”๊ฐ€

urls.py ์—๋Š” 3๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค.

  1. get_schema_view/openapi/AllowAny import

     from django.contrib import admin
     from django.urls import path, include
     from django.conf.urls import url
     from rest_framework.permissions import AllowAny
     from drf_yasg.views import get_schema_view 
     from drf_yasg import openapi
    
  2. schema_url_patterns (swagger์—์„œ API ๋ฌธ์„œ๋กœ ๋ณด๊ณ  ์‹ถ์€ URL๋“ค์„ ์ •์˜ํ•จ)

     schema_url_patterns = [ 
         path('', include('blog.urls')),
         ]
        
     schema_view_v1 = get_schema_view(
         openapi.Info(
             title="Open API",
             default_version='v1',
             description="์‹œ์Šคํ…œ API",
             terms_of_service="https://www.google.com/policies/terms/",
         ),
         public=True,
         permission_classes=(AllowAny,),
         patterns=schema_url_patterns,
     ) 
    
  3. urlpatterns (Swagger๋ฅผ ๋ณด๊ธฐ ์œ„ํ•œ ์—”๋“œํฌ์ธํŠธ ์ •์˜ํ•จ)

     urlpatterns = [
         path('admin/', admin.site.urls),
         path('',include('blog.urls')),
         url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view_v1.without_ui(cache_timeout=0), name='schema-json'),
         url(r'^swagger/$', schema_view_v1.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
         url(r'^redoc/$', schema_view_v1.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
     ]
    

API ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ถ”๊ฐ€

DRF ์—์„œ๋Š” ์š”์ฒญํ•œ request ๊ตฌ๋ถ„์„ API ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ๊ตฌ๋ถ„ํ•œ๋‹ค. (FBV ๊ธฐ์ค€)

  • FBV: Function Based View โ†’ API ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ๋ถ€๋ถ„์„ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„

FBV๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” api_view(["GET"]) ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•ด์ค˜์•ผํ•จ.

Swagger๋ฅผ ์‚ฌ์šฉํ•œ ์ƒˆ๋กœ์šด ํ…Œ์ŠคํŠธ ๊ธฐ๋Šฅ?

HTTP ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ, ํ—ค๋” ๊ฐ’์„ ํฌํ•จ์‹œ์ผœ์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋กœ๊ทธ์ธ ๋œ ํšŒ์›์— ๋Œ€ํ•ด์„œ๋งŒ ์–ด๋– ํ•œ ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค˜์•ผ ํ•  ๋•Œ, ํ—ค๋”์— ํ•ด๋‹น ์œ ์ €๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ํ† ํฐ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค๋˜์ง€..

์ด๋Ÿด ๋•Œ, Swagger UI์—์„œ ์ง์ ‘ ํ—ค๋”์— ๊ฐ’์„ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์„ ์ ์šฉํ• ๊นŒ?

๊ธฐ๋ณธ Swagger UI์—๋Š” ์ด๋Ÿฐ ์—ญํ• ์€ ์—†๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋จ.

์•„๋ž˜๋Š” accessToken, refreshToken ๊ฐ’์„ ํ—ค๋”์— ํฌํ•จ์‹œ์ผœ ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์˜ˆ์‹œ์ž„.

settings.py

SWAGGER_SETTINGS = {
    'USE_SESSION_AUTH': False,
    'SECURITY_DEFINITIONS': {
        'AccessToken': {
            'type': 'apiKey',
            'in': 'header',
            'name': 'Authorization',
            'description': 'Add accessToken in format: Bearer <accessToken>',
        },
        'RefreshToken': {
            'type': 'apiKey',
            'in': 'header',
            'name': 'Refresh-Token',
            'description': 'Add refreshToken directly',
        },
    },
}

์ด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋จ!

์ฐธ๊ณ  ์ž๋ฃŒ

[DRF] Concept part - Swagger : drf-yasg

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ