Add root API showing the new endpoints.

Signed-off-by: Michael Price <loneviking72@gmail.com>
This commit is contained in:
Michael Price
2018-03-09 04:11:28 -04:00
committed by Roberto Rosario
parent afd4748426
commit 49bb7c879e
4 changed files with 43 additions and 2 deletions

View File

@@ -1,7 +1,29 @@
from __future__ import unicode_literals
from rest_framework import generics, renderers
from rest_framework import renderers
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.schemas.generators import EndpointEnumerator
from .classes import Endpoint
from .serializers import EndpointSerializer
class APIRoot(APIView):
def get(self, request, format=None):
"""
Return a list of all users.
"""
endpoint_enumerator = EndpointEnumerator()
endpoints = []
for url in sorted(set([entry[0].split('/')[2] for entry in endpoint_enumerator.get_api_endpoints()])):
if url:
endpoints.append(Endpoint(label=url))
serializer = EndpointSerializer(endpoints, many=True)
return Response(serializer.data)
class BrowseableObtainAuthToken(ObtainAuthToken):

View File

@@ -0,0 +1,10 @@
from __future__ import unicode_literals
class Endpoint(object):
def __init__(self, label):
self.label = label
@property
def url(self):
return '/api/{}/'.format(self.label)

View File

@@ -0,0 +1,8 @@
from __future__ import unicode_literals
from rest_framework import serializers
class EndpointSerializer(serializers.Serializer):
label = serializers.CharField(read_only=True)
url = serializers.URLField(read_only=True)

View File

@@ -2,10 +2,11 @@ from __future__ import unicode_literals
from django.conf.urls import include, url
from .api_views import BrowseableObtainAuthToken
from .api_views import APIRoot, BrowseableObtainAuthToken
api_urls = [
url(r'^$', APIRoot.as_view(), name='api_root'),
url(
r'^auth/token/obtain/$', BrowseableObtainAuthToken.as_view(),
name='auth_token_obtain'