Skip to content

Instantly share code, notes, and snippets.

@stask
Created April 26, 2016 20:50
Show Gist options
  • Save stask/51a41c9ed1d00af6decb6715d8e1b8dd to your computer and use it in GitHub Desktop.
Save stask/51a41c9ed1d00af6decb6715d8e1b8dd to your computer and use it in GitHub Desktop.
# You will need to install poster via pip
# $> sudo pip install poster
import urllib2, md5, time
from poster.encode import multipart_encode, MultipartParam
from poster.streaminghttp import register_openers
URL = "https://stage.practitest.com/api/automated_tests/upload_test_result.json"
#
# Specify your API_KEY and API_SECRET_KEY below
API_KEY = ""
API_SECRET_KEY = ""
register_openers()
def auth():
ts = str(int(time.time()))
signature = md5.new(API_KEY + API_SECRET_KEY + ts).hexdigest()
return "custom api_key=" + API_KEY + ", signature=" + signature + ", ts=" + ts
def post():
items = []
# regular params
items.append(MultipartParam("project_id", "4539"))
items.append(MultipartParam("instance_display_id", "5:1"))
items.append(MultipartParam("exit_code", "0"))
items.append(MultipartParam("result", "text output\nwill be shown in 'Execution output' field\n"))
items.append(MultipartParam("instance_custom_fields[][name]", "Operating System"))
items.append(MultipartParam("instance_custom_fields[][value]", "Win"))
items.append(MultipartParam("instance_custom_fields[][name]", "Browser"))
items.append(MultipartParam("instance_custom_fields[][value]", "Chrome"))
# file params
items.append(MultipartParam.from_file("result_files[file0]", "/tmp/file0.txt"))
data, headers = multipart_encode(items)
headers["Authorization"] = auth()
req = urllib2.Request(URL, data, headers)
try:
res = urllib2.urlopen(req)
print res.getcode()
except urllib2.URLError, e:
print e.code
print "Body: ", e.read()
post()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment