1- from typing import Protocol
1+ import logging
2+ from typing import Protocol , Dict
23import boto3
34import tinydb
45from botocore .exceptions import ClientError
@@ -26,21 +27,24 @@ def build(self, type: str, **kwargs):
2627
2728
2829class TokenCache (Protocol ):
29- def upsert_authorisation_token (self , authorisation ):
30+ def upsert_authorisation_token (self , authorisation : Dict ):
3031 ...
3132
32- def get_authorisation_token (self , athlete_id ) :
33+ def get_authorisation_token (self , athlete_id : int ) -> Dict :
3334 ...
3435
35- def total_tokens (self ):
36+ def delete_authorisation_token (self , athlete_id : int ):
37+ ...
38+
39+ def total_tokens (self ) -> int :
3640 ...
3741
3842
3943class LocalTokenCache :
40- def __init__ (self , filename ):
44+ def __init__ (self , filename : str ):
4145 self .database = tinydb .TinyDB (filename )
4246
43- def upsert_authorisation_token (self , authorisation ):
47+ def upsert_authorisation_token (self , authorisation : Dict ):
4448 athlete_id = authorisation ['athlete' ]['id' ]
4549 auth_expiry = authorisation ['expires_at' ]
4650 auth_access_token = authorisation ['access_token' ]
@@ -52,11 +56,14 @@ def upsert_authorisation_token(self, authorisation):
5256 'refresh_token' : auth_refresh_token },
5357 doc_id = athlete_id ))
5458
55- def get_authorisation_token (self , athlete_id ) :
59+ def get_authorisation_token (self , athlete_id : int ) -> Dict :
5660 return self .database .get (doc_id = athlete_id )
5761
62+ def delete_authorisation_token (self , athlete_id : int ):
63+ self .database .remove (doc_ids = [athlete_id ])
64+
5865 @property
59- def total_tokens (self ):
66+ def total_tokens (self ) -> int :
6067 return len (self .database )
6168
6269
@@ -68,7 +75,7 @@ def __init__(self, aws_access_key_id, aws_secret_access_key, region_name, table_
6875 aws_secret_access_key = aws_secret_access_key )
6976 self .table = self .dynamodb_client .Table (table_name )
7077
71- def upsert_authorisation_token (self , authorisation ):
78+ def upsert_authorisation_token (self , authorisation : Dict ):
7279 athlete_id = authorisation ['athlete' ]['id' ]
7380 auth_expiry = authorisation ['expires_at' ]
7481 auth_access_token = authorisation ['access_token' ]
@@ -83,11 +90,12 @@ def upsert_authorisation_token(self, authorisation):
8390 }
8491 )
8592
86- def get_authorisation_token (self , athlete_id ) :
93+ def get_authorisation_token (self , athlete_id : int ) -> Dict :
8794 try :
8895 response = self .table .get_item (Key = {'athlete_id' : str (athlete_id )})
8996 except ClientError as e :
90- print (e .response ['Error' ]['Message' ])
97+ logging .error ('Failed to retrieve access token' )
98+ logging .error (e .response ['Error' ]['Message' ])
9199 else :
92100 data = response ['Item' ]
93101 expires_at = int (data ['expires_at' ])
@@ -98,6 +106,13 @@ def get_authorisation_token(self, athlete_id):
98106
99107 return data
100108
109+ def delete_authorisation_token (self , athlete_id : int ):
110+ try :
111+ self .table .delete_item (Key = {'athlete_id' : str (athlete_id )})
112+ except ClientError as e :
113+ logging .error (f'Failed to remove athlete { athlete_id } ' )
114+ logging .error (e .response ['Error' ]['Message' ])
115+
101116 @property
102- def total_tokens (self ):
117+ def total_tokens (self ) -> int :
103118 return self .table .item_count
0 commit comments