88from dku_constants import DKUConstants
99
1010
11- logger = SafeLogger ("api-connect plugin" , forbiden_keys = [ "token" , "password" ] )
11+ logger = SafeLogger ("api-connect plugin" , forbidden_keys = DKUConstants . FORBIDDEN_KEYS )
1212
1313
1414class RestAPIClientError (ValueError ):
@@ -17,7 +17,7 @@ class RestAPIClientError(ValueError):
1717
1818class RestAPIClient (object ):
1919
20- def __init__ (self , credential , endpoint , custom_key_values = {}):
20+ def __init__ (self , credential , endpoint , custom_key_values = {}, session = None , behaviour_when_error = None ):
2121 logger .info ("Initialising RestAPIClient, credential={}, endpoint={}" .format (logger .filter_secrets (credential ), endpoint ))
2222
2323 # presets_variables contains all variables available in templates using the {{variable_name}} notation
@@ -57,6 +57,7 @@ def __init__(self, credential, endpoint, custom_key_values={}):
5757 self .timeout = endpoint .get ("timeout" , - 1 )
5858 if self .timeout > 0 :
5959 self .requests_kwargs .update ({"timeout" : self .timeout })
60+ self .behaviour_when_error = behaviour_when_error or "add-error-column"
6061
6162 self .requests_kwargs .update ({"params" : self .params })
6263 self .pagination = Pagination ()
@@ -90,7 +91,10 @@ def __init__(self, credential, endpoint, custom_key_values={}):
9091 key_value_body = endpoint .get ("key_value_body" , {})
9192 self .requests_kwargs .update ({"json" : get_dku_key_values (key_value_body )})
9293 self .metadata = {}
94+ if self .behaviour_when_error == "keep-error-column" :
95+ self .metadata = {DKUConstants .REPONSE_ERROR_KEY : None }
9396 self .call_number = 0
97+ self .session = session or requests .Session ()
9498
9599 def set_login (self , credential ):
96100 login_type = credential .get ("login_type" , "no_auth" )
@@ -131,26 +135,34 @@ def request(self, method, url, can_raise_exeption=True, **kwargs):
131135 raise RestAPIClientError ("The api-connect plugin is stuck in a loop. Please check the pagination parameters." )
132136 request_start_time = time .time ()
133137 self .time_last_request = request_start_time
138+ error_message = None
139+ status_code = None
140+ response_headers = None
134141 try :
135142 response = self .request_with_redirect_retry (method , url , ** kwargs )
136- request_finish_time = time .time ()
143+ status_code = response .status_code
144+ response_headers = response .headers
137145 except Exception as err :
138146 self .pagination .is_last_batch_empty = True
139147 error_message = "Error: {}" .format (err )
140148 if can_raise_exeption :
141149 raise RestAPIClientError (error_message )
142- else :
143- return { "error" : error_message }
150+
151+ request_finish_time = time . time ()
144152 self .set_metadata ("request_duration" , request_finish_time - request_start_time )
145- self .set_metadata ("status_code" , response .status_code )
146- self .set_metadata ("response_headers" , "{}" .format (response .headers ))
153+ self .set_metadata ("status_code" , status_code )
154+ self .set_metadata ("response_headers" , "{}" .format (response_headers ))
155+
156+ if error_message :
157+ return {} if self .behaviour_when_error == "ignore" else {DKUConstants .REPONSE_ERROR_KEY : error_message }
158+
147159 if response .status_code >= 400 :
148160 error_message = "Error {}: {}" .format (response .status_code , response .content )
149161 self .pagination .is_last_batch_empty = True
150162 if can_raise_exeption :
151163 raise RestAPIClientError (error_message )
152164 else :
153- return {"error" : error_message }
165+ return {} if self . behaviour_when_error == "ignore" else { DKUConstants . REPONSE_ERROR_KEY : error_message }
154166 if response .status_code in [204 ]:
155167 self .pagination .update_next_page ({}, response .links )
156168 return self .empty_json_response ()
@@ -163,20 +175,20 @@ def request(self, method, url, can_raise_exeption=True, **kwargs):
163175 logger .error ("response.content={}" .format (response .content ))
164176 if can_raise_exeption :
165177 raise RestAPIClientError ("The API did not return JSON as expected. {}" .format (error_message ))
166- return {"error" : error_message }
178+ return {} if self . behaviour_when_error == "ignore" else { DKUConstants . REPONSE_ERROR_KEY : error_message }
167179
168180 self .pagination .update_next_page (json_response , response .links )
169181 return json_response
170182
171183 def request_with_redirect_retry (self , method , url , ** kwargs ):
172184 # In case of redirection to another domain, the authorization header is not kept
173185 # If redirect_auth_header is true, another attempt is made with initial headers to the redirected url
174- response = requests .request (method , url , ** kwargs )
186+ response = self . session .request (method , url , ** kwargs )
175187 if self .redirect_auth_header and not response .url .startswith (url ):
176188 redirection_kwargs = copy .deepcopy (kwargs )
177189 redirection_kwargs .pop ("params" , None ) # params are contained in the redirected url
178190 logger .warning ("Redirection ! Accessing endpoint {} with initial authorization headers" .format (response .url ))
179- response = requests .request (method , response .url , ** redirection_kwargs )
191+ response = self . session .request (method , response .url , ** redirection_kwargs )
180192 return response
181193
182194 def paginated_api_call (self , can_raise_exeption = True ):
0 commit comments