A Django Restframework router that can show APIViews and include other routers as navigable urls in the root view.
Project description
Django Browsable Router
pip install django-browsable-router
A Django Restframework router that can show APIViews and include other routers as navigable urls in the root view.
from browsable_router import APIRouter
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
class TestView(APIView):
...
class TestViewSet(ViewSet):
...
router_1 = APIRouter(name="other_routes", docstring="These are under a different route.")
router_1.register(r"view-1", TestView.as_view(), "view_1")
router_1.register(r"view-2", TestViewSet.as_view(), "view_2")
router_2 = APIRouter()
router_2.register(r"view-3", TestView.as_view(), "view_3")
router_2.navigation_routes = {
"route": router_1,
}
urlpatterns = [
path("api/", include(router_2.urls))
]
Resulting browsable API:
# API Root:
# """API root."""
#
# "route": "/api/route/"
# "view-3": "/api/view-3/"
#
# Other Routes:
# """These are under a different route."""
#
# "view-1": "/api/route/view-1/"
# "view-2": "/api/route/view-2/"
Additional stuff:
BaseAPIView
A custom APIView class that is designed for input and output verification. It allows different serializers for different HTTP methods. Simpy inherit from the class and any number of the included mixins. Then define the serializers per HTTP method in the 'pipelines' attribute.
Pipelines takes a tuple of the InputSerializer, logic method, and OutputSerializer Logic method has to return data that the OutputSerializer can serialize. This forces verification for the output, so that if something changes in the logic, or some unexpected values are returned, the endpoint will break instead of creating side effects.
from browsable_router import BaseAPIView, GetMixin, PostMixin
from rest_framework.serializers import Serializer
def get_method_logic(**kwargs):
...
def post_method_logic(**kwargs):
...
class SomeCustomView(GetMixin, PostMixin, BaseAPIView):
class GetInputSerializer(Serializer):
...
class GetOutputSerializer(Serializer):
...
class PostInputSerializer(Serializer):
...
class PostOutputSerializer(Serializer):
...
serializer_classes = {
"GET": (GetInputSerializer, get_method_logic, GetOutputSerializer),
"POST": (PostInputSerializer, post_method_logic, PostOutputSerializer),
}
ApiMetadata
This metadata class is enabled by default on BaseAPIView, and it will allow you to document your views automatically. Basically, it reads the InputSerializers fields as input, and the OutputSerializer fields as the output for each HTTP method for that view. You can then make a OPTIONS request to the view endpoint and get a JSON Schema-ish representation of the input and output for that endpoint.
SerializerAsOutputMetadata
This metadata class works like APIMetadata class, but is intended for the generic views that come with django rest framework. It assumes that the endpoint takes no arguments and returns the method serializers fields as output.
BlockSchemaAccess
This is a permission class that can be used to block access to OPTIONS schema in production (DEBUG=False).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-browsable-router-0.0.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 608f1f660391f2af68feb5efe4c0d34053bd73719b103eb51c7357fd6d55ff1c |
|
MD5 | ff0c63918e249fc4df09e30f3b902633 |
|
BLAKE2b-256 | 5136a5af379387238bfec556636372194836eccc8d8e9f45e4c9df76dd9f5448 |
Hashes for django_browsable_router-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89ad0f2e694e376bc731488b49cd1a4f51294329b87ba27930dcbd106cb154db |
|
MD5 | 63128fc72e8ea7e50a93e76130dab012 |
|
BLAKE2b-256 | b983f4bb0e123b787d3f768f350e3d90b6246e463a1c49e077b67bc8611c6b9a |