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
from urllib.parse import urljoin
_LOGGER = logging.getLogger(__name__)
class LoggedSession(requests.Session):
def request(self, method, url, **kwargs):"{method.upper()} {url}")
response = super().request(method, url, **kwargs)"STATUS CODE: {response.status_code}")
return response
class BaseAPI(object):
......@@ -9,6 +22,7 @@ class BaseAPI(object):
'Content-type': 'application/json',
'Accept': '*/*'
SESSION = LoggedSession
def __init__(self):
if not getattr(self, 'base_url', None):
......@@ -16,25 +30,27 @@ class BaseAPI(object):
if not getattr(self, 'route', None):
self.route = self.ROUTE
self.url = urljoin(self.base_url, self.route)
self.session = self.SESSION()
def get_all(self):
response = requests.get(self.url)
response = self.session.get(self.url)
return response.json()
def get(self, entry_id):
full_url = urljoin(self.url, entry_id)
response = requests.get(full_url)
response = self.session.get(full_url)
return response.json()
def post(self, data):
response ="{self.url}", json=data, headers=self.HEADERS)
response ="{self.url}", json=data)
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 = self.session.put(f"{full_url}/", json=data)
return response.json()
......@@ -11,6 +11,7 @@ class TogoWSEntry(TogoWS):
TYPE = 'entry'
def __init__(self, database, entry_format='json'):
self.database = database
self.format = entry_format
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