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

make common class for API calls

parent 197b32bd
from .togows import TogoWSEntry # noqa
from .metagenedb import MetageneDBCatalogGene # noqa
import requests
from urllib.parse import urljoin
class BaseAPI(object):
BASE_URL = ''
ROUTE = ''
HEADERS = {
'Content-type': 'application/json',
'Accept': '*/*'
}
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)
def get_all(self):
response = requests.get(self.url)
response.raise_for_status()
return response.json()
def get(self, entry_id):
full_url = urljoin(self.url, entry_id)
response = requests.get(full_url)
response.raise_for_status()
return response.json()
def post(self, data):
response = requests.post(f"{self.url}", json=data, headers=self.HEADERS)
response.raise_for_status()
return response.json()
def put(self, entry_id, data):
full_url = urljoin(self.url, entry_id)
response = requests.put(f"{full_url}/", json=data, headers=self.HEADERS)
response.raise_for_status()
return response.json()
import logging
import requests
from abc import ABC
from urllib.parse import urljoin
from .baseapi import BaseAPI
logging.basicConfig(level=logging.INFO)
_LOGGER = logging.getLogger(__name__)
class MetageneDB(ABC):
class MetageneDB(BaseAPI):
BASE_URL = 'http://localhost/'
ROUTE = ''
HEADERS = {
'Content-type': 'application/json',
'Accept': '*/*'
}
def __init__(self, base_url=BASE_URL):
self.url = urljoin(base_url, self.ROUTE)
self.base_url = base_url
super().__init__()
class MetageneDBCatalogGene(MetageneDB):
ROUTE = 'api/catalog/v1/genes/'
def get_genes(self):
response = requests.get(self.url)
response.raise_for_status()
return response.json()
def get(self, gene_id):
full_url = urljoin(self.url, gene_id)
response = requests.get(full_url)
response.raise_for_status()
return response.json()
def post(self, data):
response = requests.post(f"{self.url}", json=data, headers=self.HEADERS)
response.raise_for_status()
return response.json()
def put(self, gene_id, data):
full_url = urljoin(self.url, gene_id)
response = requests.put(f"{full_url}/", json=data, headers=self.HEADERS)
response.raise_for_status()
return response.json()
from abc import abstractmethod, ABC
import requests
from urllib.parse import urljoin
from .baseapi import BaseAPI
class TogoWS(ABC):
BASE_URL = 'http://togows.org'
@abstractmethod
def get(self, entry_id):
pass
class TogoWS(BaseAPI):
BASE_URL = 'http://togows.org'
class TogoWSEntry(TogoWS):
TYPE = 'entry'
def __init__(self, database, format='json'):
def __init__(self, database, entry_format='json'):
self.database = database
self.format = format
self.format = entry_format
self.route = f"{self.TYPE}/{self.database}/"
self.url = urljoin(self.BASE_URL, self.route)
def get(self, entry_id):
full_url = f"{self.BASE_URL}/{self.TYPE}/{self.database}/{entry_id}.{self.format}"
response = requests.get(full_url)
response.raise_for_status()
return response.json()
return super().get(f"{entry_id}.{self.format}")
def get_field(self, entry_id, field):
full_url = f"{self.BASE_URL}/{self.TYPE}/{self.database}/{entry_id}.{field}.{self.format}"
response = requests.get(full_url)
response.raise_for_status()
return response.json()
return super().get(f"{entry_id}/{field}.{self.format}")
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