api: Update for clarity (API breaking change)

- Change the term 'service' to 'shortcut' as what we want to present is actually
  shortcuts and not services. Services is already a confused term between
  firewall service and a service daemon.

- Fix icon paths containing relative URLs.

- Make the access information API extensible in future.

- Send full list of domains in access information and not just one domain per
  domain type.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2017-12-06 15:06:19 +05:30 committed by James Valleroy
parent ed25449711
commit 13c4c687da
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 37 additions and 33 deletions

View File

@ -21,9 +21,9 @@ URLs for the plinth api for android app.
from django.conf.urls import url from django.conf.urls import url
from stronghold.decorators import public from stronghold.decorators import public
from plinth.modules.api.views import get_access_info, get_services from plinth.modules.api import views
urlpatterns = [ urlpatterns = [
url(r'^api/(?P<version>[0-9]+)/services/?$', public(get_services)), url(r'^api/(?P<version>[0-9]+)/shortcuts/?$', public(views.shortcuts)),
url(r'^api/(?P<version>[0-9]+)/access-info/?$', public(get_access_info)), url(r'^api/(?P<version>[0-9]+)/access-info/?$', public(views.access_info)),
] ]

View File

@ -20,49 +20,53 @@ Plinth module for api for android app.
from django.http import HttpResponse from django.http import HttpResponse
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.templatetags.static import static
from plinth.modules.names import get_domain, get_domain_types
from plinth import frontpage from plinth import frontpage
from plinth import module_loader from plinth import module_loader
from plinth.modules import names
import json import json
def get_access_info(request, **kwargs): def access_info(request, **kwargs):
response = { """API view to return a list of domains and types."""
domain_type: get_domain(domain_type) domains = [{
for domain_type in get_domain_types() 'domain': domain,
} 'type': domain_type
return HttpResponse(json.dumps(response), content_type="application/json") } for domain_type, domains in names.domains.items() for domain in domains]
response = {'domains': domains}
return HttpResponse(json.dumps(response), content_type='application/json')
def get_services(request, **kwargs): def shortcuts(request, **kwargs):
services = [shortcut['id'].split('_')[0] """API view to return the list of frontpage services."""
for shortcut in frontpage.get_shortcuts()] # XXX: Get the module (or module name) from shortcut properly.
response = {'services': list(map(_get_service_data, services))} shortcuts = [
_get_shortcut_data(shortcut['id'].split('_')[0], shortcut)
for shortcut in frontpage.get_shortcuts()
]
response = {'shortcuts': shortcuts}
return HttpResponse( return HttpResponse(
json.dumps(response, cls=DjangoJSONEncoder), json.dumps(response, cls=DjangoJSONEncoder),
content_type="application/json") content_type='application/json')
def _get_service_data(service): def _get_shortcut_data(module_name, shortcut):
module = module_loader.loaded_modules[service] """Return detailed information about a shortcut."""
module = module_loader.loaded_modules[module_name]
def _getattr(attr, not_found=None):
"""A closure to get the enclosed module's attributes"""
return getattr(module, attr, not_found)
return { return {
key: value 'name': shortcut['name'],
for key, value in dict( 'short_description': shortcut['short_description'],
name=module.name, 'description': shortcut['details'],
short_description=_getattr('short_description'), 'icon_url': _get_icon_url(shortcut['icon']),
icon_url=_get_icon_url(_getattr('icon')), 'clients': getattr(module, 'clients', None)
description=_getattr('description'),
usage=_getattr('usage'),
manual_url=_getattr('manual_url'),
clients=_getattr('clients')).items()
} }
def _get_icon_url(icon): def _get_icon_url(icon_name):
return 'static/theme/icons/{}.svg'.format(icon) if icon else None """Return icon path given icon name."""
if not icon_name:
return None
return static('static/theme/icons/{}.png'.format(icon_name))