-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_fetcher.py
More file actions
44 lines (37 loc) · 821 Bytes
/
data_fetcher.py
File metadata and controls
44 lines (37 loc) · 821 Bytes
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
import os
import requests
from dotenv import load_dotenv
load_dotenv()
def fetch_data(animal_name):
"""
Fetches the animals data for the animal 'animal_name'.
Returns: a list of animals, each animal is a dictionary:
{
'name': ...,
'taxonomy': {
...
},
'locations': [
...
],
'characteristics': {
...
}
},
"""
API_URL = "https://api.api-ninjas.com/v1/animals"
API_KEY = os.getenv('API_KEY')
if not API_KEY:
raise RuntimeError("API_KEY is not set")
response = requests.get(
API_URL,
params={"name": animal_name},
headers={"X-Api-Key": API_KEY},
timeout=20,
)
print("Status code:", response.status_code)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
return data
return []