-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_report_config.py
More file actions
144 lines (117 loc) · 4.85 KB
/
Copy pathapi_report_config.py
File metadata and controls
144 lines (117 loc) · 4.85 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import pandas as pd
import requests
import time
import sys
import json
def load_file(input_file_path, file_extension):
if file_extension == '.json':
with open(input_file_path, 'r') as f:
return pd.json_normalize(json.load(f))
else:
raise ValueError("TRAINING LMS API Report requires a .json file.")
def get_api_token():
"""Capture API Token from OS environment variable."""
api_token = os.getenv('TRAINING_API_TOKEN')
if not api_token:
raise EnvironmentError("API token not found in environment variables. Please set the TRAINING_API_TOKEN environment variable.")
return api_token
def get_api_url():
"""Capture API Base URL from OS environment variable."""
return os.getenv('TRAINING_API_URL', 'https://api.example.com')
#%%
def export_report(api_token, report_id):
"""Initiate report export and retrieve execution_id."""
api_url = get_api_url()
url = f'{api_url}/{report_id}/export/csv'
#url = f'{api_url}/{report_id}/export/xlsx'
headers = {
"accept": "application/json, text/plain, */*",
"authorization": f"Bearer {api_token}",
"content-type": "application/json"
}
response = requests.get(url, headers=headers)
# debug api request with prints
# print(f'URL: {response.url}')
# print(f'Status Code: {response.status_code}')
# print(f'Headers: {response.request.headers}')
# print(f'Response: {response.text}')
response.raise_for_status()
json_response = response.json()
execution_id = json_response['data']['executionId']
print(f'Execution ID: {execution_id}')
return execution_id
#%%
def check_report_status(api_token, report_id, execution_id):
"""Check the status of the report export until it is completed."""
api_url = get_api_url()
url = f'{api_url}/{report_id}/exports/{execution_id}'
headers = {
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json"
}
while True:
response = requests.get(url, headers=headers)
#debug api request with prints
# print(f'URL: {response.url}')
# print(f'Status Code: {response.status_code}')
# print(f'Headers: {response.request.headers}')
# print(f'Response: {response.text}')
response.raise_for_status()
json_response = response.json()
status = json_response['data']['status']
if status == 'SUCCEEDED':
break
elif status == 'FAILED':
raise Exception('Report export failed.')
time.sleep(15) # Wait for 15 seconds before retrying
print("Checking status...")
#%%
def download_report(api_token, report_id, execution_id, output_path):
"""Download the exported report file to the specified filepath."""
api_url = get_api_url()
url = f'{api_url}/{report_id}/exports/{execution_id}/download'
headers = {
"Authorization": f"Bearer {api_token}",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
# debug api request with prints
# print(f'URL: {response.url}')
# print(f'Status Code: {response.status_code}')
# print(f'Headers: {response.request.headers}')
# print(f'Response: {response.text}')
response.raise_for_status()
with open(output_path, 'wb') as file:
file.write(response.content)
print(f'File downloaded to {output_path}')
def process(input_file_path, file_extension, report_output_dir):
df = load_file(input_file_path, file_extension)
print(f'Processing TRAINING LMS API Report for file: {input_file_path}')
# Capture API token from OS environment variable
try:
api_token = get_api_token()
except EnvironmentError as e:
print(e)
sys.exit(1)
#Read config file from the specified path
with open(input_file_path, 'r') as config_file:
config =json.load(config_file)
for report in config['reports']:
report_id = report['report_id']
filename = report['filename']
output_path= os.path.join(report_output_dir, filename)
try:
# Step 1: Make API call to export the report
print('Exporting report...')
execution_id = export_report(api_token, report_id)
# Step 2: Check the status of the report export until it is completed
print('Checking report status...')
check_report_status(api_token, report_id, execution_id)
# Step 3: Download the report file once the status is completed
print('Downloading report...')
download_report(api_token, report_id, execution_id, output_path)
print('Report download completed.')
except requests.RequestException as e:
print(f'Error occurred: {e}')
sys.exit(1)