66import json
77
88from new_ldot_workflows .logging_utils import QualtricsAPIError , logged_request
9- # from ldot_client import LdotClient
109
11- with open ("new_ldot_workflows/qualtrics_config.json" ) as f :
12- config = json .load (f )
13- QUALTRICS_BASE_URL = config ["QUALTRICS_BASE_URL" ]
14- HEADERS = config ["HEADERS" ]
15-
16- def create_data_export (survey_id : str , incomplete_bool : bool = True ) -> str :
10+ def create_data_export (qualtrics_client , survey_id : str , incomplete_bool : bool = True ) -> str :
1711 """Create a data export request for a given survey.
1812
1913 Args:
@@ -32,10 +26,10 @@ def create_data_export(survey_id: str, incomplete_bool: bool = True) -> str:
3226 try :
3327 response = logged_request (
3428 "POST" ,
35- f"{ QUALTRICS_BASE_URL } /surveys/{ survey_id } /export-responses" ,
29+ f"{ qualtrics_client . api_url } /surveys/{ survey_id } /export-responses" ,
3630 function_name = "create_data_export" ,
3731 service = "Qualtrics" ,
38- headers = HEADERS ,
32+ headers = qualtrics_client . headers ,
3933 json = payload ,
4034 raise_for_status = True ,
4135 )
@@ -51,10 +45,11 @@ def create_data_export(survey_id: str, incomplete_bool: bool = True) -> str:
5145 ) from exc
5246
5347
54- def check_export_progress (request_id : str , survey_id : str , returns : str ) -> str | float :
48+ def check_export_progress (qualtrics_client , request_id : str , survey_id : str , returns : str ) -> str | float :
5549 """Check if a an export request is complete
5650
5751 Args:
52+ qualtrics_client: The Qualtrics API client.
5853 request_id (str): The ID of the export request.
5954 survey_id (str): The ID of the survey that was exported.
6055 returns (str): The type of information to return ("percent_complete" or "fileID").
@@ -68,10 +63,10 @@ def check_export_progress(request_id: str, survey_id: str, returns: str) -> str|
6863 try :
6964 result = logged_request (
7065 "GET" ,
71- f"{ QUALTRICS_BASE_URL } /surveys/{ survey_id } /export-responses/{ request_id } " ,
66+ f"{ qualtrics_client . api_url } /surveys/{ survey_id } /export-responses/{ request_id } " ,
7267 function_name = "check_export_progress" ,
7368 service = "Qualtrics" ,
74- headers = HEADERS ,
69+ headers = qualtrics_client . headers ,
7570 raise_for_status = True ,
7671 )
7772
@@ -91,10 +86,11 @@ def check_export_progress(request_id: str, survey_id: str, returns: str) -> str|
9186 ) from exc
9287
9388
94- def download_export (file_id : str , survey_id : str ) -> pd .DataFrame :
89+ def download_export (qualtrics_client , file_id : str , survey_id : str ) -> pd .DataFrame :
9590 """Download the exported survey data and convert to DataFrame.
9691
9792 Args:
93+ qualtrics_client: The Qualtrics API client.
9894 file_id (str): The ID of the file to download.
9995 survey_id (str): The ID of the survey associated with the file.
10096 Raises:
@@ -106,10 +102,10 @@ def download_export(file_id: str, survey_id: str) -> pd.DataFrame:
106102 try :
107103 download = logged_request (
108104 "GET" ,
109- f"{ QUALTRICS_BASE_URL } /surveys/{ survey_id } /export-responses/{ file_id } /file" ,
105+ f"{ qualtrics_client . api_url } /surveys/{ survey_id } /export-responses/{ file_id } /file" ,
110106 function_name = "download_export" ,
111107 service = "Qualtrics" ,
112- headers = HEADERS ,
108+ headers = qualtrics_client . headers ,
113109 stream = True ,
114110 raise_for_status = True ,
115111 )
@@ -155,28 +151,29 @@ def check_inputs_validity(participant_study_id: str, embedded_data_field: str, s
155151 return True
156152
157153
158- def get_responses_as_df (survey_id : str , incomplete_bool : bool ) -> pd .DataFrame :
154+ def get_responses_as_df (qualtrics_client , survey_id : str , incomplete_bool : bool ) -> pd .DataFrame :
159155 """Wrapper function to request export, wait for it, download it, and convert to DataFrame.
160156
161157 Args:
158+ qualtrics_client: The Qualtrics API client.
162159 survey_id (str): The ID of the survey to get responses from.
163160 incomplete_bool (bool): Whether to get responses that are in progress or completed.
164161
165162 Returns:
166163 pd.DataFrame: The survey response data as a DataFrame.
167164 """
168165 # Request a data export
169- request_id = create_data_export (survey_id , incomplete_bool = incomplete_bool )
166+ request_id = create_data_export (qualtrics_client , survey_id , incomplete_bool = incomplete_bool )
170167
171168 # Ping the export report repeatedly until it's ready
172169 export_progress = 0.0
173170 while export_progress < 100 :
174- export_progress = check_export_progress (request_id , survey_id , returns = "percent_complete" )
171+ export_progress = check_export_progress (qualtrics_client , request_id , survey_id , returns = "percent_complete" )
175172 time .sleep (2 )
176173
177174 # When export is complete, get the file ID
178- file_id = check_export_progress (request_id , survey_id , returns = "fileID" )
179- df = download_export (file_id , survey_id )
175+ file_id = check_export_progress (qualtrics_client , request_id , survey_id , returns = "fileID" )
176+ df = download_export (qualtrics_client , file_id , survey_id )
180177 return df
181178
182179
@@ -206,16 +203,16 @@ def subject_id_to_study_identifier(ldot_client, ldot_study_id: str, id_deelnemer
206203 return subject_id_to_study_identifier_dict
207204
208205
209- def get_individual_progress (ldot_client , ldot_study_id : str , id_deelnemer_entity : str , id_location : str , subject_ids : list , embedded_data_field : str , survey_id : str ) -> float :
206+ def get_individual_progress (ldot_client , qualtrics_client , ldot_study_id : str , id_deelnemer_entity : str , id_location : str , subject_ids : list , embedded_data_field : str , survey_id : str ) -> float :
210207 """Wrapper function that gets the individual progress of a participant in a survey."""
211208 # Check for invalid inputs
212209 check_inputs_validity (subject_ids [0 ], embedded_data_field , survey_id ) # TODO: Check this
213210
214211 participant_to_progress_dict = {}
215212 subject_id_to_study_identifier_dict = subject_id_to_study_identifier (ldot_client , ldot_study_id , id_deelnemer_entity , id_location , subject_ids )
216213
217- completed_responses_df = get_responses_as_df (survey_id , incomplete_bool = False )
218- incompleted_responses_df = get_responses_as_df (survey_id , incomplete_bool = True )
214+ completed_responses_df = get_responses_as_df (qualtrics_client , survey_id , incomplete_bool = False )
215+ incompleted_responses_df = get_responses_as_df (qualtrics_client , survey_id , incomplete_bool = True )
219216 df = pd .concat ([completed_responses_df , incompleted_responses_df ], ignore_index = True )
220217
221218 if not embedded_data_field in df .columns :
@@ -233,20 +230,21 @@ def get_individual_progress(ldot_client, ldot_study_id: str, id_deelnemer_entity
233230 return participant_to_progress_dict
234231
235232if __name__ == "__main__" :
236- with open ("ldot_config.json" ) as f :
237- config = json .load (f )
238- LDOT_TOKEN_URL = config ["LDOT_TOKEN_URL" ]
239- LDOT_API_URL = config ["LDOT_API_URL" ]
240- CLIENT_ID = config ["client_id" ]
241- CLIENT_SECRET = config ["client_secret" ]
242-
243- ldot_client = LdotClient (
244- token_url = LDOT_TOKEN_URL ,
245- api_url = LDOT_API_URL ,
246- client_id = CLIENT_ID ,
247- client_secret = CLIENT_SECRET
248- )
249- print (ldot_client .headers )
233+ pass
234+ # with open("ldot_config.json") as f:
235+ # config = json.load(f)
236+ # LDOT_TOKEN_URL = config["LDOT_TOKEN_URL"]
237+ # LDOT_API_URL = config["LDOT_API_URL"]
238+ # CLIENT_ID = config["client_id"]
239+ # CLIENT_SECRET = config["client_secret"]
240+
241+ # ldot_client = LdotClient(
242+ # token_url=LDOT_TOKEN_URL,
243+ # api_url=LDOT_API_URL,
244+ # client_id=CLIENT_ID,
245+ # client_secret=CLIENT_SECRET
246+ # )
247+ # print(ldot_client.headers)
250248
251249 # # # Example usage
252250 # survey_id="SV_efCMOg6wHU0T8ii"
0 commit comments