Get rids of the APIEndPoint and APIResource classes. Register API url using the 'has_rest_api' AppConfig variable.

Signed-off-by: Michael Price <loneviking72@gmail.com>
This commit is contained in:
Michael Price
2018-04-01 18:30:59 -04:00
committed by Roberto Rosario
parent 36d19ea284
commit e1956f8d80
26 changed files with 39 additions and 197 deletions
+5 -9
View File
@@ -1,16 +1,12 @@
from __future__ import unicode_literals
from rest_framework import generics
from .classes import APIResource
from .serializers import APIResourceSerializer
from rest_framework import generics, renderers
from rest_framework.authtoken.views import ObtainAuthToken
class APIResourceTypeListView(generics.ListAPIView):
class BrowseableObtainAuthToken(ObtainAuthToken):
"""
Returns a list of all the available API resources.
Obtain an API authentication token.
"""
serializer_class = APIResourceSerializer
def get_queryset(self):
return APIResource.all()
renderer_classes = (renderers.BrowsableAPIRenderer, renderers.JSONRenderer)
+8 -4
View File
@@ -1,11 +1,12 @@
from __future__ import unicode_literals
from django.apps import apps
from django.conf import settings
from django.utils.module_loading import import_string
from django.utils.translation import ugettext_lazy as _
from common import MayanAppConfig, menu_tools
from .classes import APIEndPoint
from .links import link_api, link_api_documentation
from .licenses import * # NOQA
@@ -17,9 +18,12 @@ class RESTAPIApp(MayanAppConfig):
def ready(self):
super(RESTAPIApp, self).ready()
from .urls import api_urls
settings.STRONGHOLD_PUBLIC_URLS += (r'^/%s/.+$' % self.app_url,)
APIEndPoint(app=self, name='rest', version_string='1')
menu_tools.bind_links(links=(link_api, link_api_documentation))
for app in apps.get_app_configs():
if getattr(app, 'has_rest_api', False):
app_api_urls = import_string('{}.urls.api_urls'.format(app.label))
api_urls.extend(app_api_urls)
-83
View File
@@ -1,83 +0,0 @@
from __future__ import unicode_literals
from django.conf import settings
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.module_loading import import_string
from .exceptions import APIResourcePatternError
@python_2_unicode_compatible
class APIResource(object):
_registry = {}
@classmethod
def all(cls):
return cls._registry.values()
@classmethod
def get(cls, name):
return cls._registry[name]
def __str__(self):
return force_text(self.name)
def __init__(self, name, label, description=None):
self.label = label
self.name = name
self.description = description
self.__class__._registry[self.name] = self
@python_2_unicode_compatible
class APIEndPoint(object):
_registry = {}
_patterns = []
@classmethod
def get_all(cls):
return cls._registry.values()
@classmethod
def get(cls, name):
return cls._registry[name]
def __str__(self):
return force_text(self.app.name)
def __init__(self, app, version_string, name=None):
self.app = app
self.endpoints = []
self.name = name
self.version_string = version_string
try:
api_urls = import_string(
'{0}.urls.api_urls'.format(app.name)
)
except Exception:
if settings.DEBUG:
raise
else:
# Ignore import time errors
pass
else:
self.register_urls(api_urls)
self.__class__._registry[app.name] = self
@property
def app_name(self):
return self.app.name
def register_urls(self, urlpatterns):
from .urls import urlpatterns as app_urls
for url in urlpatterns:
if url.regex.pattern not in self.__class__._patterns:
app_urls.append(url)
self.__class__._patterns.append(url.regex.pattern)
else:
raise APIResourcePatternError(
'App "{}" tried to register API URL pattern "{}", which '
'already exists'.format(self.app.label, url.regex.pattern)
)
-9
View File
@@ -1,9 +0,0 @@
from __future__ import unicode_literals
from rest_framework import serializers
class APIResourceSerializer(serializers.Serializer):
description = serializers.CharField()
label = serializers.CharField()
name = serializers.CharField()
+6 -10
View File
@@ -1,21 +1,17 @@
from __future__ import unicode_literals
from django.conf.urls import url
from django.conf.urls import include, url
from .api_views import APIResourceTypeListView
from .views import APIBase, BrowseableObtainAuthToken
from .api_views import BrowseableObtainAuthToken
urlpatterns = []
api_urls = [
url(r'^$', APIBase.as_view(), name='api_root'),
url(
r'^resources/$', APIResourceTypeListView.as_view(),
name='resource-list'
),
url(
r'^auth/token/obtain/$', BrowseableObtainAuthToken.as_view(),
name='auth_token_obtain'
),
]
urlpatterns = [
url(r'^', include(api_urls)),
]
-21
View File
@@ -1,21 +0,0 @@
from __future__ import unicode_literals
from rest_framework import renderers
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework_swagger.views import SwaggerResourcesView
class APIBase(SwaggerResourcesView):
"""
Main entry point of the API.
"""
renderer_classes = (renderers.BrowsableAPIRenderer, renderers.JSONRenderer)
class BrowseableObtainAuthToken(ObtainAuthToken):
"""
Obtain an API authentication token.
"""
renderer_classes = (renderers.BrowsableAPIRenderer, renderers.JSONRenderer)