1717from optparse import OptionParser
1818from datetime import datetime
1919
20- __version__ = '2.100 .0'
20+ __version__ = '3 .0'
2121
2222FORMAT = "%(message)s"
2323logging .basicConfig (format = FORMAT )
@@ -109,12 +109,6 @@ class Testdroid:
109109 url = None
110110 # Api Key for authentication
111111 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
118112 # Buffer size used for downloads
119113 download_buffer_size = 65536
120114 # polling interval when awaiting for test run completion
@@ -126,20 +120,12 @@ def __init__(self, **kwargs):
126120 """ Constructor, defaults against cloud.bitbar.com """
127121
128122 self .api_key = kwargs .get ('apikey' )
129- self .username = kwargs .get ('username' )
130- self .password = kwargs .get ('password' )
131123 self .cloud_url = kwargs .get ('url' ) or "https://cloud.bitbar.com"
132124 self .download_buffer_size = kwargs .get ('download_buffer_size' ) or 65536
133125
134126 def set_apikey (self , apikey ):
135127 self .api_key = apikey
136128
137- def set_username (self , username ):
138- self .username = username
139-
140- def set_password (self , password ):
141- self .password = password
142-
143129 def set_url (self , url ):
144130 self .cloud_url = url
145131
@@ -149,56 +135,6 @@ def set_download_buffer_size(self, download_buffer_size):
149135 def set_polling_interval_mins (self , polling_interval_mins ):
150136 self .polling_interval_mins = polling_interval_mins
151137
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-
202138 def __build_headers (self ):
203139 """ Helper method for getting necessary headers to use for API calls, including authentication """
204140
@@ -208,7 +144,7 @@ def __build_headers(self):
208144 'Accept' : 'application/json' }
209145 return apikey
210146 else :
211- return {'Authorization' : 'Bearer %s' % self . get_token (), ' Accept' : 'application/json' }
147+ return {'Accept' : 'application/json' }
212148
213149 def download (self , path = None , filename = None , payload = None , callback = None ):
214150 """ Download file from API resource """
@@ -280,7 +216,7 @@ def get(self, path, payload=None, headers=None):
280216 if res .status_code not in list (range (200 , 300 )):
281217 raise RequestResponseError (res .text , res .status_code )
282218 logger .debug (res .text )
283- if headers [ 'Accept' ] == 'application/json' :
219+ if headers . get ( 'Accept' ) == 'application/json' :
284220 return res .json ()
285221 else :
286222 return res .text
@@ -514,15 +450,6 @@ def wait_test_run(self, project_id, test_run_id):
514450
515451 while True :
516452 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
526453 test_run_status = self .get_test_run (project_id , test_run_id )
527454 if test_run_status and 'state' in test_run_status :
528455 if test_run_status ['state' ] == "FINISHED" :
@@ -887,12 +814,6 @@ def format_epilog(self, formatter):
887814 parser .add_option ("-k" , "--apikey" , dest = "apikey" ,
888815 help = "API key - the API key for Bitbar Cloud. Optional. "
889816 "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." )
896817 parser .add_option ("-c" , "--url" , dest = "url" , default = "https://cloud.bitbar.com" ,
897818 help = "Cloud endpoint. Default is https://cloud.bitbar.com. "
898819 "You can use environment variable TESTDROID_URL as well." )
@@ -968,8 +889,6 @@ def cli(self, parser, commands):
968889 if options .quiet :
969890 logger .setLevel (logging .WARNING )
970891
971- username = options .username or os .environ .get ('TESTDROID_USERNAME' )
972- password = options .password or os .environ .get ('TESTDROID_PASSWORD' )
973892 apikey = options .apikey or os .environ .get ('TESTDROID_APIKEY' )
974893 url = os .environ .get ('TESTDROID_URL' ) or options .url
975894
@@ -978,8 +897,6 @@ def cli(self, parser, commands):
978897 except :
979898 polling_interval_mins = 10
980899
981- self .set_username (username )
982- self .set_password (password )
983900 self .set_apikey (apikey )
984901 self .set_url (url )
985902 self .set_polling_interval_mins (polling_interval_mins )
0 commit comments