From aba7d09c00e00f27f11c2d0c347f3b24e291b247 Mon Sep 17 00:00:00 2001 From: royl Date: Thu, 15 Jul 2021 14:34:08 +0300 Subject: [PATCH] Handle not JSON reply + fix client BadStatusLine exception --- cpapi/api_response.py | 6 ++++-- cpapi/mgmt_api.py | 24 ++++++++++++++---------- setup.py | 2 +- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/cpapi/api_response.py b/cpapi/api_response.py index 37ee3f7..21e7386 100644 --- a/cpapi/api_response.py +++ b/cpapi/api_response.py @@ -28,7 +28,7 @@ def extract_error_and_warning_messages(data): # value can be either string or list with dictionaries if isinstance(val, list): for error_or_warning in val: - error_message.append("\n- " + "message: " + error_or_warning["message"] + "\n") + error_message.append("\n- " + "message: " + error_or_warning.get("message", "") + "\n") else: error_message.append(str(val) + "\n") @@ -59,7 +59,9 @@ def __init__(self, json_response, success, status_code=None, err_message=""): else: data_dict = compatible_loads(json_response) except ValueError: - raise APIException("APIResponse received a response which is not a valid JSON.", json_response) + self.data = {"errors": [{"message": str(json_response)}]} + self.error_message = "APIResponse received a response which is not a valid JSON." + self.res_obj = {"status_code": self.status_code, "data": self.data} else: self.data = data_dict self.res_obj = {"status_code": self.status_code, "data": self.data} diff --git a/cpapi/mgmt_api.py b/cpapi/mgmt_api.py index 1e8e3ce..8caeb54 100644 --- a/cpapi/mgmt_api.py +++ b/cpapi/mgmt_api.py @@ -293,7 +293,8 @@ def api_call(self, command, payload=None, sid=None, wait_for_task=True, timeout= "User-Agent": "python-api-wrapper", "Accept": "*/*", "Content-Type": "application/json", - "Content-Length": len(_data) + "Content-Length": len(_data), + "Connection": "Keep-Alive" } # In all API calls (except for 'login') a header containing the Check Point session-id is required. @@ -318,6 +319,11 @@ 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: + self.conn = self.create_https_connection() + self.conn.request("POST", url, _data, _headers) + response = self.conn.getresponse() + res = APIResponse.from_http_response(response) except Exception as err: res = APIResponse("", False, err_message=err) finally: @@ -461,7 +467,7 @@ def get_server_fingerprint(self): Initiates an HTTPS connection to the server if need and extracts the SHA1 fingerprint from the server's certificate. :return: string with SHA1 fingerprint (all uppercase letters) """ - conn = self.get_https_connection(set_fingerprint=False, set_debug_level=False) + conn = self.get_https_connection() fingerprint_hash = conn.get_fingerprint_hash() if not self.single_conn: conn.close() @@ -709,7 +715,7 @@ def read_fingerprint_from_file(server, filename="fingerprints.txt"): return json_dict[server] return "" - def create_https_connection(self, set_fingerprint, set_debug_level): + def create_https_connection(self): context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE @@ -721,21 +727,19 @@ def create_https_connection(self, set_fingerprint, set_debug_level): conn = HTTPSConnection(self.server, self.get_port(), context=context) # Set fingerprint - if set_fingerprint: - conn.fingerprint = self.fingerprint + conn.fingerprint = self.fingerprint # Set debug level - if set_debug_level: - conn.set_debuglevel(self.http_debug_level) + conn.set_debuglevel(self.http_debug_level) conn.connect() return conn - def get_https_connection(self, set_fingerprint=True, set_debug_level=True): + def get_https_connection(self): if self.single_conn: if self.conn is None: - self.conn = self.create_https_connection(set_fingerprint, set_debug_level) + self.conn = self.create_https_connection() return self.conn - return self.create_https_connection(set_fingerprint, set_debug_level) + return self.create_https_connection() def close_connection(self): if self.conn: diff --git a/setup.py b/setup.py index 9f6ca91..431acf2 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name="cpapi", - version="1.1.2", + version="1.2.1", author="API team", author_email="api_team@checkpoint.com", description="Check Point Management API SDK",