17
17
from optparse import OptionParser
18
18
from datetime import datetime
19
19
20
- __version__ = '2.100 .0'
20
+ __version__ = '3 .0'
21
21
22
22
FORMAT = "%(message)s"
23
23
logging .basicConfig (format = FORMAT )
@@ -109,12 +109,6 @@ class Testdroid:
109
109
url = None
110
110
# Api Key for authentication
111
111
api_key = None
112
- # Oauth access token
113
- access_token = None
114
- # Oauth refresh token
115
- refresh_token = None
116
- # Unix timestamp (seconds) when token expires
117
- token_expiration_time = None
118
112
# Buffer size used for downloads
119
113
download_buffer_size = 65536
120
114
# polling interval when awaiting for test run completion
@@ -126,20 +120,12 @@ def __init__(self, **kwargs):
126
120
""" Constructor, defaults against cloud.bitbar.com """
127
121
128
122
self .api_key = kwargs .get ('apikey' )
129
- self .username = kwargs .get ('username' )
130
- self .password = kwargs .get ('password' )
131
123
self .cloud_url = kwargs .get ('url' ) or "https://cloud.bitbar.com"
132
124
self .download_buffer_size = kwargs .get ('download_buffer_size' ) or 65536
133
125
134
126
def set_apikey (self , apikey ):
135
127
self .api_key = apikey
136
128
137
- def set_username (self , username ):
138
- self .username = username
139
-
140
- def set_password (self , password ):
141
- self .password = password
142
-
143
129
def set_url (self , url ):
144
130
self .cloud_url = url
145
131
@@ -149,56 +135,6 @@ def set_download_buffer_size(self, download_buffer_size):
149
135
def set_polling_interval_mins (self , polling_interval_mins ):
150
136
self .polling_interval_mins = polling_interval_mins
151
137
152
- def get_token (self ):
153
- """ Get Oauth2 token """
154
-
155
- if not self .access_token :
156
- # TODO: refresh
157
- url = "%s/oauth/token" % self .cloud_url
158
- payload = {
159
- "client_id" : "testdroid-cloud-api" ,
160
- "grant_type" : "password" ,
161
- "username" : self .username ,
162
- "password" : self .password
163
- }
164
- res = requests .post (
165
- url ,
166
- data = payload ,
167
- headers = {"Accept" : "application/json" }
168
- )
169
- if res .status_code not in list (range (200 , 300 )):
170
- raise RequestResponseError (res .text , res .status_code )
171
-
172
- reply = res .json ()
173
-
174
- self .access_token = reply ['access_token' ]
175
- self .refresh_token = reply ['refresh_token' ]
176
- self .token_expiration_time = time .time () + reply ['expires_in' ]
177
- elif self .token_expiration_time < time .time ():
178
- url = "%s/oauth/token" % self .cloud_url
179
- payload = {
180
- "client_id" : "testdroid-cloud-api" ,
181
- "grant_type" : "refresh_token" ,
182
- "refresh_token" : self .refresh_token
183
- }
184
- res = requests .post (
185
- url ,
186
- data = payload ,
187
- headers = {"Accept" : "application/json" }
188
- )
189
- if res .status_code not in list (range (200 , 300 )):
190
- print ("FAILED: Unable to get a new access token using refresh token" )
191
- self .access_token = None
192
- return self .get_token ()
193
-
194
- reply = res .json ()
195
-
196
- self .access_token = reply ['access_token' ]
197
- self .refresh_token = reply ['refresh_token' ]
198
- self .token_expiration_time = time .time () + reply ['expires_in' ]
199
-
200
- return self .access_token
201
-
202
138
def __build_headers (self ):
203
139
""" Helper method for getting necessary headers to use for API calls, including authentication """
204
140
@@ -208,7 +144,7 @@ def __build_headers(self):
208
144
'Accept' : 'application/json' }
209
145
return apikey
210
146
else :
211
- return {'Authorization' : 'Bearer %s' % self . get_token (), ' Accept' : 'application/json' }
147
+ return {'Accept' : 'application/json' }
212
148
213
149
def download (self , path = None , filename = None , payload = None , callback = None ):
214
150
""" Download file from API resource """
@@ -280,7 +216,7 @@ def get(self, path, payload=None, headers=None):
280
216
if res .status_code not in list (range (200 , 300 )):
281
217
raise RequestResponseError (res .text , res .status_code )
282
218
logger .debug (res .text )
283
- if headers [ 'Accept' ] == 'application/json' :
219
+ if headers . get ( 'Accept' ) == 'application/json' :
284
220
return res .json ()
285
221
else :
286
222
return res .text
@@ -514,15 +450,6 @@ def wait_test_run(self, project_id, test_run_id):
514
450
515
451
while True :
516
452
time .sleep (self .polling_interval_mins * 60 )
517
- # WORKAROUND: access token thinks it's still valid,
518
- # > token valid for another 633.357925177
519
- # whilst this happens:
520
- # > Couldn't establish the state of the test run with id: 72593732. Aborting
521
- # > {u'error_description': u'Invalid access token: b3e62604-9d2a-49dc-88f5-89786ff5a6b6',
522
- # > u'error': u'invalid_token'}
523
- if not self .api_key :
524
- self .access_token = None
525
- self .get_token () # in case it expired
526
453
test_run_status = self .get_test_run (project_id , test_run_id )
527
454
if test_run_status and 'state' in test_run_status :
528
455
if test_run_status ['state' ] == "FINISHED" :
@@ -887,12 +814,6 @@ def format_epilog(self, formatter):
887
814
parser .add_option ("-k" , "--apikey" , dest = "apikey" ,
888
815
help = "API key - the API key for Bitbar Cloud. Optional. "
889
816
"You can use environment variable TESTDROID_APIKEY as well." )
890
- parser .add_option ("-u" , "--username" , dest = "username" ,
891
- help = "Username - the email address. Optional. "
892
- "You can use environment variable TESTDROID_USERNAME as well." )
893
- parser .add_option ("-p" , "--password" , dest = "password" ,
894
- help = "Password. Required if username is used. "
895
- "You can use environment variable TESTDROID_PASSWORD as well." )
896
817
parser .add_option ("-c" , "--url" , dest = "url" , default = "https://cloud.bitbar.com" ,
897
818
help = "Cloud endpoint. Default is https://cloud.bitbar.com. "
898
819
"You can use environment variable TESTDROID_URL as well." )
@@ -968,8 +889,6 @@ def cli(self, parser, commands):
968
889
if options .quiet :
969
890
logger .setLevel (logging .WARNING )
970
891
971
- username = options .username or os .environ .get ('TESTDROID_USERNAME' )
972
- password = options .password or os .environ .get ('TESTDROID_PASSWORD' )
973
892
apikey = options .apikey or os .environ .get ('TESTDROID_APIKEY' )
974
893
url = os .environ .get ('TESTDROID_URL' ) or options .url
975
894
@@ -978,8 +897,6 @@ def cli(self, parser, commands):
978
897
except :
979
898
polling_interval_mins = 10
980
899
981
- self .set_username (username )
982
- self .set_password (password )
983
900
self .set_apikey (apikey )
984
901
self .set_url (url )
985
902
self .set_polling_interval_mins (polling_interval_mins )
0 commit comments