Skip to content

Commit 035b308

Browse files
authored
Merge pull request #1 from JozefTkocz/feature/delete_athletes_from_token_store
Feature/delete athletes from token store
2 parents d035871 + 5581257 commit 035b308

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def read_requirements(requirements_file):
1616
setup(
1717
name="stravaclient",
1818
python_requires=">=3.8,<4.0",
19-
version="0.0.4",
19+
version="0.0.5",
2020
author="Jozef Tkocz",
2121
author_email="[email protected]",
2222
description=("Python client for the Strava API"),

stravaclient/storage/tokens.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Protocol
1+
import logging
2+
from typing import Protocol, Dict
23
import boto3
34
import tinydb
45
from botocore.exceptions import ClientError
@@ -26,21 +27,24 @@ def build(self, type: str, **kwargs):
2627

2728

2829
class 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

3943
class 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

tests/test_storage.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ def test_total_tokens_returns_one_when_one_token(self):
4646
assert cache.total_tokens == 1
4747
self.teardown_db(cache)
4848

49+
def test_delete_authorisation_token_removes_token_from_database(self):
50+
cache = LocalTokenCache(self.test_db_file)
51+
authorisation = {'athlete': {'id': 123},
52+
'expires_at': 1,
53+
'access_token': 'abc',
54+
'refresh_token': 'abc123'}
55+
cache.upsert_authorisation_token(authorisation=authorisation)
56+
cache.delete_authorisation_token(athlete_id=123)
57+
58+
document = cache.get_authorisation_token(athlete_id=123)
59+
assert document is None
60+
self.teardown_db(cache)
61+
4962

5063
class TestDynamoDBCache:
5164
class MockDynamoTable:

0 commit comments

Comments
 (0)