File tree Expand file tree Collapse file tree 2 files changed +53
-2
lines changed
python_utils/cached_session Expand file tree Collapse file tree 2 files changed +53
-2
lines changed Original file line number Diff line number Diff line change 6
6
URL : Final = 'https://poetrydb.org' # https://github.com/thundercomb/poetrydb#readme
7
7
8
8
session = CachedSession (
9
- cache_name = 'cache/poetry_cache' ,
9
+ cache_name = 'cache/poetry_cache/example_1 ' ,
10
10
backend = 'sqlite' ,
11
11
expire_after = 3600 ,
12
12
allowable_methods = ['GET' ],
@@ -23,12 +23,14 @@ class Poem:
23
23
24
24
25
25
def get_poem (title : str ) -> Poem :
26
- response = session .get (f'{ URL } /title/{ title } /author,title,linecount.json' )
27
26
try :
27
+ response = session .get (f'{ URL } /title/{ title } /author,title,linecount.json' )
28
28
result : list = response .json ()
29
29
if not result :
30
30
raise ValueError ('No results found' )
31
31
json : dict = result [0 ]
32
+ source : str = 'CACHE' if getattr (response , 'from_cache' , False ) else 'API'
33
+ print (f'Source: { source } ' )
32
34
poem = Poem (** json )
33
35
print (f'Poem { poem .title } by { poem .author } with { poem .linecount } lines' )
34
36
except Exception as e :
@@ -38,3 +40,6 @@ def get_poem(title: str) -> Poem:
38
40
if __name__ == '__main__' :
39
41
get_poem ('Ozymandias' )
40
42
get_poem ('Easter Week' )
43
+ print ('------------------' )
44
+ get_poem ('Ozymandias' )
45
+ get_poem ('Easter Week' )
Original file line number Diff line number Diff line change
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' )
You can’t perform that action at this time.
0 commit comments