Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions dimo/request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import json
from requests import Session
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:
Expand All @@ -20,18 +30,28 @@ def __call__(self, headers=None, data=None, params=None, **kwargs):
):
data = json.dumps(data)

response = self.session.request(
method=self.http_method,
url=self.url,
headers=headers,
params=params,
data=data,
**kwargs,
)

# TODO: Better error responses
response.raise_for_status()

if response.content:
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 "application/json" in content_type:
return response.json()
return None

return response.content