-
Notifications
You must be signed in to change notification settings - Fork 0
/
call-api.py
71 lines (59 loc) · 1.75 KB
/
call-api.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import json
import requests
import os
from dotenv import load_dotenv
load_dotenv()
# ------------------
# ------------------
# Prepare data from experiment
# ------------------
# ------------------
# Load JSON data
with open('data.json', 'r') as file:
experiment_data = json.load(file)
# Set variables to pass to the API
participant_id = experiment_data[0]['participant_id']
survey_id = experiment_data[0]['survey_id']
questions = {}
question_histories = {}
responses = {}
# Loop through every element in the experiment_data and print
for element in experiment_data:
if element['trial_type'] == 'survey-text':
# Add alias data by merging dictionaries
questions = {**questions, **element['alias_questions']}
question_histories = {**question_histories, **element['alias_question_histories']}
responses = {**responses, **element['alias_responses']}
print('------------------')
# ------------------
# ------------------
# Call API
# ------------------
# ------------------
api_key = os.getenv('ROUNDTABLE_API_KEY')
body = {
'questions': questions,
'question_histories': question_histories,
'responses': responses,
'survey_id': survey_id,
'participant_id': participant_id,
}
# Headers to be sent in the request
headers = {
"Content-Type": "application/json",
"api_key": api_key # API key goes in the headers
}
# Make request
response = requests.post(
'https://roundtable.ai/api/alias/v011',
json=body,
headers=headers
)
if response.status_code == 200:
# Get the body of the response and print
results_data = response.json()
print('Success: ', results_data)
else:
# Get the body of the response and print
response_body = response.json()
print('Error: ', response_body)