-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_2.py
More file actions
46 lines (37 loc) · 1.4 KB
/
example_2.py
File metadata and controls
46 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import requests_cache
from dataclasses import dataclass
from requests import Response
from typing import Final
URL: Final = 'https://poetrydb.org' # https://github.com/thundercomb/poetrydb#readme
requests_cache.install_cache('cache/poetry_cache/example_2',
expire_after=3600,
backend='sqlite',
allowable_methods=['GET'],
allowable_codes=(200, 404),
old_cache=True)
@dataclass
class Poem:
title: str
author: str
linecount: str
def get_poem(title: str) -> Poem:
try:
response: Response = requests.get(f'{URL}/title/{title}/author,title,linecount.json')
response.raise_for_status() # Raise an error for bad responses
result: list = response.json()
if not result:
raise ValueError('No results found')
json: dict = result[0]
source: str = 'CACHE' if getattr(response, 'from_cache', False) else 'API'
print(f'Source: {source}')
poem = Poem(**json)
print(f'Poem {poem.title} by {poem.author} with {poem.linecount} lines')
except Exception as e:
print(f'{response.status_code} - {e}')
if __name__ == '__main__':
get_poem('Ozymandias')
get_poem('Easter Week')
print('------------------')
get_poem('Ozymandias')
get_poem('Easter Week')