Skip to content

Commit cdf9c1e

Browse files
committed
Refactor example_1.py to specify cache name and improve error handling; add example_2.py for alternative implementation
1 parent 5cab0bf commit cdf9c1e

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

python_utils/cached_session/example_1.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
URL: Final = 'https://poetrydb.org' # https://github.com/thundercomb/poetrydb#readme
77

88
session = CachedSession(
9-
cache_name='cache/poetry_cache',
9+
cache_name='cache/poetry_cache/example_1',
1010
backend='sqlite',
1111
expire_after=3600,
1212
allowable_methods=['GET'],
@@ -23,12 +23,14 @@ class Poem:
2323

2424

2525
def get_poem(title: str) -> Poem:
26-
response = session.get(f'{URL}/title/{title}/author,title,linecount.json')
2726
try:
27+
response = session.get(f'{URL}/title/{title}/author,title,linecount.json')
2828
result: list = response.json()
2929
if not result:
3030
raise ValueError('No results found')
3131
json: dict = result[0]
32+
source: str = 'CACHE' if getattr(response, 'from_cache', False) else 'API'
33+
print(f'Source: {source}')
3234
poem = Poem(**json)
3335
print(f'Poem {poem.title} by {poem.author} with {poem.linecount} lines')
3436
except Exception as e:
@@ -38,3 +40,6 @@ def get_poem(title: str) -> Poem:
3840
if __name__ == '__main__':
3941
get_poem('Ozymandias')
4042
get_poem('Easter Week')
43+
print('------------------')
44+
get_poem('Ozymandias')
45+
get_poem('Easter Week')
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import requests
2+
import requests_cache
3+
from dataclasses import dataclass
4+
from requests import Response
5+
from typing import Final
6+
7+
8+
URL: Final = 'https://poetrydb.org' # https://github.com/thundercomb/poetrydb#readme
9+
10+
requests_cache.install_cache('cache/poetry_cache/example_2',
11+
expire_after=3600,
12+
backend='sqlite',
13+
allowable_methods=['GET'],
14+
allowable_codes=(200, 404),
15+
old_cache=True)
16+
17+
18+
@dataclass
19+
class Poem:
20+
title: str
21+
author: str
22+
linecount: str
23+
24+
25+
def get_poem(title: str) -> Poem:
26+
try:
27+
response: Response = requests.get(f'{URL}/title/{title}/author,title,linecount.json')
28+
response.raise_for_status() # Raise an error for bad responses
29+
result: list = response.json()
30+
if not result:
31+
raise ValueError('No results found')
32+
json: dict = result[0]
33+
source: str = 'CACHE' if getattr(response, 'from_cache', False) else 'API'
34+
print(f'Source: {source}')
35+
poem = Poem(**json)
36+
print(f'Poem {poem.title} by {poem.author} with {poem.linecount} lines')
37+
except Exception as e:
38+
print(f'{response.status_code} - {e}')
39+
40+
41+
if __name__ == '__main__':
42+
get_poem('Ozymandias')
43+
get_poem('Easter Week')
44+
print('------------------')
45+
get_poem('Ozymandias')
46+
get_poem('Easter Week')

0 commit comments

Comments
 (0)