Last active
October 26, 2016 18:21
-
-
Save niuniulla/59682ade09be14c8c3cfdd25f5535166 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
This program is used to attach a file to a defect in HPQC. | |
''' | |
import requests | |
from requests import Request, Session | |
from requests.auth import HTTPBasicAuth | |
import json,io | |
import collections | |
acc='application/xml' | |
#login to quality center | |
def connect_hpqc(s,user,password): | |
url="~/qcbin/authentication-point/authenticate" | |
resp=s.get(url,auth=HTTPBasicAuth(user,password),verify=False) | |
print('authentication Response\n',resp) | |
#create a session | |
def session_hpqc(s): | |
url="~/qcbin/rest/site-session" | |
headers={'Accept':acc} | |
resp=s.post(url,verify=False) | |
print('Session Response\n',resp) | |
#close the session | |
def close_session_hpqc(s): | |
url="~/qcbin/rest/site-session" | |
headers={'Accept':acc} | |
resp=s.delete(url,headers=headers,verify=False) | |
print('Close Session Response\n',resp) | |
#logout | |
def disconnect_hpqc(s): | |
url="~/qcbin/authentication-point/logout" | |
cookies=s.cookies | |
resp=s.get(url,verify=False) | |
print('Disconnection Response\n',resp) | |
def show_headers(req): | |
print('{}\n{}\n{}\n\n{}' .format( | |
'', | |
req.method + '' + req.url, | |
'\n'.join('{}: {}' .format(k,v) for k,v in req.headers.items()), | |
req.body.decode(), | |
'', | |
)) | |
#attach a file to the defect of ID=XXXXXXX | |
def attach_files(s, rID, filedir, filename, show_content=1): | |
url=urlb+"/qcbin/rest/domains/DEFAULT/projects/QDataQuality/defects/" | |
urlup=url+rID+"/attachments" | |
print("url to upload: "+urlup) | |
print("File to upload: "+filedir+filename) | |
print("File exists: "+str(path.isfile(filedir+filename))) | |
files=collections.OrderedDict(( | |
("filename", ("",filename)), | |
("override-existing-attachment", ("","n")), | |
("file", (filename,io.open(filedir+filename,"rb"))), | |
)) | |
try: | |
req=Request( | |
method='POST', | |
url=urlup, | |
files=files | |
) | |
prereq=s.prepare_request(req) | |
if show_content==1: | |
show_headers(prereq) | |
resp=s.send( | |
prereq, | |
stream=False, | |
timeout=None, | |
verify=False, | |
cert=None, | |
proxies=None, | |
allow_redirects=False | |
) | |
print(resp) | |
except: | |
print(rID+'Error') | |
#delete a file from the defect of ID=XXXXXXX | |
def delete_attachment(s, rID, filename): | |
url=urlb+"/qcbin/rest/domains/DEFAULT/projects/QDataQuality/defects/" | |
urlup=url+rID+"/attachments/"+filename | |
print(urlup) | |
headers={'Accept':acc} | |
try: | |
resp=s.delete(urlup,headers=headers,verify=False) | |
print('Close Session Response\n',resp) | |
except: | |
print(rID+'Error') | |
if __name__ == "__main__": | |
s=Session() | |
connect_hpqc(s) | |
session_hpqc(s) | |
attach_files("13146", "P:/Desktop/", "Hp13272_20160729.xlsx") | |
close_session_hpqc(s) | |
disconnect_hpqc(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment