@@ -121,14 +121,16 @@ def pprint_request_info(url, method, _headers, **kwargs):
121121
122122 format_str = "{}\n \t {}:\n {}"
123123 debug_print = format_str .format (debug_print , key , value )
124- return debug_print
124+ return debug_print . replace ( ' \n ' , ' \\ n' )
125125
126126
127- def pprint_response_info (response ):
128- debug_print = "\n Response" "\n \t Status: {} - {}" "\n \t Headers: \n {}"
129- headers = response .headers
130- headers = "\n " .join (["\t \t {}: {}" .format (a , b ) for a , b in headers .items ()])
127+ def pprint_response_info (response , is_success , show_headers = False ):
128+ headers = None
131129 body = None
130+ success_txt = 'Success' if is_success else 'Error'
131+ if show_headers :
132+ headers = response .headers
133+ headers = "\n " .join (["\t \t {}: {}" .format (a , b ) for a , b in headers .items ()])
132134 file_resp_headers = ["Content-Disposition" , "fileName" ]
133135
134136 if "application/json" in response .headers .get ("Content-Type" ):
@@ -144,13 +146,18 @@ def pprint_response_info(response):
144146 else :
145147 body = response .text or response .content
146148
147- debug_print = debug_print .format (response .status_code , response .reason , headers )
149+ if show_headers :
150+ debug_print = '\n {} Response\n \t Status: {} - {}\n \t Headers: \n {}'
151+ debug_print = debug_print .format (success_txt , response .status_code , response .reason , headers )
152+ else :
153+ debug_print = '\n {} Response\n \t Status: {} - {}'
154+ debug_print = debug_print .format (success_txt , response .status_code , response .reason )
148155
149156 if body is not None :
150157 format_str = "{}\n \t {}:\n {}"
151158 debug_print = format_str .format (debug_print , "Body" , body )
152159
153- return debug_print
160+ return debug_print . replace ( ' \n ' , ' \\ n' )
154161
155162
156163class Authentication (object ):
@@ -201,8 +208,8 @@ def authentication_api(self, username, password):
201208 )
202209
203210 if response .status_code not in [200 , 201 , 202 , 204 , 206 ]:
204- self ._helper .log_debug (pprint_response_info (response ))
205- raise Exception ("Authentication error" )
211+ self ._helper .log_debug (pprint_response_info (response , is_success = False , show_headers = True ))
212+ raise Exception ("Authentication error: {error}" . format ( error = pprint_response_info ( response , is_success = False , show_headers = True )) )
206213
207214 json_data = extract_and_parse_json (response )
208215
@@ -386,17 +393,17 @@ def request(self, method, url, custom_refresh, **kwargs):
386393 time .sleep (response .retry_after )
387394 continue
388395 if response .status_code == 401 and custom_refresh < 1 :
389- self ._helper .log_debug (pprint_response_info (response ))
396+ self ._helper .log_debug (pprint_response_info (response , is_success = False , show_headers = False ))
390397 self ._helper .log_debug ("Refreshing access token" )
391398 self .refresh_token ()
392399 self ._helper .log_debug ("Refreshed access token" )
393400 return self .request (method , url , 1 , ** kwargs )
394401 else :
395402 # Re-raise the error
396- self ._helper .log_debug (pprint_response_info (response ))
403+ self ._helper .log_debug (pprint_response_info (response , is_success = False , show_headers = False ))
397404 raise
398405 else :
399- self ._helper .log_debug (pprint_response_info (response ))
406+ self ._helper .log_debug (pprint_response_info (response , is_success = True , show_headers = False ))
400407 return response
401408
402409 @property
0 commit comments