-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestingbotclient.py
224 lines (182 loc) · 7.99 KB
/
testingbotclient.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
import base64
import sys
import json
import os
import requests
from requests.auth import HTTPBasicAuth
import hashlib
class TestingBotException(Exception):
def __init__(self, *args, **kwargs):
super(TestingBotException, self).__init__(*args)
self.response = kwargs.get('response')
class TestingBotClient(object):
def __init__(self, testingbotKey=None, testingbotSecret=None):
self.testingbotKey = testingbotKey
self.testingbotSecret = testingbotSecret
if self.testingbotKey is None:
self.testingbotKey = os.environ.get('TESTINGBOT_KEY', None)
self.testingbotSecret = os.environ.get('TESTINGBOT_SECRET', None)
if self.testingbotKey is None:
self.testingbotKey = os.environ.get('TB_KEY', None)
self.testingbotSecret = os.environ.get('TB_SECRET', None)
if self.testingbotKey is None:
path = os.path.join(os.path.expanduser('~'), '.testingbot')
if os.path.exists(path):
f = open(path, 'r')
data = f.read()
self.testingbotKey, self.testingbotSecret = data.split(':')
f.close()
self.information = Information(self)
self.tests = Tests(self)
self.user = User(self)
self.storage = Storage(self)
self.tunnel = Tunnel(self)
self.build = Build(self)
self.api_url = 'https://api.testingbot.com/v1/'
def post(self, url, data):
response = requests.post(self.api_url + url, data=data, auth=(self.testingbotKey, self.testingbotSecret))
if response.status_code not in [200, 201]:
raise TestingBotException('{}: {}.\nTestingBot API Error'.format(
response.status_code, response.text), response=response)
return response.json()
def delete(self, url):
response = requests.delete(self.api_url + url, auth=(self.testingbotKey, self.testingbotSecret))
if response.status_code not in [200, 201]:
raise TestingBotException('{}: {}.\nTestingBot API Error'.format(
response.status_code, response.text), response=response)
return response.json()
def put(self, url, data):
response = requests.put(self.api_url + url, data=data, auth=(self.testingbotKey, self.testingbotSecret))
if response.status_code not in [200, 201]:
raise TestingBotException('{}: {}.\nTestingBot API Error'.format(
response.status_code, response.text), response=response)
return response.json()
def get(self, url):
response = requests.get(self.api_url + url, auth=(self.testingbotKey, self.testingbotSecret))
if response.status_code not in [200, 201]:
raise TestingBotException('{}: {}.\nTestingBot API Error'.format(
response.status_code, response.text), response=response)
return response.json()
def get_share_link(self, identifier):
return hashlib.md5(("%s:%s:%s" % (self.testingbotKey, self.testingbotSecret, identifier)).encode('utf-8')).hexdigest()
class Tests(object):
def __init__(self, client):
self.client = client
def get_test_ids(self):
"""List all tests sessionId's belonging to the user."""
url = '/tests'
tests = self.client.get(method, url)
test_ids = [attr['session_id'] for attr in tests['data']]
return test_ids
def get_tests(self, offset = 0, limit = 10):
"""List all tests belonging to the user."""
url = '/tests?offset=' + str(offset) + '&count=' + str(limit)
tests = self.client.get(url)
return tests["data"]
def get_test(self, sessionId):
"""Get meta-data for a specific test"""
return self.client.get('/tests/' + sessionId)
def update_test(self, sessionId, name=None, passed=None, status_message=None, build=None):
"""Update attributes for the specified test."""
params = {}
if status_message is not None:
params['test[status_message]'] = status_message
if name is not None:
params['test[name]'] = name
if passed is not None:
params['test[success]'] = ('1' if passed else '0')
if build is not None:
params['build'] = build
url = '/tests/%s' % sessionId
response = self.client.put(url, params)
return response['success']
def delete_test(self, sessionId):
"""Deletes a test."""
url = '/tests/%s' % sessionId
response = self.client.delete(url)
return response['success']
def stop_test(self, sessionId):
"""Stops a test."""
url = '/tests/%s/stop' % sessionId
response = self.client.put(url)
return response['success']
class Storage(object):
def __init__(self, client):
self.client = client
def upload_local_file(self, filepath):
"""Uploads a local file to TestingBot Storage."""
with open(filepath, 'rb') as f:
return requests.post(
self.client.api_url + "/storage",
files={'file': f},
auth=(self.client.testingbotKey, self.client.testingbotSecret)
).json()
def upload_remote_file(self, remoteUrl):
return self.client.post("/storage", { 'url': remoteUrl })
def get_stored_file(self, app_url):
"""Retrieves meta-data for a file previously uploaded to TestingBot Storage."""
return self.client.get("/storage/" + app_url.replace("tb://", ""))
def remove_file(self, app_url):
"""Removes a file previously uploaded to TestingBot Storage."""
return self.client.delete("/storage/" + app_url.replace("tb://", ""))
def get_stored_files(self, offset = 0, limit = 10):
"""Retrieves all files previously uploaded to TestingBot Storage."""
return self.client.get("/storage/?count=" + str(limit) + "&offset=" + str(offset))
class Information(object):
def __init__(self, client):
self.client = client
def get_browsers(self):
"""Get details of all browsers currently supported on TestingBot"""
url = '/browsers'
browsers = self.client.get(url)
return browsers
def get_devices(self):
"""Get details of all devices currently on TestingBot"""
url = '/devices'
devices = self.client.get(url)
return devices
def get_available_devices(self):
"""Get details of all devices currently available on TestingBot"""
url = '/devices/available'
devices = self.client.get(url)
return devices
def get_device(self, deviceId):
"""Get details of a specific device on TestingBot"""
url = '/devices/' + deviceId
device = self.client.get(url)
return device
class Tunnel(object):
def __init__(self, client):
self.client = client
def get_tunnels(self):
"""Get TestingBot Tunnels currently running"""
return self.client.get('/tunnel/list')
def delete_tunnel(self, tunnelId):
"""Delete a specific TestingBot Tunnel"""
return self.client.delete('/tunnel/' + tunnelId)
class Build(object):
def __init__(self, client):
self.client = client
def get_builds(self, offset = 0, limit = 10):
"""Get all builds"""
return self.client.get('/builds?offset=' + str(offset) + '&count=' + str(limit))
def get_tests_for_build(self, buildId):
"""Get tests for a specific build"""
return self.client.get('/builds/' + buildId)
def delete_build(self, buildId):
"""Delete a specific build"""
return self.client.delete('/builds/' + buildId)
class User(object):
def __init__(self, client):
self.client = client
def get_user_information(self):
"""Access current user information"""
url = '/user'
info = self.client.get(url)
return info
def update_user_information(self, newUser):
"""Update current user information"""
url = '/user'
info = self.client.put(url, newUser)
return info