@@ -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 ):
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
@@ -91,7 +91,7 @@ def __init__(self, credential, endpoint, custom_key_values={}):
9191 self .requests_kwargs .update ({"json" : get_dku_key_values (key_value_body )})
9292 self .metadata = {}
9393 self .call_number = 0
94- self .session = requests .Session ()
94+ self .session = session or requests .Session ()
9595
9696 def set_login (self , credential ):
9797 login_type = credential .get ("login_type" , "no_auth" )
@@ -132,19 +132,23 @@ def request(self, method, url, can_raise_exeption=True, **kwargs):
132132 raise RestAPIClientError ("The api-connect plugin is stuck in a loop. Please check the pagination parameters." )
133133 request_start_time = time .time ()
134134 self .time_last_request = request_start_time
135+ error_message = None
135136 try :
136137 response = self .request_with_redirect_retry (method , url , ** kwargs )
137- request_finish_time = time .time ()
138138 except Exception as err :
139139 self .pagination .is_last_batch_empty = True
140140 error_message = "Error: {}" .format (err )
141141 if can_raise_exeption :
142142 raise RestAPIClientError (error_message )
143- else :
144- return { "error" : error_message }
143+
144+ request_finish_time = time . time ()
145145 self .set_metadata ("request_duration" , request_finish_time - request_start_time )
146146 self .set_metadata ("status_code" , response .status_code )
147147 self .set_metadata ("response_headers" , "{}" .format (response .headers ))
148+
149+ if error_message :
150+ return {"error" : error_message }
151+
148152 if response .status_code >= 400 :
149153 error_message = "Error {}: {}" .format (response .status_code , response .content )
150154 self .pagination .is_last_batch_empty = True
0 commit comments