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

externalize classes to call APIs

parent c5cb39cc
Pipeline #14327 passed with stages
in 2 minutes and 19 seconds
......@@ -53,6 +53,7 @@ metagenedb = {editable = true,path = "."}
drf-yasg = "*"
packaging = "*"
python-slugify = "*"
master = {git = "https://github.com/khillion/bioapi.git"}
[requires]
python_version = "3.7"
{
"_meta": {
"hash": {
"sha256": "8c5558c05e9194e5413000349af9f264e61330fd1b4bd37e1fdb1e5579b361f5"
"sha256": "ccf8681e4b40107fa82f5be41d08cd73348d3b3ebc0fdbad1a18dbf549297337"
},
"pipfile-spec": 6,
"requires": {
......@@ -55,11 +55,11 @@
},
"django": {
"hashes": [
"sha256:16a5d54411599780ac9dfe3b9b38f90f785c51259a584e0b24b6f14a7f69aae8",
"sha256:9a2f98211ab474c710fcdad29c82f30fc14ce9917c7a70c3682162a624de4035"
"sha256:148a4a2d1a85b23883b0a4e99ab7718f518a83675e4485e44dc0c1d36988c5fa",
"sha256:deb70aa038e59b58593673b15e9a711d1e5ccd941b5973b30750d5d026abfd56"
],
"index": "pypi",
"version": "==2.2.4"
"version": "==2.2.5"
},
"django-cors-headers": {
"hashes": [
......@@ -95,11 +95,11 @@
},
"djangorestframework": {
"hashes": [
"sha256:42979bd5441bb4d8fd69d0f385024a114c3cae7df0f110600b718751250f6929",
"sha256:aedb48010ebfab9651aaab1df5fd3b4848eb4182afc909852a2110c24f89a359"
"sha256:5488aed8f8df5ec1d70f04b2114abc52ae6729748a176c453313834a9ee179c8",
"sha256:dc81cbf9775c6898a580f6f1f387c4777d12bd87abf0f5406018d32ccae71090"
],
"index": "pypi",
"version": "==3.10.2"
"version": "==3.10.3"
},
"djangorestframework-jwt": {
"hashes": [
......@@ -177,6 +177,10 @@
],
"version": "==1.1.1"
},
"master": {
"git": "https://github.com/khillion/bioapi.git",
"ref": "201003535ff7b788492f056884b1ad166ee02b59"
},
"metagenedb": {
"editable": true,
"path": "."
......@@ -497,11 +501,11 @@
},
"importlib-metadata": {
"hashes": [
"sha256:23d3d873e008a513952355379d93cbcab874c58f4f034ff657c7a87422fa64e8",
"sha256:80d2de76188eabfbfcf27e6a37342c2827801e59c4cc14b0371c56fed43820e3"
"sha256:9ff1b1c5a354142de080b8a4e9803e5d0d59283c93aed808617c787d16768375",
"sha256:b7143592e374e50584564794fcb8aaf00a23025f9db866627f89a21491847a8d"
],
"index": "pypi",
"version": "==0.19"
"version": "==0.20"
},
"inflection": {
"hashes": [
......@@ -518,11 +522,11 @@
},
"ipython": {
"hashes": [
"sha256:1d3a1692921e932751bc1a1f7bb96dc38671eeefdc66ed33ee4cbc57e92a410e",
"sha256:537cd0176ff6abd06ef3e23f2d0c4c2c8a4d9277b7451544c6cbf56d1c79a83d"
"sha256:c4ab005921641e40a68e405e286e7a1fcc464497e14d81b6914b4fd95e5dee9b",
"sha256:dd76831f065f17bddd7eaa5c781f5ea32de5ef217592cf019e34043b56895aa1"
],
"markers": "python_version >= '3.3'",
"version": "==7.7.0"
"version": "==7.8.0"
},
"ipython-genutils": {
"hashes": [
......@@ -812,11 +816,11 @@
},
"pytest": {
"hashes": [
"sha256:95b1f6db806e5b1b5b443efeb58984c24945508f93a866c1719e1a507a957d7c",
"sha256:c3d5020755f70c82eceda3feaf556af9a341334414a8eca521a18f463bcead88"
"sha256:95d13143cc14174ca1a01ec68e84d76ba5d9d493ac02716fd9706c949a505210",
"sha256:b78fe2881323bd44fd9bd76e5317173d4316577e7b1cddebae9136a4495ec865"
],
"index": "pypi",
"version": "==5.1.1"
"version": "==5.1.2"
},
"pytest-cov": {
"hashes": [
......@@ -881,10 +885,10 @@
},
"qtconsole": {
"hashes": [
"sha256:40f53ab58ef77aa4b53aca1ee314eed873d05952dec22d2ab84d20660943b7de",
"sha256:756bdcb6de6900dc50b14430accff2e47b846c3e7820e04075d4067b4c0ab52f"
"sha256:40d5d8e00d070ea266dbf6f0da74c4b9597b8b8d67cd8233c3ffd8debf923703",
"sha256:b91e7412587e6cfe1644696538f73baf5611e837be5406633218443b2827c6d9"
],
"version": "==4.5.4"
"version": "==4.5.5"
},
"send2trash": {
"hashes": [
......
from .togows import TogoWSEntryAPI # noqa
from .metagenedb import (MetageneDBCatalogGeneAPI, MetageneDBCatalogTaxonomyAPI, # noqa
MetageneDBCatalogFunctionAPI)
import logging
import requests
from urllib.parse import urljoin
logging.basicConfig(level=logging.INFO)
_LOGGER = logging.getLogger(__name__)
class LoggedSession(requests.Session):
def request(self, method, url, **kwargs):
_LOGGER.debug(f"{method.upper()} {url}")
response = super().request(method, url, **kwargs)
_LOGGER.debug(f"STATUS CODE: {response.status_code}")
return response
class BaseAPI(object):
BASE_URL = ''
ROUTE = ''
HEADERS = {
'Content-type': 'application/json',
'Accept': '*/*'
}
SESSION = LoggedSession
def __init__(self):
if not getattr(self, 'base_url', None):
self.base_url = self.BASE_URL
if not getattr(self, 'route', None):
self.route = self.ROUTE
self.url = urljoin(self.base_url, self.route)
self.session = self.SESSION()
self.session.headers.update(self.HEADERS)
def get_all(self, params=None):
response = self.session.get(self.url, params=params)
response.raise_for_status()
return response.json()
def get(self, entry_id):
full_url = urljoin(self.url, entry_id)
response = self.session.get(full_url)
response.raise_for_status()
return response.json()
def post(self, data):
response = self.session.post(f"{self.url}", json=data)
response.raise_for_status()
return response.json()
def put(self, data, entry_id=None):
if entry_id:
full_url = urljoin(self.url, entry_id)
else:
full_url = self.url
response = self.session.put(f"{full_url}/", json=data)
response.raise_for_status()
return response.json()
from .baseapi import BaseAPI
class MetageneDBAPI(BaseAPI):
BASE_URL = 'http://localhost/'
def __init__(self, base_url=BASE_URL):
self.base_url = base_url
super().__init__()
class MetageneDBCatalogGeneAPI(MetageneDBAPI):
ROUTE = 'api/catalog/v1/genes/'
class MetageneDBCatalogTaxonomyAPI(MetageneDBAPI):
ROUTE = 'api/catalog/v1/taxonomy/'
class MetageneDBCatalogFunctionAPI(MetageneDBAPI):
ROUTE = 'api/catalog/v1/functions/'
from urllib.parse import urljoin
from .baseapi import BaseAPI
class TogoWSAPI(BaseAPI):
BASE_URL = 'http://togows.org'
class TogoWSEntryAPI(TogoWSAPI):
TYPE = 'entry'
def __init__(self, database, entry_format='json'):
super().__init__()
self.database = database
self.format = entry_format
self.route = f"{self.TYPE}/{self.database}/"
self.url = urljoin(self.BASE_URL, self.route)
def get(self, entry_id):
return super().get(f"{entry_id}.{self.format}")
def get_field(self, entry_id, field):
return super().get(f"{entry_id}/{field}.{self.format}")
from requests.exceptions import HTTPError
from bioapi import MetageneDBCatalogGeneAPI
from django.urls import reverse
from django.utils.http import urlencode
from metagenedb.common.utils.api import MetageneDBCatalogGeneAPI
class MetageneDBAPIMock(MetageneDBCatalogGeneAPI):
"""
......
......@@ -5,9 +5,9 @@ import sys
from itertools import islice
from requests.exceptions import HTTPError
from bioapi import MetageneDBCatalogGeneAPI, MetageneDBCatalogTaxonomyAPI
from slugify import slugify
from metagenedb.common.utils.api import MetageneDBCatalogGeneAPI, MetageneDBCatalogTaxonomyAPI
from metagenedb.common.utils.parsers import IGCLineParser
logging.basicConfig(level=logging.INFO)
......
......@@ -7,9 +7,9 @@ import sys
from requests.exceptions import HTTPError
import django
from bioapi import MetageneDBCatalogFunctionAPI
from django.core.exceptions import ValidationError
from metagenedb.common.utils.api import MetageneDBCatalogFunctionAPI
from metagenedb.common.utils.parsers import KEGGLineParser
# Before model import, we need to called django.setup() to Load apps
......
Supports Markdown
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