Skip to content

Commit 2704b5a

Browse files
authored
Merge pull request #33 from chkp-royl/master
Handle not JSON reply + fix client BadStatusLine exception
2 parents 737ce9f + aba7d09 commit 2704b5a

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

cpapi/api_response.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def extract_error_and_warning_messages(data):
2828
# value can be either string or list with dictionaries
2929
if isinstance(val, list):
3030
for error_or_warning in val:
31-
error_message.append("\n- " + "message: " + error_or_warning["message"] + "\n")
31+
error_message.append("\n- " + "message: " + error_or_warning.get("message", "") + "\n")
3232
else:
3333
error_message.append(str(val) + "\n")
3434

@@ -59,7 +59,9 @@ def __init__(self, json_response, success, status_code=None, err_message=""):
5959
else:
6060
data_dict = compatible_loads(json_response)
6161
except ValueError:
62-
raise APIException("APIResponse received a response which is not a valid JSON.", json_response)
62+
self.data = {"errors": [{"message": str(json_response)}]}
63+
self.error_message = "APIResponse received a response which is not a valid JSON."
64+
self.res_obj = {"status_code": self.status_code, "data": self.data}
6365
else:
6466
self.data = data_dict
6567
self.res_obj = {"status_code": self.status_code, "data": self.data}

cpapi/mgmt_api.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ def api_call(self, command, payload=None, sid=None, wait_for_task=True, timeout=
293293
"User-Agent": "python-api-wrapper",
294294
"Accept": "*/*",
295295
"Content-Type": "application/json",
296-
"Content-Length": len(_data)
296+
"Content-Length": len(_data),
297+
"Connection": "Keep-Alive"
297298
}
298299

299300
# 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=
318319
res = APIResponse("", False, err_message=err_message)
319320
else:
320321
res = APIResponse("", False, err_message=err)
322+
except (http_client.CannotSendRequest, http_client.BadStatusLine) as e:
323+
self.conn = self.create_https_connection()
324+
self.conn.request("POST", url, _data, _headers)
325+
response = self.conn.getresponse()
326+
res = APIResponse.from_http_response(response)
321327
except Exception as err:
322328
res = APIResponse("", False, err_message=err)
323329
finally:
@@ -461,7 +467,7 @@ def get_server_fingerprint(self):
461467
Initiates an HTTPS connection to the server if need and extracts the SHA1 fingerprint from the server's certificate.
462468
:return: string with SHA1 fingerprint (all uppercase letters)
463469
"""
464-
conn = self.get_https_connection(set_fingerprint=False, set_debug_level=False)
470+
conn = self.get_https_connection()
465471
fingerprint_hash = conn.get_fingerprint_hash()
466472
if not self.single_conn:
467473
conn.close()
@@ -709,7 +715,7 @@ def read_fingerprint_from_file(server, filename="fingerprints.txt"):
709715
return json_dict[server]
710716
return ""
711717

712-
def create_https_connection(self, set_fingerprint, set_debug_level):
718+
def create_https_connection(self):
713719
context = ssl.create_default_context()
714720
context.check_hostname = False
715721
context.verify_mode = ssl.CERT_NONE
@@ -721,21 +727,19 @@ def create_https_connection(self, set_fingerprint, set_debug_level):
721727
conn = HTTPSConnection(self.server, self.get_port(), context=context)
722728

723729
# Set fingerprint
724-
if set_fingerprint:
725-
conn.fingerprint = self.fingerprint
730+
conn.fingerprint = self.fingerprint
726731

727732
# Set debug level
728-
if set_debug_level:
729-
conn.set_debuglevel(self.http_debug_level)
733+
conn.set_debuglevel(self.http_debug_level)
730734
conn.connect()
731735
return conn
732736

733-
def get_https_connection(self, set_fingerprint=True, set_debug_level=True):
737+
def get_https_connection(self):
734738
if self.single_conn:
735739
if self.conn is None:
736-
self.conn = self.create_https_connection(set_fingerprint, set_debug_level)
740+
self.conn = self.create_https_connection()
737741
return self.conn
738-
return self.create_https_connection(set_fingerprint, set_debug_level)
742+
return self.create_https_connection()
739743

740744
def close_connection(self):
741745
if self.conn:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="cpapi",
6-
version="1.1.2",
6+
version="1.2.1",
77
author="API team",
88
author_email="[email protected]",
99
description="Check Point Management API SDK",

0 commit comments

Comments
 (0)