44import queue
55import threading
66import time
7- from datetime import datetime , timedelta , timezone
7+ from contextlib import suppress
8+ from datetime import UTC , datetime , timedelta
89
910import keyring
1011import pytz
@@ -40,7 +41,7 @@ def __init__(self, base_url):
4041 self .logger .addHandler (handler )
4142
4243 REDIS_HOST = os .environ .get ("REDIS_HOST" , "localhost" )
43- REDIS_PORT = int (os .environ .get ("REDIS_PORT" , 6379 ))
44+ REDIS_PORT = int (os .environ .get ("REDIS_PORT" , " 6379" ))
4445
4546 # Initialize Redis client with error handling
4647 try :
@@ -68,7 +69,7 @@ def __init__(self, base_url):
6869 )
6970 except redis .ConnectionError as e :
7071 self .logger .error (
71- f"Unable to connect to Redis at { REDIS_HOST } :{ REDIS_PORT } .\n ERROR: { str ( e ) } "
72+ f"Unable to connect to Redis at { REDIS_HOST } :{ REDIS_PORT } .\n ERROR: { e !s } "
7273 )
7374 self .redis_client = None
7475 self .pubsub = None
@@ -104,7 +105,7 @@ def _load_stored_tokens(self):
104105 self .token_expiry = None
105106
106107 except Exception as e :
107- self .logger .error (f"Error loading stored tokens: { str ( e ) } " )
108+ self .logger .error (f"Error loading stored tokens: { e !s } " )
108109 # If there's an error, start with clean slate
109110 self .access_token = None
110111 self .refresh_token = None
@@ -142,26 +143,20 @@ def _store_tokens(self, access_token=None, refresh_token=None, token_expiry=None
142143 self .logger .info ("Removed stored token expiry" )
143144
144145 except Exception as e :
145- self .logger .error (f"Error storing tokens: { str ( e ) } " )
146+ self .logger .error (f"Error storing tokens: { e !s } " )
146147
147148 def _clear_stored_tokens (self ):
148149 """
149150 Clears all stored tokens from secure storage.
150151 """
151- try :
152+ with suppress ( keyring . errors . PasswordDeleteError ) :
152153 keyring .delete_password ("flet-chat" , "access_token" )
153- except keyring .errors .PasswordDeleteError :
154- pass # Token doesn't exist
155154
156- try :
155+ with suppress ( keyring . errors . PasswordDeleteError ) :
157156 keyring .delete_password ("flet-chat" , "refresh_token" )
158- except keyring .errors .PasswordDeleteError :
159- pass # Token doesn't exist
160157
161- try :
158+ with suppress ( keyring . errors . PasswordDeleteError ) :
162159 keyring .delete_password ("flet-chat" , "token_expiry" )
163- except keyring .errors .PasswordDeleteError :
164- pass # Token doesn't exist
165160
166161 self .logger .info ("Cleared all stored tokens" )
167162
@@ -175,7 +170,7 @@ def is_authenticated(self):
175170
176171 # Check if token is expired (with 5 minute buffer)
177172 if self .token_expiry :
178- current_time = datetime .now (timezone . utc )
173+ current_time = datetime .now (UTC )
179174 if current_time >= self .token_expiry - timedelta (minutes = 5 ):
180175 return False
181176
@@ -197,13 +192,13 @@ def _listen_to_pubsub(self):
197192 self .message_queue .put ({"channel" : channel , "data" : data })
198193 except redis .ConnectionError as e :
199194 self .logger .error (
200- f"Redis connection error: { str ( e ) } . Attempting to reconnect in 5 seconds..."
195+ f"Redis connection error: { e !s } . Attempting to reconnect in 5 seconds..."
201196 )
202197 time .sleep (5 ) # Wait before reconnecting
203198 self ._reconnect_redis ()
204199 except Exception as e :
205200 self .logger .error (
206- f"Unexpected error in pubsub listener: { str ( e ) } . Continuing..."
201+ f"Unexpected error in pubsub listener: { e !s } . Continuing..."
207202 )
208203
209204 def _process_messages (self ):
@@ -221,7 +216,7 @@ def _process_messages(self):
221216 callback (data )
222217 except Exception as e :
223218 self .logger .error (
224- f"Error in callback for channel '{ channel } ': { str ( e ) } "
219+ f"Error in callback for channel '{ channel } ': { e !s } "
225220 )
226221
227222 def _handle_response (self , response ):
@@ -278,7 +273,7 @@ def _refresh_token(self):
278273 self ._clear_stored_tokens ()
279274 return False
280275 except Exception as e :
281- self .logger .error (f"Exception during token refresh: { str ( e ) } " )
276+ self .logger .error (f"Exception during token refresh: { e !s } " )
282277 return False
283278
284279 def _request (self , method , endpoint , auth_required = True , ** kwargs ):
@@ -289,15 +284,17 @@ def _request(self, method, endpoint, auth_required=True, **kwargs):
289284 headers = kwargs .get ("headers" , {})
290285
291286 if auth_required :
292- current_time = datetime .now (timezone .utc )
293- if not self .access_token or (
294- self .token_expiry
295- and current_time >= self .token_expiry - timedelta (minutes = 5 )
296- ):
297- if not self ._refresh_token ():
298- return ApiResponse (
299- False , error = "Failed to refresh token. Please log in again."
287+ current_time = datetime .now (UTC )
288+ if (
289+ not self .access_token
290+ or (
291+ self .token_expiry
292+ and current_time >= self .token_expiry - timedelta (minutes = 5 )
300293 )
294+ ) and not self ._refresh_token ():
295+ return ApiResponse (
296+ False , error = "Failed to refresh token. Please log in again."
297+ )
301298
302299 headers ["Authorization" ] = f"Bearer { self .access_token } "
303300 kwargs ["headers" ] = headers
@@ -307,9 +304,9 @@ def _request(self, method, endpoint, auth_required=True, **kwargs):
307304 api_response = self ._handle_response (response )
308305
309306 if (
310- not api_response .success
311- and api_response .status_code == 401
312- and "Could not validate credentials" in (api_response .error or "" )
307+ not api_response .success
308+ and api_response .status_code == 401
309+ and "Could not validate credentials" in (api_response .error or "" )
313310 ):
314311 self .logger .warning (
315312 "Received 401 Unauthorized. Attempting to refresh token."
@@ -322,7 +319,7 @@ def _request(self, method, endpoint, auth_required=True, **kwargs):
322319
323320 return api_response
324321 except Exception as e :
325- self .logger .error (f"HTTP request exception: { str ( e ) } " )
322+ self .logger .error (f"HTTP request exception: { e !s } " )
326323 return ApiResponse (False , error = str (e ))
327324
328325 def subscribe_to_channel (self , channel_name , callback ):
@@ -372,15 +369,15 @@ def _handle_redis_message(self, message):
372369 self .subscriptions [channel ](data )
373370 except Exception as e :
374371 self .logger .error (
375- f"Error in callback for channel '{ channel } ': { str ( e ) } "
372+ f"Error in callback for channel '{ channel } ': { e !s } "
376373 )
377374
378375 def _reconnect_redis (self ):
379376 """
380377 Attempts to reconnect to Redis and resubscribe to channels.
381378 """
382379 REDIS_HOST = os .environ .get ("REDIS_HOST" , "localhost" )
383- REDIS_PORT = int (os .environ .get ("REDIS_PORT" , 6379 ))
380+ REDIS_PORT = int (os .environ .get ("REDIS_PORT" , " 6379" ))
384381 try :
385382 self .redis_client = redis .Redis (
386383 host = REDIS_HOST ,
@@ -392,13 +389,13 @@ def _reconnect_redis(self):
392389 self .redis_client .ping ()
393390 self .pubsub = self .redis_client .pubsub ()
394391 # Resubscribe to existing channels
395- for channel in self .subscriptions . keys () :
392+ for channel in self .subscriptions :
396393 self .pubsub .subscribe (** {channel : self ._handle_redis_message })
397394 self .logger .info (f"Resubscribed to Redis channel '{ channel } '" )
398395 self .logger .info (f"Reconnected to Redis at { REDIS_HOST } :{ REDIS_PORT } " )
399396 except redis .ConnectionError as e :
400397 self .logger .error (
401- f"Failed to reconnect to Redis: { str ( e ) } . Will retry in 5 seconds."
398+ f"Failed to reconnect to Redis: { e !s } . Will retry in 5 seconds."
402399 )
403400 time .sleep (5 )
404401 self ._reconnect_redis ()
0 commit comments