Commit 0120036a authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

replace endpoint by class to call different external APIs

parent 9d65ac11
from rest_framework.response import Response
from metagenedb.api.catalog.filters import FunctionFilter
from metagenedb.apps.catalog.models import Function
from metagenedb.apps.catalog.serializers import FunctionSerializer
from metagenedb.common.utils.external_api.togows import get_kegg
from .bulk_viewset import BulkViewSet
......@@ -10,3 +13,8 @@ class FunctionViewSet(BulkViewSet):
serializer_class = FunctionSerializer
lookup_field = 'function_id'
filterset_class = FunctionFilter
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
from django.urls import path
from metagenedb.api.external.views import KeggInfoTogows
urlpatterns = [
path(r'togows/<kegg_id>', KeggInfoTogows.as_view(), name='kegg-info'),
]
from .togows import KeggInfoTogows # noqa
from bioapi.togows import TogoWSEntryAPI
from requests.exceptions import HTTPError
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.status import (HTTP_200_OK, HTTP_404_NOT_FOUND)
class KeggInfoTogows(APIView):
"""
Obtain detailed information about KEGG orthology entry.
"""
def get(self, request, kegg_id=None):
"""
Return a list of all users.
"""
kegg_api = TogoWSEntryAPI("kegg-orthology")
try:
content = kegg_api.get(kegg_id)[0]
status = HTTP_200_OK
except HTTPError as http_err:
content = [{'error': str(http_err)}]
status = HTTP_404_NOT_FOUND
return Response(content, status=status)
......@@ -3,6 +3,5 @@ from django.urls import include, path
urlpatterns = [
path('auth/', include(('metagenedb.api.accounts.urls', 'auth'))),
path('catalog/', include(('metagenedb.api.catalog.urls', 'catalog'))),
path('external/', include(('metagenedb.api.external.urls', 'external')))
path('catalog/', include(('metagenedb.api.catalog.urls', 'catalog')))
]
import logging
from bioapi.togows import TogoWSEntryAPI
logger = logging.getLogger(__name__)
class GetFunctionExternalInfo:
def __init__(self, function_id, source):
self.function_id = function_id
self.source = source
def _get_unknown_source(self):
logger.warning("No source of information for %s from %s" % (self.function_id, self.source))
return {}
def _get_kegg(self):
"""
Get detailed information from KEGG orthology through Togows.
"""
kegg_api = TogoWSEntryAPI("kegg-orthology")
return kegg_api.get(self.function_id)[0]
def get_details(self):
return getattr(self, f"_get_{self.source}", self._get_unknown_source)()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment