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,39 @@ 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
+ path_str = f"\n Path: { url } "
64
+
46
65
if response .status_code == 401 :
47
- raise APIAccessDenied ("Unauthorized" )
66
+ raise APIAccessDenied (f "Unauthorized{ path_str } { headers_str } " )
48
67
if response .status_code == 403 :
49
68
try :
50
- error_message = response .json ().get (' error' , {}).get (' message' , '' )
69
+ error_message = response .json ().get (" error" , {}).get (" message" , "" )
51
70
if "Insufficient permissions for API method" in error_message :
52
- raise APIInsufficientPermissions (error_message )
71
+ raise APIInsufficientPermissions (f" { error_message } { path_str } { headers_str } " )
53
72
elif "Organization not allowed" in error_message :
54
- raise APIOrganizationNotAllowed (error_message )
73
+ raise APIOrganizationNotAllowed (f" { error_message } { path_str } { headers_str } " )
55
74
elif "Insufficient max quota" in error_message :
56
- raise APIInsufficientQuota (error_message )
75
+ raise APIInsufficientQuota (f" { error_message } { path_str } { headers_str } " )
57
76
else :
58
- raise APIAccessDenied (error_message or " Access denied" )
77
+ raise APIAccessDenied (f" { error_message or ' Access denied' } { path_str } { headers_str } " )
59
78
except ValueError :
60
- # If JSON parsing fails
61
- raise APIAccessDenied ("Access denied" )
79
+ raise APIAccessDenied (f"Access denied{ path_str } { headers_str } " )
62
80
if response .status_code == 404 :
63
- raise APIResourceNotFound (f"Path not found { path } " )
81
+ raise APIResourceNotFound (f"Path not found { path } { path_str } { headers_str } " )
64
82
if response .status_code == 429 :
65
- retry_after = response .headers .get (' retry-after' )
83
+ retry_after = response .headers .get (" retry-after" )
66
84
if retry_after :
67
85
try :
68
86
seconds = int (retry_after )
@@ -73,23 +91,34 @@ def do_request(
73
91
time_msg = f" Retry after: { retry_after } "
74
92
else :
75
93
time_msg = ""
76
- raise APIInsufficientQuota (f"Insufficient quota for API route.{ time_msg } " )
94
+ raise APIInsufficientQuota (f"Insufficient quota for API route.{ time_msg } { path_str } { headers_str } " )
77
95
if response .status_code == 502 :
78
- raise APIBadGateway ("Upstream server error" )
96
+ raise APIBadGateway (f "Upstream server error{ path_str } { headers_str } " )
79
97
if response .status_code >= 400 :
80
- raise APIFailure (f"Bad Request: HTTP { response .status_code } " )
81
-
98
+ raise APIFailure (
99
+ f"Bad Request: HTTP original_status_code:{ response .status_code } { path_str } { headers_str } " ,
100
+ status_code = 500 ,
101
+ )
102
+
82
103
return response
83
-
104
+
84
105
except Timeout :
85
106
request_duration = time .time () - start_time
86
107
raise APITimeout (f"Request timed out after { request_duration :.2f} seconds" )
87
108
except ConnectionError as error :
88
109
request_duration = time .time () - start_time
89
110
raise APIConnectionError (f"Connection error after { request_duration :.2f} seconds: { error } " )
90
- except (APIAccessDenied , APIInsufficientQuota , APIResourceNotFound , APIFailure ,
91
- APITimeout , APIConnectionError , APIBadGateway , APIInsufficientPermissions ,
92
- APIOrganizationNotAllowed ):
111
+ except (
112
+ APIAccessDenied ,
113
+ APIInsufficientQuota ,
114
+ APIResourceNotFound ,
115
+ APIFailure ,
116
+ APITimeout ,
117
+ APIConnectionError ,
118
+ APIBadGateway ,
119
+ APIInsufficientPermissions ,
120
+ APIOrganizationNotAllowed ,
121
+ ):
93
122
# Let all our custom exceptions propagate up unchanged
94
123
raise
95
124
except Exception as error :
0 commit comments