Skip to main content

Injecting parameters through annotations based on drf-yasg

Project description

yasg-util

Injecting parameters through annotations based on drf-yasg

Requirements

  • Python 3.6+
  • drf-yasg

Installation

$ pip install yasg-util

Start

settings.py

INSTALLED_APPS = [
   ...
   'django.contrib.staticfiles',  # required for serving swagger ui's css/js files
   'drf_yasg',
   ...
]

urls.py

from django.urls import path, re_path
from drf_yasg import openapi
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from rest_framework.fields import IntegerField, CharField, DictField
from rest_framework.serializers import Serializer
from rest_framework.views import APIView
from yasg_util import restapi, Location

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,),
)


class DemoSerializer(Serializer):
    name = CharField(required=True, min_length=2, max_length=20, help_text='name')
    age = IntegerField(required=True, min_value=0, max_value=150, help_text='ahe')

    class Meta:
        location = Location.BODY


class ResponseSerializer(Serializer):
    code = IntegerField(default=200, help_text='status code')
    msg = CharField(default='success', help_text='response message')
    data = DictField(help_text='response data')


class DemoAPIView(APIView):
    @restapi(response_model=ResponseSerializer)
    def post(self, request, pk: int, serializer: DemoSerializer):
        return {'code': 0, 'msg': 'success', 'data': {"pk": pk, "info": serializer.data}}


urlpatterns = [
    re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('doc/', 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'),
    path('demo/<int:pk>', DemoAPIView.as_view()),
]

go to http://127.0.0.1:8000/doc

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yasg-util-0.2.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

yasg_util-0.2.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file yasg-util-0.2.0.tar.gz.

File metadata

  • Download URL: yasg-util-0.2.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.2

File hashes

Hashes for yasg-util-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1a7a2cdd6fb0ec5500e735e9427e3e72493e3bcd5e4d72ba1a0d8eb2afac255d
MD5 b6176dde28fa8a3ea17901274210763f
BLAKE2b-256 0811fc9593841a50565f01f7dac6bbc1745257b9f4abca7d728943b0c7fbc7c1

See more details on using hashes here.

File details

Details for the file yasg_util-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: yasg_util-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.2

File hashes

Hashes for yasg_util-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee2e3767026656220001b27e8695807c785b5b8bfece98d862b33cb87f24d621
MD5 d007f2883a0b9608adadd1f857d7e884
BLAKE2b-256 3bef3fa9c40d4f22a32c231de9d04775c1759f34db38c2ac0fb1eb702f452d47

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page