SSAFY/Django

[Django] swagger

황성안 2021. 4. 27. 17:09
728x90

Swagger

문서를 우리가 쓰기 편하게 변환시켜줌

 

drf yasg

https://drf-yasg.readthedocs.io/en/stable/

pip install -U drf-yasg
INSTALLED_APPS = [
   ...
   'django.contrib.staticfiles',  # required for serving swagger ui's css/js files
   'drf_yasg',
   ...
]

articles > urls.py

맨위에

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

urlpatterns 위에

schema_view = get_schema_view(
   openapi.Info(
      title="성안쓰 가즈아",
      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,),
)

urlpattenrs 안에

url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),

이걸

path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), 이걸로 바꾼다음 ㄱㄱ

 

url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
# ㅊ첫번쨰는 JSON 테마로
   url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
# 두번째꺼는 YAML 테마
   url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),

 

결과

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

from django.urls import path
from . import views

schema_view = get_schema_view(
   openapi.Info(
      title="성안쓰 가즈아",
      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('articles/', views.article_list),
    path('articles/<int:article_id>/', views.article_detail),

    path('comments/', views.comment_list),
    path('comments/<int:comment_id>/', views.comment_detail),
    
    
    path('articles/<int:article_id>/comments/', views.create_comment), # 댓글 생성
    
    path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    # 
]

 

 

test해보기

http://localhost:8000/api/v1/swagger/ 해보면됨

 

2번쨰

path('redoc/', schema_view.with_ui('redoc', cache_timeout=0),),

 

swager에서 post 요청을 해볼수있도록 하는거

from drf_yasg.utils import swagger_auto_schema # 추가
@swagger_auto_schema(methods=['POST'], request_body=ArticleListSerializer) # 함수 위에 추가해주기


 

사이트들어가서 POST > data object에서 title. content 내요 ㅇ입력하기

다음 execute

그리고 아래를보면

code 디테일 모두보임

 

 

트래픽 줄이기?

settings.py에서 추가해주기

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.AnonRateThrottle',
    ],
    'DEFAULT_THROTTLE_RATES': {
        'anon': '5/minute', # second, minute, hour, day rksmd  
    }
}

 

728x90

'SSAFY > Django' 카테고리의 다른 글

Dom 실무?  (0) 2021.04.29
[Django] Dom  (0) 2021.04.28
[Django] REST - API ( 맛보기 )  (0) 2021.04.26
[Django] 좋아요, follow, hashtag 간단 코딩  (0) 2021.04.01
[Django] 로그인한 사용자 판별하기  (0) 2021.03.27