Skip to content

Commit 585afb1

Browse files
author
“Carmel
committed
Add logging of API calls
1 parent 449f5a1 commit 585afb1

23 files changed

Lines changed: 397 additions & 82 deletions
-283 Bytes
Binary file not shown.
4.61 KB
Binary file not shown.
10.7 KB
Binary file not shown.
2.96 KB
Binary file not shown.

add_individual_to_survey.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
from new_ldot_workflows.qualtrics_settings import BASE_URL, HEADERS, SURVEYIDS, QualtricsAPIError
3+
from new_ldot_workflows.logging_utils import logged_request
34

45
def get_mailing_list_id_of_distribution(survey_id: str, distribution_id: str) -> str:
56
"""Get the mailing list ID connected to a distribution"""
@@ -10,8 +11,15 @@ def get_mailing_list_id_of_distribution(survey_id: str, distribution_id: str) ->
1011
"surveyId": survey_id
1112
}
1213
try:
13-
response = requests.get(endpoint, headers=HEADERS, params=parameters)
14-
response.raise_for_status()
14+
response = logged_request(
15+
"GET",
16+
endpoint,
17+
function_name="get_mailing_list_id_of_distribution",
18+
service="Qualtrics",
19+
headers=HEADERS,
20+
params=parameters,
21+
raise_for_status=True,
22+
)
1523
mailing_list_id = response.json()["result"]["recipients"]["mailingListId"]
1624

1725
return mailing_list_id
@@ -40,8 +48,15 @@ def get_directory_id() -> str:
4048
"includeCount": False
4149
}
4250
try:
43-
response = requests.get(endpoint, headers=HEADERS, params=parameters)
44-
response.raise_for_status()
51+
response = logged_request(
52+
"GET",
53+
endpoint,
54+
function_name="get_directory_id",
55+
service="Qualtrics",
56+
headers=HEADERS,
57+
params=parameters,
58+
raise_for_status=True,
59+
)
4560
data = response.json()
4661
directory_id = data["result"]["elements"][0]["directoryId"]
4762

@@ -69,8 +84,14 @@ def check_contact_in_mailing_list(participant_study_id: str, mailing_list_id: st
6984
print("Checking for contact in mailing list... ")
7085
endpoint = f"{BASE_URL}/directories/{directory_id}/mailinglists/{mailing_list_id}/contacts"
7186
try:
72-
response = requests.get(endpoint, headers=HEADERS)
73-
response.raise_for_status()
87+
response = logged_request(
88+
"GET",
89+
endpoint,
90+
function_name="check_contact_in_mailing_list",
91+
service="Qualtrics",
92+
headers=HEADERS,
93+
raise_for_status=True,
94+
)
7495
contacts = response.json()["result"]["elements"]
7596
except requests.exceptions.RequestException as exc:
7697
raise QualtricsAPIError(
@@ -107,8 +128,15 @@ def add_contact_to_mailing_list(participant_study_id: str, mailing_list_id: str,
107128
"embeddedData": {embedded_data_field: participant_study_id}
108129
}
109130
try:
110-
response = requests.post(endpoint, headers=HEADERS, json=contact_information_payload)
111-
response.raise_for_status()
131+
response = logged_request(
132+
"POST",
133+
endpoint,
134+
function_name="add_contact_to_mailing_list",
135+
service="Qualtrics",
136+
headers=HEADERS,
137+
json=contact_information_payload,
138+
raise_for_status=True,
139+
)
112140

113141
except requests.exceptions.RequestException as exc:
114142
raise QualtricsAPIError(
@@ -137,8 +165,15 @@ def get_personal_link(survey_id: str, distribution_id: str, participant_study_id
137165
"surveyId": str(survey_id)
138166
}
139167
try:
140-
response = requests.get(endpoint, headers=HEADERS, params=parameters)
141-
response.raise_for_status()
168+
response = logged_request(
169+
"GET",
170+
endpoint,
171+
function_name="get_personal_link",
172+
service="Qualtrics",
173+
headers=HEADERS,
174+
params=parameters,
175+
raise_for_status=True,
176+
)
142177
data = response.json()
143178
personal_link = [item["link"] for item in data["result"]["elements"] if ("externalDataReference" in item) and (item["externalDataReference"] == participant_study_id)][0]
144179

get_distributions.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
from new_ldot_workflows.qualtrics_settings import BASE_URL, HEADERS, QualtricsAPIError
3+
from new_ldot_workflows.logging_utils import logged_request
34

45
def get_distributions(survey_id):
56
endpoint = f"{BASE_URL}/distributions"
@@ -8,7 +9,15 @@ def get_distributions(survey_id):
89
"distributionRequestType": "GeneratedInvite"
910
}
1011

11-
response = requests.get(endpoint, headers=HEADERS, params=parameters)
12+
response = logged_request(
13+
"GET",
14+
endpoint,
15+
function_name="get_distributions",
16+
service="Qualtrics",
17+
headers=HEADERS,
18+
params=parameters,
19+
raise_for_status=False,
20+
)
1221

1322
if response.status_code == 200:
1423
result = response.json()["result"]["elements"]
@@ -44,8 +53,15 @@ def get_mailing_list_id_of_distribution(survey_id: str, distribution_id: str) ->
4453
"surveyId": survey_id
4554
}
4655
try:
47-
response = requests.get(endpoint, headers=HEADERS, params=parameters)
48-
response.raise_for_status()
56+
response = logged_request(
57+
"GET",
58+
endpoint,
59+
function_name="get_mailing_list_id_of_distribution",
60+
service="Qualtrics",
61+
headers=HEADERS,
62+
params=parameters,
63+
raise_for_status=True,
64+
)
4965
mailing_list_id = response.json()["result"]["recipients"]["mailingListId"]
5066

5167
return mailing_list_id
@@ -73,8 +89,15 @@ def get_directory_id() -> str:
7389
"includeCount": False
7490
}
7591
try:
76-
response = requests.get(endpoint, headers=HEADERS, params=parameters)
77-
response.raise_for_status()
92+
response = logged_request(
93+
"GET",
94+
endpoint,
95+
function_name="get_directory_id",
96+
service="Qualtrics",
97+
headers=HEADERS,
98+
params=parameters,
99+
raise_for_status=True,
100+
)
78101
data = response.json()
79102
directory_id = data["result"]["elements"][0]["directoryId"]
80103

get_individual_progress.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import zipfile
55
import io
66
import pandas as pd
7+
from new_ldot_workflows.logging_utils import logged_request
78

89
def create_data_export(survey_id: str, incomplete_bool: bool = True) -> str:
910
"""Create a data export request for a given survey.
@@ -23,8 +24,15 @@ def create_data_export(survey_id: str, incomplete_bool: bool = True) -> str:
2324
"exportResponsesInProgress": incomplete_bool
2425
}
2526
try:
26-
response = requests.post(endpoint, headers=HEADERS, json=payload)
27-
response.raise_for_status()
27+
response = logged_request(
28+
"POST",
29+
endpoint,
30+
function_name="create_data_export",
31+
service="Qualtrics",
32+
headers=HEADERS,
33+
json=payload,
34+
raise_for_status=True,
35+
)
2836
request_id = response.json()['result']["progressId"]
2937
return request_id
3038
except requests.exceptions.RequestException as exc:
@@ -53,8 +61,14 @@ def check_export_progress(request_id: str, survey_id: str, returns: str) -> str|
5361
print("Checking on export progress... ")
5462
endpoint = f"{BASE_URL}/surveys/{survey_id}/export-responses/{request_id}"
5563
try:
56-
result = requests.get(endpoint, headers=HEADERS)
57-
result.raise_for_status()
64+
result = logged_request(
65+
"GET",
66+
endpoint,
67+
function_name="check_export_progress",
68+
service="Qualtrics",
69+
headers=HEADERS,
70+
raise_for_status=True,
71+
)
5872

5973
if returns == "percent_complete":
6074
percent_complete = result.json()["result"]["percentComplete"]
@@ -86,12 +100,15 @@ def download_export(file_id: str, survey_id: str) -> pd.DataFrame:
86100
print("Downloading export file... ")
87101
endpoint = f"{BASE_URL}/surveys/{survey_id}/export-responses/{file_id}/file"
88102
try:
89-
download = requests.get(
103+
download = logged_request(
104+
"GET",
90105
endpoint,
106+
function_name="download_export",
107+
service="Qualtrics",
91108
headers=HEADERS,
92-
stream=True
109+
stream=True,
110+
raise_for_status=True,
93111
)
94-
download.raise_for_status()
95112

96113
with zipfile.ZipFile(io.BytesIO(download.content)) as z:
97114
csv_name = [name for name in z.namelist() if name.endswith(".csv")][0]
-12 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)