-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/swagger #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feature/swagger #32
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7475a18
Feat: swagger json λ°°ν¬
jungmir baf34e9
Docs: FAQ swagger νμΌ μμ
jungmir 744f9cf
Docs: response schema μμ
jungmir 3e83b00
Docs: response λͺ¨λΈ μμ
jungmir ec12766
Chore: setup-dev μ€ν μ pre-commitμ install νλλ‘ μ»€λ§¨λ ꡬμ±
jungmir 280866b
Docs: swagger λͺ¨λ λΆλ¦¬
jungmir 12df2f1
Chore: μ€λ³΅ command μ κ±°
jungmir 0e3cb29
Merge branch 'develop' into feature/swagger
jungmir 4b56c16
Merge branch 'develop' into feature/swagger
jungmir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class CoreConfig(AppConfig): | ||
| name = "core" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from rest_framework import status | ||
| from rest_framework.exceptions import APIException | ||
|
|
||
|
|
||
| class BaseException(APIException): | ||
| status_code = status.HTTP_200_OK | ||
| default_detail = "Internal Server Error" | ||
| default_code = "ERROR" | ||
|
|
||
|
|
||
| class NotExistException(BaseException): | ||
| status_code = status.HTTP_200_OK | ||
| default_detail = "Not Exist." | ||
| default_code = "Not Exist" |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from django.core.exceptions import PermissionDenied as DjangoPermissionDenied | ||
| from django.http import Http404 | ||
| from rest_framework.exceptions import APIException, NotFound, PermissionDenied | ||
| from rest_framework.views import set_rollback | ||
|
|
||
| from core.responses.base import BaseResponse | ||
|
|
||
|
|
||
| def custom_exception_handler(exc, context): | ||
| if isinstance(exc, Http404): | ||
| exc = NotFound(*(exc.args)) | ||
| elif isinstance(exc, DjangoPermissionDenied): | ||
| exc = PermissionDenied(*(exc.args)) | ||
|
|
||
| if isinstance(exc, APIException): | ||
| headers = {} | ||
| if getattr(exc, "auth_header", None): | ||
| headers["WWW-Authenticate"] = exc.auth_header | ||
| if getattr(exc, "wait", None): | ||
| headers["Retry-After"] = "%d" % exc.wait | ||
|
|
||
| error = dict(code=exc.default_code, message=exc.detail) | ||
|
|
||
| set_rollback() | ||
| return BaseResponse(error=error, headers=headers, status="ERROR") | ||
|
|
||
| return None |
jungmir marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from rest_framework.pagination import PageNumberPagination as DRFPageNumberPagination | ||
|
|
||
| from .responses.base import BaseResponse | ||
|
|
||
|
|
||
| class PageNumberPagination(DRFPageNumberPagination): | ||
| page_size = 10 | ||
| page_size_query_param = "page_size" | ||
| max_page_size = 100 # page λ¨μμ μμ² μ΅λ size | ||
|
|
||
| def get_paginated_response(self, data): | ||
| pagination = { | ||
| "count": self.page.paginator.count, | ||
| "next": self.get_next_link(), | ||
| "previous": self.get_previous_link(), | ||
| } | ||
| return BaseResponse(data=data, pagination=pagination) | ||
|
|
||
| def get_paginated_response_schema(self, schema: dict) -> dict: | ||
| return schema |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from typing import Literal | ||
|
|
||
| from rest_framework.response import Response | ||
| from rest_framework.serializers import Serializer | ||
| from rest_framework.status import HTTP_200_OK | ||
|
|
||
| from core.responses.serializer import SuccessResponseSerializer | ||
|
|
||
|
|
||
| class BaseResponse(Response): | ||
| def __init__( | ||
| self, | ||
| data: dict | list | None = None, | ||
| error: dict | None = None, | ||
| pagination: dict | None = None, | ||
| template_name: str | None = None, | ||
| headers: dict | None = None, | ||
| exception: bool = False, | ||
| content_type: str | None = None, | ||
| status: Literal["SUCCESS", "ERROR"] = "SUCCESS", | ||
| ): | ||
| response_format = { | ||
| "status": status, | ||
| "error": error, | ||
| "data": data, | ||
| "pagination": pagination, | ||
| } | ||
| super().__init__( | ||
| response_format, HTTP_200_OK, template_name, headers, exception, content_type | ||
| ) | ||
|
|
||
| def __class_getitem__(cls, *args, **kwargs): | ||
| return cls | ||
|
|
||
| def get_response_schema(self) -> Serializer: | ||
| return SuccessResponseSerializer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from rest_framework import serializers | ||
|
|
||
|
|
||
| class ErrorSerializer(serializers.Serializer): | ||
| code = serializers.CharField() | ||
| message = serializers.CharField() | ||
|
|
||
| def to_representation(self, instance): | ||
| return { | ||
| "code": instance.code, | ||
| "message": instance.message, | ||
| } | ||
|
|
||
|
|
||
| class PageNumberPaginationSerializer(serializers.Serializer): | ||
| count = serializers.IntegerField() | ||
| next = serializers.CharField() | ||
| previous = serializers.CharField() | ||
|
|
||
| def to_representation(self, instance): | ||
| return { | ||
| "count": instance.count, | ||
| "next": instance.next, | ||
| "previous": instance.previous, | ||
| } | ||
|
|
||
|
|
||
| class SuccessResponseSerializer(serializers.Serializer): | ||
| status = serializers.CharField(default="SUCCESS") | ||
| data = serializers.DictField(required=False) | ||
| pagination = PageNumberPaginationSerializer(required=False, allow_null=True) | ||
|
|
||
| def to_representation(self, instance): | ||
| return { | ||
| "status": instance.status, | ||
| "data": instance.data, | ||
| "error": None, | ||
| "pagination": None, | ||
| } | ||
|
|
||
|
|
||
| class ListSuccessResponseSerializer(serializers.Serializer): | ||
| status = serializers.CharField(default="SUCCESS") | ||
| data = serializers.ListField(required=False, child=serializers.DictField()) | ||
| pagination = PageNumberPaginationSerializer(required=False, allow_null=True) | ||
|
|
||
| def to_representation(self, instance): | ||
| return { | ||
| "status": instance.status, | ||
| "data": instance.data, | ||
| "error": None, | ||
| "pagination": instance.pagination, | ||
| } | ||
|
|
||
|
|
||
| class ErrorResponseSerializer(serializers.Serializer): | ||
| status = serializers.CharField(default="ERROR") | ||
| error = ErrorSerializer() | ||
|
|
||
| def to_representation(self, instance): | ||
| return { | ||
| "status": instance.status, | ||
| "error": instance.error, | ||
| "data": None, | ||
| "pagination": None, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from typing import Callable | ||
|
|
||
| from drf_spectacular.utils import extend_schema | ||
|
|
||
|
|
||
| class SwaggerSchema: | ||
| @classmethod | ||
| def generate_schema(cls, *args, **kwargs) -> Callable: | ||
| return extend_schema(*args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Create your tests here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| from drf_spectacular.utils import OpenApiExample, OpenApiResponse | ||
|
|
||
| from core.responses.serializer import ( | ||
| ErrorResponseSerializer, | ||
| ListSuccessResponseSerializer, | ||
| SuccessResponseSerializer, | ||
| ) | ||
| from core.swagger import SwaggerSchema | ||
|
|
||
|
|
||
| class FAQAPIDocs(SwaggerSchema): | ||
| @classmethod | ||
| def retrieve(cls): | ||
| responses = { | ||
| "μ±κ³΅": OpenApiResponse( | ||
| response=SuccessResponseSerializer, | ||
| description="λ¨μΌ μλ΅ μ±κ³΅", | ||
| examples=[ | ||
| OpenApiExample( | ||
| name="FAQ μ‘°ν", | ||
| value={ | ||
| "status": "SUCCESS", | ||
| "data": { | ||
| "id": 1, | ||
| "question": "μ§λ¬Έ", | ||
| "answer": "λ΅λ³", | ||
| "created_at": "2021-01-01", | ||
| }, | ||
| }, | ||
| ), | ||
| ], | ||
| ), | ||
| "μλ¬": OpenApiResponse( | ||
| response=ErrorResponseSerializer, | ||
| description="μλ΅ μλ¬", | ||
| examples=[ | ||
| OpenApiExample( | ||
| name="λ°μ΄ν° μμ", | ||
| value={ | ||
| "status": "ERROR", | ||
| "error": {"code": "NOT_EXIST", "message": "λ°μ΄ν°κ° μμ΅λλ€."}, | ||
| }, | ||
| ), | ||
| ], | ||
| ), | ||
| } | ||
| return cls.generate_schema( | ||
| operation_id="faq_retrieve", description="FAQ μ‘°ν", responses=responses | ||
| ) | ||
|
|
||
| @classmethod | ||
| def list(cls): | ||
| responses = { | ||
| "μ±κ³΅": OpenApiResponse( | ||
| response=ListSuccessResponseSerializer, | ||
| description="λ€μ€ μλ΅ μ±κ³΅", | ||
| examples=[ | ||
| OpenApiExample( | ||
| name="FAQ λͺ©λ‘ μ‘°ν (νμ΄μ§λ€μ΄μ μμ)", | ||
| value={ | ||
| "status": "SUCCESS", | ||
| "data": [ | ||
| { | ||
| "id": 1, | ||
| "question": "μ§λ¬Έ1", | ||
| "answer": "λ΅λ³1", | ||
| }, | ||
| ], | ||
| "pagination": { | ||
| "count": 20, | ||
| "next": "http://localhost:8000/api/v1/faqs/?page=2", | ||
| "previous": "http://localhost:8000/api/v1/faqs/?page=1", | ||
| }, | ||
| }, | ||
| ), | ||
| OpenApiExample( | ||
| name="FAQ λͺ©λ‘ μ‘°ν (λ°μ΄ν° μμ)", | ||
| value={ | ||
| "status": "SUCCESS", | ||
| "data": [ | ||
| { | ||
| "id": 1, | ||
| "question": "μ§λ¬Έ1", | ||
| "answer": "λ΅λ³1", | ||
| }, | ||
| ], | ||
| "pagination": {"count": 1, "next": None, "previous": None}, | ||
| }, | ||
| ), | ||
| OpenApiExample( | ||
| name="FAQ λͺ©λ‘ μ‘°ν (λ°μ΄ν° μμ)", | ||
| value={ | ||
| "status": "SUCCESS", | ||
| "data": [], | ||
| "pagination": {"count": 0, "next": None, "previous": None}, | ||
| }, | ||
| ), | ||
| ], | ||
| ), | ||
| "μλ¬": OpenApiResponse(response=ErrorResponseSerializer, description="μλ΅ μλ¬"), | ||
| } | ||
| return cls.generate_schema( | ||
| operation_id="faq_list", description="λͺ¨λ FAQ λͺ©λ‘ μ‘°ν", responses=responses | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.