-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcentinel_client.py
More file actions
71 lines (55 loc) · 2.16 KB
/
centinel_client.py
File metadata and controls
71 lines (55 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from urllib import urlencode
from urllib2 import HTTPError, URLError, urlopen
from xml.etree import ElementTree as ET
class CentinelClient(object):
""" Object responsible for communicating with Centinel """
data_dict = {}
response = {}
def add(self, name, value):
""" add item to data dictionary """
self.data_dict[name] = value
def add_many(self, data_dict):
""" Update dictionary with a dictionary """
self.data_dict.update(data_dict)
def print_request_dict(self):
""" Print current data in the dictionary """
for name, value in self.data_dict:
print "{0} = {1}".format(name, value)
def send_request(self, url, timeout=30):
""" Send the request to centinel """
xml = self.generate_xml()
data = urlencode({'cmpi_msg': xml})
try:
response = urlopen(url, data, timeout)
except URLError, e:
self.response['ErrDesc'] = "URLError (couldn't contact): {0}".format(e)
return False
except HTTPError, e:
self.response['ErrDesc'] = "HTTPError (Server Error): {0}".format(e)
return False
return self.parse_xml(response.read())
def check_valid(self):
""" Check there were no errors """
if self.response['ErrorNo'] == "0":
return True
else:
return False
def generate_xml(self):
""" generate the xml from the data dictionary to send to centinel """
root = ET.Element("CardinalMPI")
for name, value in self.data_dict.iteritems():
ET.SubElement(root, name).text = str(value)
return ET.tostring(root)
def parse_xml(self, xml_string):
""" parse the response from centinel """
xml = ET.XML(xml_string)
returned_data = {}
for child in xml.getchildren():
returned_data[child.tag] = child.text
self.response = returned_data
return self.check_valid()
def generate_error(self, error):
""" generate an error xml """
root = ET.Element("CardinalMPI")
ET.SubElement(root, "ErrorDesc").text = error
return ET.tostring(root)