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

Add logger to requests

parent 6743c3d3
Pipeline #13970 passed with stages
in 1 minute and 40 seconds
import logging
import requests import requests
from urllib.parse import urljoin 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.info(f"{method.upper()} {url}")
response = super().request(method, url, **kwargs)
_LOGGER.info(f"STATUS CODE: {response.status_code}")
return response
class BaseAPI(object): class BaseAPI(object):
BASE_URL = '' BASE_URL = ''
...@@ -9,6 +22,7 @@ class BaseAPI(object): ...@@ -9,6 +22,7 @@ class BaseAPI(object):
'Content-type': 'application/json', 'Content-type': 'application/json',
'Accept': '*/*' 'Accept': '*/*'
} }
SESSION = LoggedSession
def __init__(self): def __init__(self):
if not getattr(self, 'base_url', None): if not getattr(self, 'base_url', None):
...@@ -16,25 +30,27 @@ class BaseAPI(object): ...@@ -16,25 +30,27 @@ class BaseAPI(object):
if not getattr(self, 'route', None): if not getattr(self, 'route', None):
self.route = self.ROUTE self.route = self.ROUTE
self.url = urljoin(self.base_url, self.route) self.url = urljoin(self.base_url, self.route)
self.session = self.SESSION()
self.session.headers.update(self.HEADERS)
def get_all(self): def get_all(self):
response = requests.get(self.url) response = self.session.get(self.url)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def get(self, entry_id): def get(self, entry_id):
full_url = urljoin(self.url, entry_id) full_url = urljoin(self.url, entry_id)
response = requests.get(full_url) response = self.session.get(full_url)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def post(self, data): def post(self, data):
response = requests.post(f"{self.url}", json=data, headers=self.HEADERS) response = self.session.post(f"{self.url}", json=data)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def put(self, entry_id, data): def put(self, entry_id, data):
full_url = urljoin(self.url, entry_id) full_url = urljoin(self.url, entry_id)
response = requests.put(f"{full_url}/", json=data, headers=self.HEADERS) response = self.session.put(f"{full_url}/", json=data)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
...@@ -11,6 +11,7 @@ class TogoWSEntry(TogoWS): ...@@ -11,6 +11,7 @@ class TogoWSEntry(TogoWS):
TYPE = 'entry' TYPE = 'entry'
def __init__(self, database, entry_format='json'): def __init__(self, database, entry_format='json'):
super().__init__()
self.database = database self.database = database
self.format = entry_format self.format = entry_format
self.route = f"{self.TYPE}/{self.database}/" self.route = f"{self.TYPE}/{self.database}/"
......
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