Separate documentations for internal and external APIs (drf_yasg)

Viewed 645

I have two set of APIs: Internals, which are used in our client applications which we develop in our team, And externals, which is used by our business partners. I want to have a single document page which by authentication, shows internals APIs to our developers, and external APIs to every other viewer. How can I do that?

I use: Django, DRF, and drf-yasg.

P.S: I know this question is very general, but I do not have any clue where to start. I only guess some settings in get_schema_view, my views, and URL patterns are needed.

1 Answers

You can set the urlconf parameter in the get_schema_view(...) function

from django.urls import path
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view_internal = get_schema_view(
    openapi.Info(
        title="Snippets API",
        default_version='v1',
        description="Test description",
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
    urlconf="path.to.your.internal_app.urls",
)
schema_view_public = get_schema_view(
    openapi.Info(
        title="Snippets API",
        default_version='v1',
        description="Test description",
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
    urlconf="path.to.your.public_app.urls",
)

urlpatterns = [
    path(
        'public/swagger/',
        schema_view_public.with_ui('swagger', cache_timeout=0),
        name='schema-swagger-ui-internal'
    ),
    path(
        'internal/swagger/',
        schema_view_internal.with_ui('swagger', cache_timeout=0),
        name='schema-swagger-ui-internal'
    ),
]

Alternatively, you can set the patterns parameter also, which accept a list of URL patterns

Related