From 0120036a9086e826443598830344e8e17154067b Mon Sep 17 00:00:00 2001 From: Kenzo-Hugo Hillion <kenzo-hugo.hillion1@pasteur.fr> Date: Thu, 14 Nov 2019 17:40:36 +0100 Subject: [PATCH] replace endpoint by class to call different external APIs --- .../metagenedb/api/catalog/views/function.py | 8 +++++ backend/metagenedb/api/external/urls.py | 7 ---- .../metagenedb/api/external/views/__init__.py | 1 - .../metagenedb/api/external/views/togows.py | 25 --------------- backend/metagenedb/api/urls.py | 3 +- .../utils/external_api}/__init__.py | 0 .../common/utils/external_api/togows.py | 32 +++++++++++++++++++ 7 files changed, 41 insertions(+), 35 deletions(-) delete mode 100644 backend/metagenedb/api/external/urls.py delete mode 100644 backend/metagenedb/api/external/views/__init__.py delete mode 100644 backend/metagenedb/api/external/views/togows.py rename backend/metagenedb/{api/external => common/utils/external_api}/__init__.py (100%) create mode 100644 backend/metagenedb/common/utils/external_api/togows.py diff --git a/backend/metagenedb/api/catalog/views/function.py b/backend/metagenedb/api/catalog/views/function.py index 87d98ac..14906fa 100644 --- a/backend/metagenedb/api/catalog/views/function.py +++ b/backend/metagenedb/api/catalog/views/function.py @@ -1,6 +1,9 @@ +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) diff --git a/backend/metagenedb/api/external/urls.py b/backend/metagenedb/api/external/urls.py deleted file mode 100644 index acc8c56..0000000 --- a/backend/metagenedb/api/external/urls.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.urls import path - -from metagenedb.api.external.views import KeggInfoTogows - -urlpatterns = [ - path(r'togows/<kegg_id>', KeggInfoTogows.as_view(), name='kegg-info'), -] diff --git a/backend/metagenedb/api/external/views/__init__.py b/backend/metagenedb/api/external/views/__init__.py deleted file mode 100644 index fb67f87..0000000 --- a/backend/metagenedb/api/external/views/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .togows import KeggInfoTogows # noqa diff --git a/backend/metagenedb/api/external/views/togows.py b/backend/metagenedb/api/external/views/togows.py deleted file mode 100644 index 9fc60b1..0000000 --- a/backend/metagenedb/api/external/views/togows.py +++ /dev/null @@ -1,25 +0,0 @@ -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) diff --git a/backend/metagenedb/api/urls.py b/backend/metagenedb/api/urls.py index 5a521e8..7ab55a6 100644 --- a/backend/metagenedb/api/urls.py +++ b/backend/metagenedb/api/urls.py @@ -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'))) ] diff --git a/backend/metagenedb/api/external/__init__.py b/backend/metagenedb/common/utils/external_api/__init__.py similarity index 100% rename from backend/metagenedb/api/external/__init__.py rename to backend/metagenedb/common/utils/external_api/__init__.py diff --git a/backend/metagenedb/common/utils/external_api/togows.py b/backend/metagenedb/common/utils/external_api/togows.py new file mode 100644 index 0000000..3ea4910 --- /dev/null +++ b/backend/metagenedb/common/utils/external_api/togows.py @@ -0,0 +1,32 @@ +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)() + + + + + -- GitLab