-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
55 lines (41 loc) · 1.64 KB
/
main.py
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
47
48
49
50
51
52
53
54
55
import requests
import os
from db.insert_data import insert_data, log_api_call
# Load environment variables if in development
if os.getenv('ENVIRONMENT') == 'development':
from dotenv import load_dotenv
load_dotenv()
API_URL = os.getenv('MATOMO_API_URL')
# Fetch data from the API, handle errors, and log the call into db.
def fetch_data(url):
try:
response = requests.get(url)
# Raise an exception for HTTP error responses
response.raise_for_status()
data = response.json()
# Check if the data is a list
if isinstance(data, list):
total_rows = len(data)
else:
total_rows = 0
# Log successful API call with no error message
log_api_call(status="success", status_code=response.status_code, total_rows_found=total_rows,
error_message=None)
return data
except requests.RequestException as e:
log_api_call(status="failure", status_code=getattr(e.response, 'status_code', 'N/A'), total_rows_found=0,
error_message=str(e))
return None
except ValueError as e:
log_api_call(status="failure", status_code=response.status_code if 'response' in locals() else 'N/A',
total_rows_found=0, error_message=f"JSON decoding failed: {e}")
return None
def main():
"""Main function to fetch data and insert it into the database."""
data = fetch_data(API_URL)
if data:
insert_data(data) # Insert data into the database if fetching was successful
else:
print("No data to insert into the database.")
if __name__ == "__main__":
main()