内部和外部 API 的单独文档 (drf_yasg)

问题描述

我有两组 API:内部 API,用于我们团队开发的客户端应用程序,以及外部 API,用于我们的业务合作伙伴。我想要一个单一的文档页面,通过身份验证,向我们的开发人员显示内部 API,向所有其他查看者显示外部 API。我该怎么做?

我使用:Django、DRF 和 drf-yasg。

P.S:我知道这个问题很笼统,但我不知道从哪里开始。我只是猜测 get_schema_view 中的一些设置、我的视图和 URL 模式是需要的。

解决方法

您可以在urlconf函数中设置get_schema_view(...)参数

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",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',]

或者,您也可以设置 patterns 参数,该参数接受 URL 模式列表