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

add detailed option for function API

parent eaa9d70a
Pipeline #18285 passed with stages
in 2 minutes and 22 seconds
import logging
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 GetFunctionExternalInfo
from .bulk_viewset import BulkViewSet
logger = logging.getLogger(__name__)
class FunctionViewSet(BulkViewSet):
queryset = Function.objects.all()
serializer_class = FunctionSerializer
lookup_field = 'function_id'
filterset_class = FunctionFilter
def _get_external_info(self, db_data):
detailed_info_retriever = GetFunctionExternalInfo(db_data['function_id'],
db_data['source'])
detailed_data = detailed_info_retriever.get_details() # @TODO check consistency?
return detailed_data
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
returned_data = serializer.data
if request.query_params.get('detailed', None):
returned_data = self._get_external_info(returned_data)
return Response(returned_data)
import logging
from django.conf import settings
from bioapi.togows import TogoWSEntryAPI
......@@ -21,7 +23,13 @@ class GetFunctionExternalInfo:
Get detailed information from KEGG orthology through Togows.
"""
kegg_api = TogoWSEntryAPI("kegg-orthology")
return kegg_api.get(self.function_id)[0]
response = kegg_api.get(self.function_id)[0]
response[settings.API_KEY_ADDITIONAL_INFO] = {
'comment': f"Information retrieved from external source: {kegg_api.url}",
'url': f"{kegg_api.url}{self.function_id}"
}
return response
def get_details(self):
logger.info("Retrieving information from KEGG through togows")
return getattr(self, f"_get_{self.source}", self._get_unknown_source)()
......@@ -146,3 +146,6 @@ STATIC_ROOT = public_root('static')
STATIC_URL = env.str('STATIC_URL', default='/static/')
SECRET_KEY = env.str('SECRET_KEY', default='')
# -- key for API when additional information is added to the payload
API_KEY_ADDITIONAL_INFO = env.str('API_KEY_ADDITIONAL_INFO', default='metagenedb_additional_info')
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