forked from bitbar/test-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.py
More file actions
58 lines (47 loc) · 2.33 KB
/
upload.py
File metadata and controls
58 lines (47 loc) · 2.33 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
#
# Example how to upload application to Bitbar Cloud before Appium test
#
import argparse
import base64
import os
import requests
import sys
class UploadApp():
def __init__(self):
self.myfile = os.environ.get('BITBAR_APP_PATH') or \
'../../../../../apps/android/testdroid-sample-app.apk'
self.upload_url = os.environ.get('BITBAR_UPLOAD_URL') or \
'https://cloud.bitbar.com/api/v2/me/files'
# Provide mandatory API key with this env var or with -k/--apikey flag
self.api_key = os.environ.get("BITBAR_APIKEY") or ""
def parse_args(self):
parser = argparse.ArgumentParser(description='Upload a mobile app to Bitbar Cloud and get a handle to it')
parser.add_argument('-k', '--apikey', type=str, required=False, help="User's apiKey to identify to cloud, or set environment variable BITBAR_APIKEY")
parser.add_argument('-a', '--app_path', type=str, required=False, help="Path to app to upload or set environment variable BITBAR_APP_PATH. Current value is: '{}'".format(self.myfile))
parser.add_argument('-u', '--url', type=str, required=False, help="Bitbar Cloud url to upload app or set environment variable BITBAR_UPLOAD_URL. Current value is: '{}'".format(self.upload_url))
args = parser.parse_args()
if args.app_path:
self.myfile = args.app_path
if args.url:
self.upload_url = args.url
if args.apikey:
self.api_key = args.apikey
# Sanity checks
if len(self.api_key) == 0:
print("ERROR: API key is missing. Provide BITBAR_APIKEY env var or -k/--apikey <APIKEY> flag.")
sys.exit(1)
def build_headers(self):
hdrs = {'Authorization': 'Basic %s' % base64.b64encode(self.api_key + ":"),
'Accept': 'application/json'}
return hdrs
def upload_app(self):
self.parse_args()
files = {'file': (os.path.basename(self.myfile), open(self.myfile, 'rb'), 'application/octet-stream')}
r = requests.post(self.upload_url, files=files, headers=self.build_headers())
try:
print("File id to use in bitbar capabilities in your test: {}".format(r.json()['id']))
except ValueError:
print("Upload response: \n{}".format(r))
if __name__ == '__main__':
up = UploadApp()
up.upload_app()