2
2
import requests
3
3
from socketdev .core .classes import Response
4
4
from socketdev .exceptions import (
5
- APIKeyMissing , APIFailure , APIAccessDenied , APIInsufficientQuota ,
6
- APIResourceNotFound , APITimeout , APIConnectionError , APIBadGateway ,
7
- APIInsufficientPermissions , APIOrganizationNotAllowed
5
+ APIKeyMissing ,
6
+ APIFailure ,
7
+ APIAccessDenied ,
8
+ APIInsufficientQuota ,
9
+ APIResourceNotFound ,
10
+ APITimeout ,
11
+ APIConnectionError ,
12
+ APIBadGateway ,
13
+ APIInsufficientPermissions ,
14
+ APIOrganizationNotAllowed ,
8
15
)
9
16
from socketdev .version import __version__
10
17
from requests .exceptions import Timeout , ConnectionError
@@ -24,7 +31,12 @@ def set_timeout(self, timeout: int):
24
31
self .request_timeout = timeout
25
32
26
33
def do_request (
27
- self , path : str , headers : dict | None = None , payload : [dict , str ] = None , files : list = None , method : str = "GET"
34
+ self ,
35
+ path : str ,
36
+ headers : dict | None = None ,
37
+ payload : [dict , str ] = None ,
38
+ files : list = None ,
39
+ method : str = "GET" ,
28
40
) -> Response :
29
41
if self .encoded_key is None or self .encoded_key == "" :
30
42
raise APIKeyMissing
@@ -36,33 +48,38 @@ def do_request(
36
48
"accept" : "application/json" ,
37
49
}
38
50
url = f"{ self .api_url } /{ path } "
51
+
52
+ def format_headers (headers_dict ):
53
+ return "\n " .join (f"{ k } : { v } " for k , v in headers_dict .items ())
54
+
39
55
try :
40
56
start_time = time .time ()
41
57
response = requests .request (
42
58
method .upper (), url , headers = headers , data = payload , files = files , timeout = self .request_timeout
43
59
)
44
60
request_duration = time .time () - start_time
45
-
61
+
62
+ headers_str = f"\n \n Headers:\n { format_headers (response .headers )} " if response .headers else ""
63
+
46
64
if response .status_code == 401 :
47
- raise APIAccessDenied ("Unauthorized" )
65
+ raise APIAccessDenied (f "Unauthorized{ headers_str } " )
48
66
if response .status_code == 403 :
49
67
try :
50
- error_message = response .json ().get (' error' , {}).get (' message' , '' )
68
+ error_message = response .json ().get (" error" , {}).get (" message" , "" )
51
69
if "Insufficient permissions for API method" in error_message :
52
- raise APIInsufficientPermissions (error_message )
70
+ raise APIInsufficientPermissions (f" { error_message } { headers_str } " )
53
71
elif "Organization not allowed" in error_message :
54
- raise APIOrganizationNotAllowed (error_message )
72
+ raise APIOrganizationNotAllowed (f" { error_message } { headers_str } " )
55
73
elif "Insufficient max quota" in error_message :
56
- raise APIInsufficientQuota (error_message )
74
+ raise APIInsufficientQuota (f" { error_message } { headers_str } " )
57
75
else :
58
- raise APIAccessDenied (error_message or " Access denied" )
76
+ raise APIAccessDenied (f" { error_message or ' Access denied' } { headers_str } " )
59
77
except ValueError :
60
- # If JSON parsing fails
61
- raise APIAccessDenied ("Access denied" )
78
+ raise APIAccessDenied (f"Access denied{ headers_str } " )
62
79
if response .status_code == 404 :
63
- raise APIResourceNotFound (f"Path not found { path } " )
80
+ raise APIResourceNotFound (f"Path not found { path } { headers_str } " )
64
81
if response .status_code == 429 :
65
- retry_after = response .headers .get (' retry-after' )
82
+ retry_after = response .headers .get (" retry-after" )
66
83
if retry_after :
67
84
try :
68
85
seconds = int (retry_after )
@@ -73,23 +90,31 @@ def do_request(
73
90
time_msg = f" Retry after: { retry_after } "
74
91
else :
75
92
time_msg = ""
76
- raise APIInsufficientQuota (f"Insufficient quota for API route.{ time_msg } " )
93
+ raise APIInsufficientQuota (f"Insufficient quota for API route.{ time_msg } { headers_str } " )
77
94
if response .status_code == 502 :
78
95
raise APIBadGateway ("Upstream server error" )
79
96
if response .status_code >= 400 :
80
- raise APIFailure (f"Bad Request: HTTP { response .status_code } " )
81
-
97
+ raise APIFailure (f"Bad Request: HTTP { response .status_code } { headers_str } " )
98
+
82
99
return response
83
-
100
+
84
101
except Timeout :
85
102
request_duration = time .time () - start_time
86
103
raise APITimeout (f"Request timed out after { request_duration :.2f} seconds" )
87
104
except ConnectionError as error :
88
105
request_duration = time .time () - start_time
89
106
raise APIConnectionError (f"Connection error after { request_duration :.2f} seconds: { error } " )
90
- except (APIAccessDenied , APIInsufficientQuota , APIResourceNotFound , APIFailure ,
91
- APITimeout , APIConnectionError , APIBadGateway , APIInsufficientPermissions ,
92
- APIOrganizationNotAllowed ):
107
+ except (
108
+ APIAccessDenied ,
109
+ APIInsufficientQuota ,
110
+ APIResourceNotFound ,
111
+ APIFailure ,
112
+ APITimeout ,
113
+ APIConnectionError ,
114
+ APIBadGateway ,
115
+ APIInsufficientPermissions ,
116
+ APIOrganizationNotAllowed ,
117
+ ):
93
118
# Let all our custom exceptions propagate up unchanged
94
119
raise
95
120
except Exception as error :
0 commit comments