forked from DIMO-Network/dimo-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.py
More file actions
57 lines (45 loc) · 1.68 KB
/
request.py
File metadata and controls
57 lines (45 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
from typing import Any
from requests import Session, RequestException
class HTTPError(Exception):
"""Http error wrapper with status code and (optional) response body"""
def __init__(self, status: int, message: str, body: Any = None):
super().__init__(f"HTTP {status}: {message}")
self.status = status
self.body = body
class Request:
def __init__(self, http_method: str, url: str, session: Session):
self.http_method = http_method
self.url = url
self.session = session
def __call__(self, headers=None, data=None, params=None, **kwargs):
headers = headers or {}
headers.update(kwargs.pop("headers", {}))
if (
data
and isinstance(data, dict)
and headers.get("Content-Type") == "application/json"
):
data = json.dumps(data)
try:
response = self.session.request(
method=self.http_method,
url=self.url,
headers=headers,
params=params,
data=data,
**kwargs,
)
response.raise_for_status()
except RequestException as exc:
status = getattr(exc.response, "status_code", None)
body = None
try:
body = exc.response.json()
except Exception:
body = exc.response.txt if exc.response else None
raise HTTPError(status=status or -1, message=str(exc), body=body)
content_type = response.headers.get("Content-Type", "")
if "json" in content_type:
return response.json()
return response.content