Skip to content

Instantly share code, notes, and snippets.

@barkady
Created May 26, 2014 06:58
Show Gist options
  • Save barkady/9ca900b1d4bf000d81dd to your computer and use it in GitHub Desktop.
Save barkady/9ca900b1d4bf000d81dd to your computer and use it in GitHub Desktop.
request solution
import requests
import xml.etree.ElementTree as ET
class ALMUrl:
def __init__(self, ip, port, domain, project):
self.__base = u'http://' + ip + u':' + port + u'/qcbin'
self.__auth = self.__base + u'/authentication-point/authenticate'
self.__logout = self.__base + u'/authentication-point/logout'
self.__work = self.__base + u'/rest/domains/' + domain + u'/projects/' + project
def get_auth(self):
return self.__auth
def get_logout(self):
return self.__logout
def __getattr__(self, *args):
result = self.__work
for arg in args:
result += '/' + arg
return result
class ALMSession:
def __init__(self, Logging, login, password):
try:
self.__Logging = Logging
self.__headers = {"Accept":"application/xml",
"Content-Type":"application/xml",
"KeepAlive":"true",
"Cookie": None}#"Authorization":"Basic " + base64.b64encode(login + ':' + password)}
self.__user_pass = (login, password)
except:
self.__Logging.error(u"Exception while creating ALMSession", self.__headers, self.__h)
def FieldsToDict(self, obj, base_type, dict):
almxml = ET.fromstring(obj)
if almxml.attrib["TotalResults"] == 0:
return
one_dict = {}
for fields in almxml.findall(".//*[@Type='" + base_type + "']/Fields"): #that means find all tags with attribute "Type" = 'release', and take their child tags named "Fields"
one_dict.clear()
for field in fields:
one_dict[field.get('Name').decode('utf-8')] = field.find("Value").text
if isinstance(one_dict[field.get('Name')], str):
one_dict[field.get('Name').decode('utf-8')] = one_dict[field.get('Name')].decode('utf-8')
dict.append(one_dict.copy())
return
def create_object(self, entity_type, dictionary):
entity = ET.Element('Entity')
entity.set('Type', entity_type)
fields = ET.SubElement(entity, 'Fields')
for field_name in dictionary:
field = ET.SubElement(fields, 'Field')
field.set('Name', field_name)
value = ET.SubElement(field, 'Value')
value.text = dictionary[field_name]
return ET.tostring(entity, encoding="UTF-8", method="xml")
def Open(self, ALMUrl):
r = requests.get(ALMUrl.get_auth(), auth=self.__user_pass)
if r.status_code is 200:
self.__Logging.info(u"Open ALM session success", {'AUTH URL': ALMUrl.get_auth()}, {'HEADERS': self.__headers})
self.__headers["Cookie"] = r.headers['set-cookie']
return 0
else:
self.__Logging.error(u"Open ALM session", r.status_code, r.reason, {'AUTH URL': ALMUrl.get_auth()}, {'HEADERS': self.__headers})
return int(r.status_code)
def Close(self, ALMUrl):
if self.__headers["Cookie"] is not None:
r = requests.get(ALMUrl.get_logout(), headers=self.__headers, auth=self.__user_pass)
if r.status_code is 200:
self.__Logging.info(u"Close ALM session success", {'LOGOUT URL': ALMUrl.get_logout()}, {'HEADERS': self.__headers})
return 0
else:
self.__Logging.error(u"Close ALM session", r.status_code, r.reason, {'LOGOUT URL': ALMUrl.get_logout()}, {'HEADERS': self.__headers})
return int(r.status_code)
else:
self.__Logging.error("Close ALM session", "1", "httplib2.Http was not initialized")
return 1
def Get(self, ALMUrl, *args):
if self.__headers["Cookie"] is not None:
r = requests.get(ALMUrl.__getattr__(*args), headers=self.__headers, auth=self.__user_pass)
if r.status_code == 200:
self.__Logging.info("[ALMSession] Get success", {"URL": ALMUrl.__getattr__(*args)}, {"HEADERS": self.__headers})
res = []
self.FieldsToDict(r.content, 'release', res)
return 0, res
elif r.status_code == 500:
exceptionxml = ET.fromstring(r.text)
self.__Logging.error("[ALMSession] Get ALM function with errors", exceptionxml[0].text, exceptionxml[1].text, {"PATH": ALMUrl.__getattr__(*args)}, {"HEADERS": self.__headers})
return int(r.status_code), None
else:
self.__Logging.error("[ALMSession] Get ALM function with errors", r.status_code, r.reason, {"PATH": ALMUrl.__getattr__(*args)}, {"HEADERS": self.__headers})
return int(r.status_code), None
else:
self.__Logging.error("[ALMSession] Get ALM function with errors", "1", "httplib2.Http not initialized")
return 1, None
def Put(self, ALMUrl, data, *args):
if self.__headers["Cookie"] is not None:
r = requests.put(ALMUrl.__getattr__(*args),
headers=self.__headers,
data=data,
auth=self.__user_pass)
if r.status_code == 200:
self.__Logging.info("[ALMSession] Put success", {"URL": ALMUrl.__getattr__(*args)})
return 0
elif r.status_code == 500:
exceptionxml = ET.fromstring(r.content)
self.__Logging.error("[ALMSession] Put ALM function with errors", exceptionxml[0].text, exceptionxml[1].text, {"PATH": ALMUrl.__getattr__(*args)}, {"DATA": data}, {"HEADERS": self.__headers})
return int(r.status_code)
else:
self.__Logging.error("[ALMSession] Put ALM function with errors", r.status_code, r.reason, {"PATH": ALMUrl.__getattr__(*args)}, {"DATA": data}, {"HEADERS": self.__headers})
return int(r.status_status)
else:
self.__Logging.error("[ALMSession] Put ALM function with errors", "1", "httplib2.Http not initialized")
return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment