Switch to a resource and service based API from previous app based one.
Signed-off-by: Michael Price <loneviking72@gmail.com>
This commit is contained in:
committed by
Roberto Rosario
parent
f3427c7470
commit
b4bf9bfaee
@@ -5,10 +5,35 @@ 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):
|
||||
@@ -48,6 +73,12 @@ class APIEndPoint(object):
|
||||
def register_urls(self, urlpatterns):
|
||||
from .urls import urlpatterns as app_urls
|
||||
|
||||
app_urls += [
|
||||
url(r'^%s/' % (self.name or self.app.name), include(urlpatterns)),
|
||||
]
|
||||
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)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user