Skip to content

Commit

Permalink
Merge pull request #33 from chkp-royl/master
Browse files Browse the repository at this point in the history
Handle not JSON reply + fix client BadStatusLine exception
  • Loading branch information
chkp-royl authored Jul 15, 2021
2 parents 737ce9f + aba7d09 commit 2704b5a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions cpapi/api_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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}
Expand Down
24 changes: 14 additions & 10 deletions cpapi/mgmt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="cpapi",
version="1.1.2",
version="1.2.1",
author="API team",
author_email="[email protected]",
description="Check Point Management API SDK",
Expand Down

0 comments on commit 2704b5a

Please sign in to comment.