Skip to content

Commit

Permalink
Merge pull request #36 from chkp-royl/master
Browse files Browse the repository at this point in the history
Save API calls log only if debug_file is not empty
  • Loading branch information
chkp-royl authored Nov 18, 2021
2 parents 9b62d4a + b86eae3 commit d861576
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions cpapi/mgmt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
self.port = port
# management server fingerprint
self.fingerprint = fingerprint
Expand Down Expand Up @@ -120,7 +121,6 @@ def __init__(self, api_client_args=None):
# User agent will be use in api call request header
self.user_agent = api_client_args.user_agent


def __enter__(self):
return self

Expand Down Expand Up @@ -324,7 +324,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()
Expand All @@ -345,16 +345,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.debug_file:
# 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":
Expand Down

0 comments on commit d861576

Please sign in to comment.