Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 43a2450

Browse files
authored
fix: Handle non-json response (#133)
* Handle non-json response * Export SeamAPIException * Use .get to get error meta
1 parent 93aa9fa commit 43a2450

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

seamapi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
# type: ignore
33

44
from seamapi.seam import Seam
5+
from seamapi.seam import SeamAPIException

seamapi/seam.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,10 @@ def make_request(self, method: str, path: str, **kwargs):
125125
)
126126

127127

128-
parsed_response = response.json()
129-
130128
if response.status_code != 200:
131-
raise SeamAPIException(
132-
response.status_code,
133-
response.headers["seam-request-id"],
134-
parsed_response["error"],
135-
)
129+
raise SeamAPIException(response)
130+
131+
if "application/json" in response.headers["content-type"]:
132+
return response.json()
136133

137-
return parsed_response
134+
return response.text

seamapi/types.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
class SeamAPIException(Exception):
2323
def __init__(
2424
self,
25-
status_code: int,
26-
request_id: str,
27-
metadata: Optional[Dict[str, any]],
25+
response,
2826
):
29-
self.status_code = status_code
30-
self.request_id = request_id
31-
self.metadata = metadata
27+
self.status_code = response.status_code
28+
self.request_id = response.headers.get("seam-request-id", None)
29+
30+
self.metadata = None
31+
if "application/json" in response.headers["content-type"]:
32+
parsed_response = response.json()
33+
self.metadata = parsed_response.get("error", None)
3234

3335
super().__init__(
34-
f"SeamAPIException: status={status_code}, request_id={request_id}, metadata={metadata}"
36+
f"SeamAPIException: status={self.status_code}, request_id={self.request_id}, metadata={self.metadata}"
3537
)
3638

3739

0 commit comments

Comments
 (0)