3434import threading
3535import base64
3636import json
37- import time
3837
3938
4039from datetime import datetime
@@ -87,17 +86,8 @@ def __init__(self, host=None, header_name=None, header_value=None, cookie=None):
8786 # Set default User-Agent.
8887 self .user_agent = 'PureCloud SDK/python'
8988
90- # store clientId and clientSecret to request new access token for OAuth (Code Authorization)
91- self .client_id = ""
92- self .client_secret = ""
9389 # access token for OAuth
9490 self .access_token = ""
95- # refresh token for OAuth (Code Authorization)
96- self .refresh_token = ""
97- # lock object used to ensure 1 thread tries to request a new access token
98- self .refresh_token_lock = threading .Lock ()
99- # flag indicating a token refresh is being carried out
100- self .refresh_in_progress = False
10191
10292
10393
@@ -166,83 +156,6 @@ def get_saml2bearer_token(self,client_id,client_secret,org_name,assertion):
166156 self .access_token = data [0 ]["access_token" ]
167157 return self ;
168158
169- def get_code_authorization_token (self ,client_id ,client_secret ,auth_code ,redirect_uri ):
170- """:param client_id: Client Id to authenticate with
171- :param client_secret: Client Secret to authenticate with
172- :param auth_code: Authorization code
173- :param redirect_uri: Authorized redirect URI for your Code Authorization client
174- :return:
175- """
176-
177- self .client_id = client_id
178- self .client_secret = client_secret
179-
180- query_params = {}
181- body = None
182- url = re .sub (r'\/\/(api)\.' , '//login.' , self .host ) + '/oauth/token'
183-
184- post_params = {'grant_type' : 'authorization_code' ,
185- 'code' : auth_code ,
186- 'redirect_uri' : redirect_uri
187- }
188-
189- auth_string = 'Basic ' + base64 .b64encode (bytes ((client_id + ':' + client_secret ).encode ('ascii' ))).decode (
190- 'ascii' )
191-
192- header_params = {
193- "Authorization" : auth_string ,
194- 'Content-Type' : 'application/x-www-form-urlencoded'
195- }
196- header_params = self .sanitize_for_serialization (header_params )
197- post_params = self .sanitize_for_serialization (post_params )
198-
199- response = self .request ("POST" , url ,
200- query_params = query_params ,
201- headers = header_params ,
202- post_params = post_params , body = body )
203- data = json .loads ('[' + response .data + ']' )
204-
205- self .access_token = data [0 ]["access_token" ]
206- self .refresh_token = data [0 ]["refresh_token" ]
207-
208- return self , data [0 ]
209-
210- def refresh_code_authorization_token (self ,client_id ,client_secret ,refresh_token ):
211- """:param client_id: Client Id to authenticate with
212- :param client_secret: Client Secret to authenticate with
213- :param refresh_token: Refresh token used to request a new access token
214- :return:
215- """
216-
217- query_params = {}
218- body = None
219- url = re .sub (r'\/\/(api)\.' , '//login.' , self .host ) + '/oauth/token'
220-
221- post_params = {'grant_type' : 'refresh_token' ,
222- 'refresh_token' : refresh_token
223- }
224-
225- auth_string = 'Basic ' + base64 .b64encode (bytes ((client_id + ':' + client_secret ).encode ('ascii' ))).decode (
226- 'ascii' )
227-
228- header_params = {
229- "Authorization" : auth_string ,
230- 'Content-Type' : 'application/x-www-form-urlencoded'
231- }
232- header_params = self .sanitize_for_serialization (header_params )
233- post_params = self .sanitize_for_serialization (post_params )
234-
235- response = self .request ("POST" , url ,
236- query_params = query_params ,
237- headers = header_params ,
238- post_params = post_params , body = body )
239- data = json .loads ('[' + response .data + ']' )
240-
241- self .access_token = data [0 ]["access_token" ]
242- self .refresh_token = data [0 ]["refresh_token" ]
243-
244- return self , data [0 ]
245-
246159 @property
247160 def user_agent (self ):
248161 """
@@ -260,42 +173,19 @@ def user_agent(self, value):
260173 def set_default_header (self , header_name , header_value ):
261174 self .default_headers [header_name ] = header_value
262175
263- def handle_expired_access_token (self ):
264- if self .refresh_token_lock .acquire (False ):
265- try :
266- self .refresh_in_progress = True
267- self .refresh_code_authorization_token (self .client_id , self .client_secret , self .refresh_token )
268- finally :
269- self .refresh_in_progress = False
270- self .refresh_token_lock .release ()
271- else :
272- start_time = time .time ()
273- sleep_duration = 0.200
274- # Wait maximum of refresh_token_wait_time seconds for other thread to complete refresh
275- # It would be ideal to use refresh_token_lock.acquire with a timeout value here but that was added in python 3.2 and we have to support older versions
276- while time .time () - start_time < Configuration ().refresh_token_wait_time :
277- time .sleep (sleep_duration )
278- if not self .refresh_in_progress :
279- return
280- # Abort with error if we have waited refresh_token_wait_time seconds and refresh still isn't complete
281- raise ApiException (
282- status = 500 ,
283- reason = "Token refresh took longer than `{0}` seconds"
284- .format (str (Configuration ().refresh_token_wait_time ))
285- )
286-
287176 def __call_api (self , resource_path , method ,
288177 path_params = None , query_params = None , header_params = None ,
289178 body = None , post_params = None , files = None ,
290179 response_type = None , auth_settings = None , callback = None ):
180+
291181 # headers parameters
292182 header_params = header_params or {}
293183 header_params .update (self .default_headers )
294184 if self .cookie :
295185 header_params ['Cookie' ] = self .cookie
296186 if header_params :
297187 header_params = self .sanitize_for_serialization (header_params )
298- header_params ['purecloud-sdk' ] = '107 .0.0'
188+ header_params ['purecloud-sdk' ] = '108 .0.0'
299189
300190 # path parameters
301191 if path_params :
@@ -326,20 +216,11 @@ def __call_api(self, resource_path, method,
326216 # request url
327217 url = self .host + resource_path
328218
329- response_data = None
330-
331- try :
332- # perform request and return response
333- response_data = self .request (method , url , query_params = query_params ,
334- headers = header_params , post_params = post_params , body = body )
335- except ApiException as e :
336- if Configuration ().should_refresh_access_token and e .status == 401 and self .refresh_token != "" :
337- self .handle_expired_access_token ()
338- return self .__call_api (resource_path , method , path_params ,
339- query_params , header_params , body , post_params ,
340- files , response_type , auth_settings , callback )
341- else :
342- raise
219+ # perform request and return response
220+ response_data = self .request (method , url ,
221+ query_params = query_params ,
222+ headers = header_params ,
223+ post_params = post_params , body = body )
343224
344225 self .last_response = response_data
345226
0 commit comments