How can i generate static api documentation for django rest APIs? - django

I went through the documentation and tried to use swagger and coreapi, but both are interactive, I want the same api documentation without interaction. Is it possible?
Here you can see API documentation
https://editor.swagger.io/
but its interacting with the respective api. I want same UI and without interaction.

Oh it is really easy.
First install: django-rest-swagger==2.1.2
Add to settings: 'rest_framework_swagger'
Next make urls.py changes:
from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework import permissions
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.schemas import get_schema_view
schema_view = get_schema_view(
title='Swagger My - API documentation',
public=True,
renderer_classes=[CoreJSONRenderer, OpenAPIRenderer, SwaggerUIRenderer],
authentication_classes=[JSONWebTokenAuthentication],
permission_classes=[permissions.AllowAny]
)
urlpatterns = [
# Swagger
path('docs/', schema_view),
]
And you choose renderer_classes=[CoreJSONRenderer, OpenAPIRenderer, SwaggerUIRenderer].
This is example with JWT JSONWebTokenAuthentication for auth.

Related

Simple JWT Method Not Allowed on Django App deployed on Gcloud App Engine

I am fairly new to django and gcloud and I am currently stuck at this issue where I am trying deploy my Django App on Gcloud App Engine. When accessing the api for getting token I got this error.
Error when accessing API from App Engine
from django.contrib import admin
from django.urls import include, path
from rest_framework_simplejwt import views as jwt_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('quitnow/', include('quitnow.urls')),
path("admin/", admin.site.urls),
path('api/token/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
]
This is my urls.py. When I try to post my information to get token, the post requests doesn't return a response and hangs. I am stuck on this for days. Would really appreciate any help!

Swagger Django [No operations defined in spec]

I have been trying to integrate Swagger with Django Application and encounter this error:
No operations defined in spec!
My project structure is
App
views.py
urls.py
..
App2
settings.py
urls.py
..
I am using drf_yasg for my purpose. I have included all details in settings.py and in App2 I have this in urls.py:
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="Testing",
default_version='v1',
description="Doc Integration",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="abc#abc.com"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns=[path("admin/", admin.site.urls),
path("", include("app.urls")),
path('swagger/', 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'),]
My class views such as (class Att(generic.TemplateView) and methods are declared in views.py under App. I tried methods such as #swagger_auto_schema, #api_view, to display app class and functions in the documentation. But it just returns No operations defined in spec!.
I tried using routers to register the view as well but did not work, even django-rest-swagger fails. Any help is appreciated. Thanks!
I have a project made of Django and I'm new to DRF. I had seen No operations defined in spec! as well while trying drf-yasg which is one of Swagger generators. Hope this way works for you.
I imported ->
from rest_framework.views import APIView
The class in views.py was like
class FeedView(View):
and the functions showed up when I changed the class like
class FeedView(APIView):
I need to deal with #swagger_auto_schema, #api_view to make my API document works with proper parameters.

Django DRF Swagger: In urls.py SimpleRouter / DefaultRouter are not auto discovered by swagger docs

Django DRF Swagger docs are not showing the ModelViewSets API endpoints registered as ROUTERS (not urlpattern).
In the example below standard docs (rest_framework.documentation) are showing/documenting this "follow_up" API and swagger docs are not, total skip nothing is showing.
For urlpatterns all is good, and below code for 'this_is_showing' is being nicely documented:
from urls.py file
from rest_framework.documentation import include_docs_urls
from rest_framework.routers import SimpleRouter, DefaultRouter
from rest_framework_swagger.views import get_swagger_view
from . import views
schema_view = get_swagger_view(title=MY APP API')
router = DefaultRouter()
router.register("follow_up", views.FollowUpViewSet)
urlpatterns = [
url(r'^this_is_showing/$', views.SomeView.as_view(), name='view'),
url(r'docs/', include_docs_urls(
title='API Docs', public=True)),
url(r'^swag/', schema_view),
]
What am I missing?
django-rest-swagger==2.2.0,
djangorestframework==3.11.0
EDIT 1
django-rest-swagger Package not maintained anymore!
Moved to drf_yasg: great tool with swagger and reDocs inside.
The DRF docs suggest that if you want to get the auto-generated API list view you need to use the DefaultRouter. I wonder if the SimpleRouter lacks the introspective mechanism (or other hooks) that django-rest-swagger uses to get its information.
https://www.django-rest-framework.org/api-guide/routers/#defaultrouter
EDIT 1
The DRF-swagger docs say that their example uses the DRF example: https://django-rest-swagger.readthedocs.io/en/latest/
The DRF example uses the default router: https://github.com/encode/rest-framework-tutorial/blob/master/snippets/urls.py
EDIT 2
I believe you'll also need to include the router somewhere in your URL patterns. If you look here: https://github.com/encode/rest-framework-tutorial/blob/master/snippets/urls.py
Not only is the DefaultRouter being used, but the router that's registered is included in the URL patters:
from django.conf.urls import include, url
from rest_framework.routers import DefaultRouter
from snippets import views
# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)
# The API URLs are now determined automatically by the router.
# Additionally, we include the login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls))
]

rest framework swagger 'AutoSchema' object has no attribute get_link

I am trying to browse the api documentation of rest framework swagger. but getting error AttributeError: 'AutoSchema' object has no attribute 'get_link
I have used django rest framework swagger. but not able to browse the documentation.
from django.contrib import admin
from django.urls import path, include
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API')
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('auth.urls')),
path('docs/', schema_view),
]
add below line in settings.py
REST_FRAMEWORK = {'DEFAULT_SCHEMA_CLASS':'rest_framework.schemas.coreapi.AutoSchema' }
djagno-rest-swagger is deprecated and suggests to use drf-yasg
use drf-yasg it is more useful than djagno-rest-swagger
When I add this to my seetings.py, works fine
REST_FRAMEWORK = {
...
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}

drf-yasg provides wrong paths to URIs

In my application I need to have multiple Swagger pages with grouped endpoints for multiple clients.
One of my clients (paths) supplies mobile app API, another supplies web client API. URL patterns are kept in 2 different urls.py accordingly.
I'm using drf-yasg to generate schema for my API.
To generate swagger specification for those I'm initializing 2 separate schema_views for each urls.py file like this:
from api_mobile.urls import urlpatterns as mobile_patterns
from api_web.urls import urlpatterns as web_patterns
mobile_schema_view = get_schema_view(
openapi.Info(
title="Mobile API",
default_version='v3',
),
public=True,
permission_classes=(permissions.AllowAny,),
patterns=mobile_patterns,
)
web_schema_view = get_schema_view(
openapi.Info(
title="Web API",
default_version='v1',
),
public=True,
permission_classes=(permissions.AllowAny,),
patterns=web_patterns,
)
urlpatterns = [
path(
'api/mobile/docs',
mobile_schema_view.with_ui('swagger', cache_timeout=0),
name='mobile-schema-ui'
),
path(
'api/web/docs',
web_schema_view.with_ui('swagger', cache_timeout=0),
name='web-schema-ui'
),
path('api/mobile/v3/', include('api_mobile.urls'), name='mobile_urls'),
path('api/web/v1/', include('api_web.urls'), name='web_urls'),
...
]
Where mobile_patterns and web_patterns are just a list of url patterns.
If I open http://localhost:8000/api/mobile/docs or http://localhost:8000/api/web/docs I do see correctly generated schema for both lists of patterns, yet if I try to do a request directly from swagger specification page all endpoints return 404 error – they all try to do a request to non-existing url pattern without providing full path to endpoint.
So if I do a request to any view from mobile endpoints swagger tries to do a request at
http://localhost:8000/some_mobile_url/ instead of http://localhost:8000/api/mobile/v3/some_mobile_url/
And situation is the same for another schema, swagger wrongly requests http://localhost:8000/some_web_url/ instead of using full path
http://localhost:8000/api/web/v3/some_web_url/
Obviously being able to test API directly through swagger is very important so specification itself is not enough in my case.
Is this an issue in me misconfiguring swagger itlesf or should I be somehow providing path to swagger so it prepends full path to each url accordingly?
This is working fine for us:
api_schema.py
from django.conf.urls import include, url
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from books.api.v1.urls import urlpatterns as api_v1
API_DESCRIPTION = openapi.Info(
...
)
schema_view = get_schema_view(
info=...,
...
url='https://oursite.company.io/',
patterns=[
url('api/v1/', include(api_v1)),
],
)
books.api.v1.urls.py
from django.conf.urls import include, url
urlpatterns = [
url(r'^books', ...),
...
]
urls.py
from ...api_schema import schema_view
...
url(r'^api/v1/', include(api_v1)),
url(r'^api/schema(?P<format>\.json|\.yaml)$',
schema_view.without_ui(cache_timeout=0),
name='api_schema_v1'),
...