Skip to content
Snippets Groups Projects
Commit 8c16acfe authored by Robert Habermann's avatar Robert Habermann
Browse files

refactor _ticket_<function>_json methods

parent 4b4c94ab
No related branches found
No related tags found
No related merge requests found
......@@ -734,19 +734,7 @@ class Client(object):
Uses HTTP Method: POST
"""
headers = {"Content-Type": "application/json"}
json_payload = json.dumps(payload)
try:
self.response = requests.post(url,
proxies=proxies,
data=json_payload,
headers=headers,
verify=self.https_verify)
except requests.exceptions.RequestException as err:
logger.error("Requests Exception: {}".format(err))
raise requests.exceptions.RequestException("Requests Exception: {}".format(err))
self.response = self._ticket_request("POST", url, payload, self.https_verify)
# TODO 2016-04-18 (RH): This should be more thorough/verbose..!
try:
......@@ -1027,22 +1015,7 @@ class Client(object):
Uses HTTP Method: GET
"""
headers = {"Content-Type": "application/json"}
json_payload = json.dumps(payload)
try:
self.response = requests.get(url,
proxies=proxies,
data=json_payload,
headers=headers,
verify=self.https_verify)
# critical error: HTTP request resulted in an error!
except Exception as err:
logger.error("Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!"
"Error with http communication: {0}".format(err))
# raise OTRSHTTPError("get http")
raise OTRSHTTPError("Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!"
"Error with http communication: {0}".format(err))
self.response = self._ticket_request("GET", url, payload, self.https_verify)
try:
self.ticket_json = self.response.json()
......@@ -1213,22 +1186,8 @@ class Client(object):
Uses HTTP Method: GET
"""
headers = {"Content-Type": "application/json"}
json_payload = json.dumps(payload)
try:
self.response = requests.get(url,
proxies=proxies,
data=json_payload,
headers=headers,
verify=self.https_verify)
# critical error: HTTP request resulted in an error!
except Exception as err:
logger.error("Failed to access OTRS HTTP. Check Hostname, Proxy, SSL Certificate!"
"!Error with http communication: {0}".format(err))
# raise OTRSHTTPError("get http")
raise OTRSHTTPError("Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!"
"Error with http communication: {0}".format(err))
self.response = self._ticket_request("GET", url, payload, self.https_verify)
try:
self.ticket_json = self.response.json()
......@@ -1401,22 +1360,8 @@ class Client(object):
Uses HTTP Method: PATCH
"""
headers = {"Content-Type": "application/json"}
json_payload = json.dumps(payload)
try:
self.response = requests.patch(url,
proxies=proxies,
data=json_payload,
headers=headers,
verify=self.https_verify)
# critical error: HTTP request resulted in an error!
except Exception as err:
logger.error("Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!"
"Error with http communication: {0}".format(err))
# raise OTRSHTTPError("get http")
raise OTRSHTTPError("Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!"
"Error with http communication: {0}".format(err))
self.response = self._ticket_request("PATCH", url, payload, self.https_verify)
try:
......@@ -1465,4 +1410,49 @@ class Client(object):
return True
@staticmethod
def _ticket_request(http_method=None, url=None, payload=None, https_verify=True):
""" _ticket_request
Args:
http_method=
url (str):
payload (dict):
Raises:
OTRSHTTPError
OTRSAPIError
Returns:
response
.. note::
Uses one of HTTP Method: GET, PATCH, POST
"""
headers = {"Content-Type": "application/json"}
json_payload = json.dumps(payload)
try:
if http_method.upper() in ["GET", "PATCH", "POST"]:
response = requests.request(http_method.upper(),
url,
proxies=proxies,
data=json_payload,
headers=headers,
verify=https_verify)
else:
raise NotImplementedError("Only valid HTTP Methods: GET, PATCH, POST")
# critical error: HTTP request resulted in an error!
except Exception as err:
logger.error("Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!"
"Error with http communication: {0}".format(err))
# raise OTRSHTTPError("get http")
raise OTRSHTTPError("Failed to access OTRS. Check Hostname, Proxy, SSL Certificate!"
"Error with http communication: {0}".format(err))
return response
# EOF
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment