2010-04-17 02:44:34 +04:00
|
|
|
from django.conf import settings
|
2012-09-08 03:10:39 +04:00
|
|
|
from django.conf.urls import include, patterns, url
|
2010-02-06 02:38:38 +03:00
|
|
|
|
2010-08-27 21:07:16 +04:00
|
|
|
from piston.resource import Resource
|
2010-02-06 02:38:38 +03:00
|
|
|
|
2010-12-22 02:45:02 +03:00
|
|
|
from addons.urls import ADDON_ID
|
2010-09-14 21:08:54 +04:00
|
|
|
from api import authentication, handlers, views
|
2010-02-06 02:38:38 +03:00
|
|
|
|
2010-04-17 02:44:34 +04:00
|
|
|
API_CACHE_TIMEOUT = getattr(settings, 'API_CACHE_TIMEOUT', 500)
|
|
|
|
|
|
|
|
|
|
|
|
# Wrap class views in a lambda call so we get an fresh instance of the class
|
|
|
|
# for thread-safety.
|
2010-04-19 21:28:06 +04:00
|
|
|
def class_view(cls):
|
2010-04-17 02:44:34 +04:00
|
|
|
inner = lambda *args, **kw: cls()(*args, **kw)
|
2010-04-19 21:28:06 +04:00
|
|
|
return inner
|
2010-04-17 02:44:34 +04:00
|
|
|
|
2010-02-17 03:22:22 +03:00
|
|
|
|
2010-02-20 04:13:42 +03:00
|
|
|
# Regular expressions that we use in our urls.
|
2010-02-23 21:19:18 +03:00
|
|
|
type_regexp = '/(?P<addon_type>[^/]*)'
|
2010-02-20 04:13:42 +03:00
|
|
|
limit_regexp = '/(?P<limit>\d*)'
|
|
|
|
platform_regexp = '/(?P<platform>\w*)'
|
|
|
|
version_regexp = '/(?P<version>[^/]*)'
|
2011-12-05 22:07:12 +04:00
|
|
|
compat_mode = '(?:/(?P<compat_mode>(?:strict|normal|ignore)))?'
|
2010-02-20 04:13:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
def build_urls(base, appendages):
|
|
|
|
"""
|
|
|
|
Many of our urls build off each other:
|
|
|
|
e.g.
|
|
|
|
/search/:query
|
|
|
|
/search/:query/:type
|
|
|
|
.
|
|
|
|
.
|
|
|
|
/search/:query/:type/:limit/:platform/:version
|
2011-12-05 22:07:12 +04:00
|
|
|
/search/:query/:type/:limit/:platform/:version/:compatMode
|
2010-02-20 04:13:42 +03:00
|
|
|
"""
|
|
|
|
urls = [base]
|
|
|
|
for i in range(len(appendages)):
|
2010-04-17 02:44:34 +04:00
|
|
|
urls.append(base + ''.join(appendages[:i + 1]))
|
2010-02-20 04:13:42 +03:00
|
|
|
|
|
|
|
return urls
|
|
|
|
|
|
|
|
|
2010-02-17 03:22:22 +03:00
|
|
|
base_search_regexp = r'search/(?P<query>[^/]+)'
|
2011-12-05 22:07:12 +04:00
|
|
|
appendages = [type_regexp, limit_regexp, platform_regexp, version_regexp,
|
|
|
|
compat_mode]
|
2010-02-20 04:13:42 +03:00
|
|
|
search_regexps = build_urls(base_search_regexp, appendages)
|
|
|
|
|
|
|
|
base_list_regexp = r'list'
|
|
|
|
appendages.insert(0, '/(?P<list_type>[^/]+)')
|
|
|
|
list_regexps = build_urls(base_list_regexp, appendages)
|
|
|
|
|
2010-02-17 03:22:22 +03:00
|
|
|
|
|
|
|
api_patterns = patterns('',
|
|
|
|
# Addon_details
|
2010-12-22 02:45:02 +03:00
|
|
|
url('addon/%s$' % ADDON_ID, class_view(views.AddonDetailView),
|
2011-06-28 23:33:33 +04:00
|
|
|
name='api.addon_detail'),
|
|
|
|
url(r'^get_language_packs$', class_view(views.LanguageView),
|
|
|
|
name='api.language'),)
|
2010-02-17 03:22:22 +03:00
|
|
|
|
|
|
|
for regexp in search_regexps:
|
|
|
|
api_patterns += patterns('',
|
2010-04-19 21:28:06 +04:00
|
|
|
url(regexp + '/?$', class_view(views.SearchView), name='api.search'))
|
2010-02-17 03:22:22 +03:00
|
|
|
|
2010-02-20 04:13:42 +03:00
|
|
|
for regexp in list_regexps:
|
|
|
|
api_patterns += patterns('',
|
2010-04-19 21:28:06 +04:00
|
|
|
url(regexp + '/?$', class_view(views.ListView), name='api.list'))
|
2010-02-20 04:13:42 +03:00
|
|
|
|
2011-07-09 03:49:10 +04:00
|
|
|
ad = {'authentication': authentication.AMOOAuthAuthentication(two_legged=True)}
|
2010-08-27 21:07:16 +04:00
|
|
|
user_resource = Resource(handler=handlers.UserHandler, **ad)
|
2010-09-14 21:09:07 +04:00
|
|
|
addons_resource = Resource(handler=handlers.AddonsHandler, **ad)
|
2011-11-25 02:37:37 +04:00
|
|
|
apps_resource = Resource(handler=handlers.AppsHandler, **ad)
|
2010-09-28 00:13:48 +04:00
|
|
|
version_resource = Resource(handler=handlers.VersionsHandler, **ad)
|
2010-08-27 21:07:16 +04:00
|
|
|
|
|
|
|
piston_patterns = patterns('',
|
|
|
|
url(r'^user/$', user_resource, name='api.user'),
|
2010-09-14 21:09:07 +04:00
|
|
|
url(r'^addons/$', addons_resource, name='api.addons'),
|
2010-12-22 02:45:02 +03:00
|
|
|
url(r'^addon/%s$' % ADDON_ID, addons_resource, name='api.addon'),
|
|
|
|
url(r'^addon/%s/versions$' % ADDON_ID, version_resource,
|
2010-09-28 00:13:48 +04:00
|
|
|
name='api.versions'),
|
2010-12-22 02:45:02 +03:00
|
|
|
url(r'^addon/%s/version/(?P<version_id>\d+)$' % ADDON_ID,
|
2011-11-25 02:37:37 +04:00
|
|
|
version_resource, name='api.version'),
|
|
|
|
url(r'^apps/$', apps_resource, name='api.apps'),
|
|
|
|
url(r'^app/%s$' % ADDON_ID, apps_resource, name='api.app'),
|
2010-08-27 21:07:16 +04:00
|
|
|
)
|
|
|
|
|
2010-02-06 02:38:38 +03:00
|
|
|
urlpatterns = patterns('',
|
|
|
|
# Redirect api requests without versions
|
2010-02-20 04:13:42 +03:00
|
|
|
url('^((?:addon|search|list)/.*)$', views.redirect_view),
|
2010-02-17 03:22:22 +03:00
|
|
|
|
2010-08-27 21:07:16 +04:00
|
|
|
# Piston
|
|
|
|
url(r'^2/', include(piston_patterns)),
|
2011-11-23 01:16:56 +04:00
|
|
|
url(r'^2/performance/add$', views.performance_add,
|
|
|
|
name='api.performance.add'),
|
2010-08-27 21:07:16 +04:00
|
|
|
|
2010-02-17 03:22:22 +03:00
|
|
|
# Append api_version to the real api views
|
2011-07-27 23:58:39 +04:00
|
|
|
url(r'^(?P<api_version>\d+|\d+.\d+)/search/guid:(?P<guids>.*)',
|
|
|
|
views.guid_search),
|
2010-02-17 03:22:22 +03:00
|
|
|
url(r'^(?P<api_version>\d+|\d+.\d+)/', include(api_patterns)),
|
2010-02-06 02:38:38 +03:00
|
|
|
)
|