From b7b250017dcb16c3b16dc67623fae4b0d8a822b0 Mon Sep 17 00:00:00 2001 From: royl Date: Thu, 18 Nov 2021 16:11:16 +0200 Subject: [PATCH 1/2] Add 'save_logs' argument to API client Add flag that indicates whether or not save logs of API calls --- cpapi/mgmt_api.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/cpapi/mgmt_api.py b/cpapi/mgmt_api.py index 213b4b3..f5ab6c6 100644 --- a/cpapi/mgmt_api.py +++ b/cpapi/mgmt_api.py @@ -41,7 +41,8 @@ class APIClientArgs: # single_conn is set to True by default, when work on parallel set to False def __init__(self, port=None, fingerprint=None, sid=None, server="127.0.0.1", http_debug_level=0, api_calls=None, debug_file="", proxy_host=None, proxy_port=8080, - api_version=None, unsafe=False, unsafe_auto_accept=False, context="web_api", single_conn=True, user_agent="python-api-wrapper"): + api_version=None, unsafe=False, unsafe_auto_accept=False, context="web_api", single_conn=True, + user_agent="python-api-wrapper", save_logs=True): self.port = port # management server fingerprint self.fingerprint = fingerprint @@ -71,6 +72,8 @@ def __init__(self, port=None, fingerprint=None, sid=None, server="127.0.0.1", ht self.single_conn = single_conn # User agent will be use in api call request header self.user_agent = user_agent + # Flag whether or not save logs of api calls + self.save_logs = save_logs class APIClient: @@ -119,7 +122,8 @@ def __init__(self, api_client_args=None): self.single_conn = api_client_args.single_conn # User agent will be use in api call request header self.user_agent = api_client_args.user_agent - + # Flag whether or not save logs of api calls + self.save_logs = api_client_args.save_logs def __enter__(self): return self @@ -130,8 +134,9 @@ def __exit__(self, exc_type, exc_value, traceback): if self.sid: self.api_call("logout") self.close_connection() - # save debug data with api calls to disk - self.save_debug_data() + if self.save_logs: + # save debug data with api calls to disk + self.save_debug_data() def get_port(self): """returns the port of the API client (int)""" @@ -324,7 +329,7 @@ def api_call(self, command, payload=None, sid=None, wait_for_task=True, timeout= res = APIResponse("", False, err_message=err_message) else: res = APIResponse("", False, err_message=err) - except (http_client.CannotSendRequest, http_client.BadStatusLine) as e: + except (http_client.CannotSendRequest, http_client.BadStatusLine, ConnectionAbortedError) as e: self.conn = self.create_https_connection() self.conn.request("POST", url, _data, _headers) response = self.conn.getresponse() @@ -345,16 +350,17 @@ def api_call(self, command, payload=None, sid=None, wait_for_task=True, timeout= json_data["password"] = "****" _data = json.dumps(json_data) - # Store the request and the reply (for debug purpose). - _api_log = { - "request": { - "url": url, - "payload": compatible_loads(_data), - "headers": _headers - }, - "response": res.response() - } - self.api_calls.append(_api_log) + if self.save_logs: + # Store the request and the reply (for debug purpose). + _api_log = { + "request": { + "url": url, + "payload": compatible_loads(_data), + "headers": _headers + }, + "response": res.response() + } + self.api_calls.append(_api_log) # If we want to wait for the task to end, wait for it if wait_for_task is True and res.success and command != "show-task": From b86eae36e57e6ae43bc934838747c23ed076bc0d Mon Sep 17 00:00:00 2001 From: royl Date: Thu, 18 Nov 2021 17:23:13 +0200 Subject: [PATCH 2/2] use existing debug_file to decide if to save logs --- cpapi/mgmt_api.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/cpapi/mgmt_api.py b/cpapi/mgmt_api.py index f5ab6c6..4351fc1 100644 --- a/cpapi/mgmt_api.py +++ b/cpapi/mgmt_api.py @@ -42,7 +42,7 @@ class APIClientArgs: def __init__(self, port=None, fingerprint=None, sid=None, server="127.0.0.1", http_debug_level=0, api_calls=None, debug_file="", proxy_host=None, proxy_port=8080, api_version=None, unsafe=False, unsafe_auto_accept=False, context="web_api", single_conn=True, - user_agent="python-api-wrapper", save_logs=True): + user_agent="python-api-wrapper"): self.port = port # management server fingerprint self.fingerprint = fingerprint @@ -72,8 +72,6 @@ def __init__(self, port=None, fingerprint=None, sid=None, server="127.0.0.1", ht self.single_conn = single_conn # User agent will be use in api call request header self.user_agent = user_agent - # Flag whether or not save logs of api calls - self.save_logs = save_logs class APIClient: @@ -122,8 +120,6 @@ def __init__(self, api_client_args=None): self.single_conn = api_client_args.single_conn # User agent will be use in api call request header self.user_agent = api_client_args.user_agent - # Flag whether or not save logs of api calls - self.save_logs = api_client_args.save_logs def __enter__(self): return self @@ -134,9 +130,8 @@ def __exit__(self, exc_type, exc_value, traceback): if self.sid: self.api_call("logout") self.close_connection() - if self.save_logs: - # save debug data with api calls to disk - self.save_debug_data() + # save debug data with api calls to disk + self.save_debug_data() def get_port(self): """returns the port of the API client (int)""" @@ -350,7 +345,7 @@ def api_call(self, command, payload=None, sid=None, wait_for_task=True, timeout= json_data["password"] = "****" _data = json.dumps(json_data) - if self.save_logs: + if self.debug_file: # Store the request and the reply (for debug purpose). _api_log = { "request": {