Skip to content

Instantly share code, notes, and snippets.

@hadrizi
Last active October 21, 2020 11:35
Show Gist options
  • Save hadrizi/d5b5d6c277d38f154782de438af7d463 to your computer and use it in GitHub Desktop.
Save hadrizi/d5b5d6c277d38f154782de438af7d463 to your computer and use it in GitHub Desktop.
Swagger file to wrap swagger_auto_schema and action decorators
"""
Swagger file v0.1
This is custom file used to store swagger_auto_schema and action data
Structure of each function is:
1. Description
2. All possible responses and requests
3. Request body if needed
4. Responses array with all possible HTTP codes
"""
from rest_framework.decorators import action
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
def build_func(func, detail=False, methods=None, description=None, responses=None, request_body=None, url_path=None):
actionized_func = action(detail=detail, methods=methods, url_path=url_path)(func)
swaggerized_func = swagger_auto_schema(request_body=request_body, operation_description=description, responses=responses)(actionized_func)
return swaggerized_func
def method_name_swagger(func):
description = "Method description."
detail = False
methods = ['get', ]
# Response and request variables
# They all must begin with __
# According to PEP8 it means they should not be used outside of this class
__response_200 = openapi.Schema(type=openapi.TYPE_OBJECT, properties={})
request_body = openapi.Schema(type=openapi.TYPE_OBJECT, properties={})
responses = {
200: __response_200,
}
return build_func(func, detail=detail, methods=methods, description=description, responses=responses, request_body=request_body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment