27 lines
788 B
Python
27 lines
788 B
Python
import requests
|
|
|
|
|
|
class HttpClient:
|
|
def __init__(self, endpoint, token):
|
|
self._endpoint = endpoint.rstrip("/")
|
|
self._session = requests.Session()
|
|
self._session.headers.update({
|
|
"Authorization": f"Bearer {token}",
|
|
"User-Agent": "Mozilla/5.0",
|
|
})
|
|
|
|
def get(self, path, **kwargs):
|
|
kwargs.setdefault("timeout", 10)
|
|
r = self._session.get(f"{self._endpoint}{path}", **kwargs)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
def post(self, path, data=None, **kwargs):
|
|
kwargs.setdefault("timeout", 30)
|
|
r = self._session.post(f"{self._endpoint}{path}", json=data, **kwargs)
|
|
r.raise_for_status()
|
|
try:
|
|
return r.json()
|
|
except Exception:
|
|
return {}
|