-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_functions.py
More file actions
23 lines (20 loc) · 837 Bytes
/
api_functions.py
File metadata and controls
23 lines (20 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import requests
from dao_classes import Monster
# direct api connection not prefered here (use download_json to download json data and json.loads to load data in program memory)
def request_monster_api(index_name: str) -> Monster:
"""
Send a request to the DnD API for a monster's characteristic
:param index_name: name of the monster
:return: Monster object
"""
# api-endpoint
url = f"https://www.dnd5eapi.co/api/monsters/{index_name}"
r = requests.get(url=url)
# extracting data in json format
data = r.json()
return Monster(name=data['name'],
armor_class=data['armor_class'],
hit_points=data['hit_points'],
hit_dice=data['hit_dice'],
xp=data['xp'],
challenge_rating=data['challenge_rating'])