@@ -39,72 +39,65 @@ def __init__(
3939 async def perform (self ) -> Union [T , None ]:
4040 """
4141 Async method to make an HTTP request to the JigsawStack API.
42-
43- Returns:
44- Union[T, None]: A generic type of the Request class or None
45-
46- Raises:
47- aiohttp.ClientResponseError: If the request fails
4842 """
4943 async with self .__get_session () as session :
5044 resp = await self .make_request (session , url = f"{ self .api_url } { self .path } " )
5145
52- # delete calls do not return a body
53- if await resp .text () == "" and resp .status == 200 :
54- return None
46+ # For binary responses
47+ if resp .status == 200 :
48+ content_type = resp .headers .get ("content-type" , "" )
49+ if content_type not in ["application/json" , "text/html" ]:
50+ content = await resp .read ()
51+ return cast (T , content )
5552
56- # safety net for non-JSON responses
57- content_type = resp .headers .get ("content-type" , "" )
58- if "application/json" not in content_type :
59- raise_for_code_and_type (
60- code = 500 ,
61- message = "Failed to parse JigsawStack API response. Please try again." ,
62- )
63-
64- # handle error responses
53+ # For error responses
6554 if resp .status != 200 :
66- error = await resp .json ()
67- raise_for_code_and_type (
68- code = resp .status ,
69- message = error .get ("message" ),
70- err = error .get ("error" ),
71- )
72-
73- return cast (T , await resp .json ())
74-
75- async def perform_file (self ) -> Union [aiohttp .ClientResponse , None ]:
76- """
77- Async method to make an HTTP request and return the raw response.
78-
79- Returns:
80- Union[aiohttp.ClientResponse, None]: The raw response object
81- """
55+ try :
56+ error = await resp .json ()
57+ raise_for_code_and_type (
58+ code = resp .status ,
59+ message = error .get ("message" ),
60+ err = error .get ("error" ),
61+ )
62+ except json .JSONDecodeError :
63+ raise_for_code_and_type (
64+ code = 500 ,
65+ message = "Failed to parse response. Invalid content type or encoding." ,
66+ )
67+
68+ # For JSON responses
69+ try :
70+ return cast (T , await resp .json ())
71+ except json .JSONDecodeError :
72+ content = await resp .read ()
73+ return cast (T , content )
74+
75+ async def perform_file (self ) -> Union [T , None ]:
8276 async with self .__get_session () as session :
8377 resp = await self .make_request (session , url = f"{ self .api_url } { self .path } " )
8478
85- # delete calls do not return a body
86- if await resp .text () == "" and resp .status == 200 :
87- return None
88-
89- # handle error responses
90- if (
91- "application/json" not in resp .headers .get ("content-type" , "" )
92- and resp .status != 200
93- ):
94- raise_for_code_and_type (
95- code = 500 ,
96- message = "Failed to parse JigsawStack API response. Please try again." ,
97- error_type = "InternalServerError" ,
98- )
99-
10079 if resp .status != 200 :
101- error = await resp .json ()
102- raise_for_code_and_type (
103- code = resp .status ,
104- message = error .get ("message" ),
105- err = error .get ("error" ),
106- )
107- return resp
80+ try :
81+ error = await resp .json ()
82+ raise_for_code_and_type (
83+ code = resp .status ,
84+ message = error .get ("message" ),
85+ err = error .get ("error" ),
86+ )
87+ except json .JSONDecodeError :
88+ raise_for_code_and_type (
89+ code = 500 ,
90+ message = "Failed to parse response. Invalid content type or encoding." ,
91+ )
92+
93+ # For binary responses
94+ if resp .status == 200 :
95+ content_type = resp .headers .get ("content-type" , "" )
96+ if "application/json" not in content_type :
97+ content = await resp .read ()
98+ return cast (T , content )
99+
100+ return cast (T , await resp .json ())
108101
109102 async def perform_with_content (self ) -> T :
110103 """
0 commit comments