22import requests
33from socketdev .core .classes import Response
44from 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 ,
815)
916from socketdev .version import __version__
1017from requests .exceptions import Timeout , ConnectionError
@@ -24,7 +31,12 @@ def set_timeout(self, timeout: int):
2431 self .request_timeout = timeout
2532
2633 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" ,
2840 ) -> Response :
2941 if self .encoded_key is None or self .encoded_key == "" :
3042 raise APIKeyMissing
@@ -36,33 +48,39 @@ def do_request(
3648 "accept" : "application/json" ,
3749 }
3850 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+
3955 try :
4056 start_time = time .time ()
4157 response = requests .request (
4258 method .upper (), url , headers = headers , data = payload , files = files , timeout = self .request_timeout
4359 )
4460 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+
4665 if response .status_code == 401 :
47- raise APIAccessDenied ("Unauthorized" )
66+ raise APIAccessDenied (f "Unauthorized{ path_str } { headers_str } " )
4867 if response .status_code == 403 :
4968 try :
50- error_message = response .json ().get (' error' , {}).get (' message' , '' )
69+ error_message = response .json ().get (" error" , {}).get (" message" , "" )
5170 if "Insufficient permissions for API method" in error_message :
52- raise APIInsufficientPermissions (error_message )
71+ raise APIInsufficientPermissions (f" { error_message } { path_str } { headers_str } " )
5372 elif "Organization not allowed" in error_message :
54- raise APIOrganizationNotAllowed (error_message )
73+ raise APIOrganizationNotAllowed (f" { error_message } { path_str } { headers_str } " )
5574 elif "Insufficient max quota" in error_message :
56- raise APIInsufficientQuota (error_message )
75+ raise APIInsufficientQuota (f" { error_message } { path_str } { headers_str } " )
5776 else :
58- raise APIAccessDenied (error_message or " Access denied" )
77+ raise APIAccessDenied (f" { error_message or ' Access denied' } { path_str } { headers_str } " )
5978 except ValueError :
60- # If JSON parsing fails
61- raise APIAccessDenied ("Access denied" )
79+ raise APIAccessDenied (f"Access denied{ path_str } { headers_str } " )
6280 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 } " )
6482 if response .status_code == 429 :
65- retry_after = response .headers .get (' retry-after' )
83+ retry_after = response .headers .get (" retry-after" )
6684 if retry_after :
6785 try :
6886 seconds = int (retry_after )
@@ -73,23 +91,34 @@ def do_request(
7391 time_msg = f" Retry after: { retry_after } "
7492 else :
7593 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 } " )
7795 if response .status_code == 502 :
78- raise APIBadGateway ("Upstream server error" )
96+ raise APIBadGateway (f "Upstream server error{ path_str } { headers_str } " )
7997 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+
82103 return response
83-
104+
84105 except Timeout :
85106 request_duration = time .time () - start_time
86107 raise APITimeout (f"Request timed out after { request_duration :.2f} seconds" )
87108 except ConnectionError as error :
88109 request_duration = time .time () - start_time
89110 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+ ):
93122 # Let all our custom exceptions propagate up unchanged
94123 raise
95124 except Exception as error :
0 commit comments