forked from bitbar/test-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_finder.py
More file actions
129 lines (113 loc) · 4.79 KB
/
device_finder.py
File metadata and controls
129 lines (113 loc) · 4.79 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
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
# -*- coding: utf-8 -*-
import os
import requests
import sys
import base64
from random import shuffle
class DeviceFinder:
""" Constructor
"""
def __init__(self, url="https://cloud.bitbar.com", api_key="", download_buffer_size=65536):
self.cloud_url = os.environ.get('BITBAR_URL') or url
self.api_key = os.environ.get('BITBAR_APIKEY') or api_key
self.download_buffer_size = download_buffer_size
self.url_query = None
# to use devices from particular device group set the device
# group id as environment variable. Otherwise free devices are
# looked for
self.device_group = os.environ.get('BITBAR_DEVICE_GROUP') or ""
""" Append dictionary items to header
"""
def _build_headers(self, headers=None):
hdrs = {'Authorization' : 'Basic %s' % base64.b64encode((self.api_key+":").encode(encoding='utf_8')).decode(), 'Accept' : 'application/json' }
if headers is not None:
hdrs.update(headers)
return hdrs
""" GET from API resource
"""
def get(self, path=None, get_headers=None):
self.url_query = "%s/api/v2/%s" % (self.cloud_url, path)
query_headers = self._build_headers(get_headers)
res = requests.get(self.url_query, headers=query_headers)
if res.ok:
return res.json()
else:
print("Failed query: {}\nusing headers: {}".format(self.url_query,
query_headers))
sys.exit(-1)
""" Returns list of devices
"""
def get_devices(self, limit=0):
query_str = "devices?limit=%s" % (limit)
if self.device_group:
query_str += "&device_group_id[]=%s" % (self.device_group)
devices = self.get(query_str)['data']
shuffle(devices)
return devices
""" Find available Android device
"""
def available_android_device(self, limit=0):
print("Searching available Android device...")
for device in self.get_devices(limit):
if self.device_group:
if (device['online'] is True and
device['locked'] is False and
device['osType'] == "ANDROID" and
device['softwareVersion']['apiLevel'] > 16):
print("Found device '%s'" % device['displayName'])
print("")
return str(device['displayName'])
else:
if (device['online'] is True and
device['creditsPrice'] == 0 and
device['locked'] is False and
device['osType'] == "ANDROID" and
device['softwareVersion']['apiLevel'] > 16):
print("Found device '%s'" % device['displayName'])
print("")
return str(device['displayName'])
print("No available device found")
print("")
return ""
""" Find available iOS device
"""
def available_ios_device(self, limit=0):
print("Searching available iOS device...")
for device in self.get_devices(limit):
if self.device_group:
if (device['online'] is True and
device['locked'] is False and
device['osType'] == "IOS"):
print("Found device '%s'" % device['displayName'])
print("")
return str(device['displayName'])
else:
if (device['online'] is True and
device['creditsPrice'] == 0 and
device['locked'] is False and
device['osType'] == "IOS"):
print("Found device '%s'" % device['displayName'])
print("")
return str(device['displayName'])
print("No available device found")
print("")
return ""
""" Find out the API level of a Device
"""
def device_API_level(self, deviceName):
print("Searching for API level of device '%s'" % deviceName)
try:
devices = self.get(path="devices?search=%s" % deviceName)['data']
picked_device = devices[0]
print("Selected device '{}' has API level '{}'".format(picked_device['displayName'],
picked_device['softwareVersion']['apiLevel']))
return picked_device['softwareVersion']['apiLevel']
except Exception as e:
print("Error: %s" % e)
return
""" DeviceFinder should rather be used from an other script rather
than directly from command line
"""
if __name__ == '__main__':
df = DeviceFinder()
print("DeviceFinder: {}".format(df.available_android_device()))