def get_an_access_token(): # read configuration file for client id and client secret and other parameters config = DictFile('casi.config').read() # make an instance of the api, using the settings of the configuration file api = CastorApi(config) # try to get an access-token api.sessions.get_access_token(verbose=False) #show the result print('the access-token is: %s' % api.access_token)
def get_forms(): # read the configuration in a dict configuration = DictFile('odk.conFig').read(verbose=False) # use the configuration to set up a connector to the odk-api odkapi = OdkApi(configuration) forms_response = odkapi.forms.list(verbose=True) if forms_response.status_code != 200: print('something went wrong when requesting the list of forms.') else: all_forms = xmltodict.parse(forms_response.text) print(json.dumps(all_forms, indent=2)) t = odkapi.forms.xml('KoCoFilterpapier-2', True) print(t)
def list_records(): # read configuration file for client id and client secret and other parameters config=DictFile('casi.config').read() # make an instance of the api api = CastorApi(config) # try to get an access-token api.sessions.get_access_token(verbose=False) #request the studies response = api.records.list(study_id='86A2719F-AA11-414F-BDF3-5A54FBE60F0F', verbose=True) # display the records in the response for one_record in response['records']: print(one_record)
def list_studies(): # read configuration file for client id and client secret and other parameters config = DictFile('casi.config').read() # make an instance of the api api = CastorApi(config) # try to get an access-token api.sessions.get_access_token(verbose=False) #request the studies #all_studies = api.studies.list(verbose=False, complete_output=False) all_studies = api.studies.list(verbose=True, complete_output=False) # display name and id for one_study in all_studies: print(one_study['name'], '\t', one_study['study_id'])
def cycle_through_syncs(): # read configuration file for client id and client secret and other parameters config=DictFile('casi.config').read() # start a report #my_report = Reporter() # start with requesting an access token, which we can use for about an hour api = CastorApi(config) access_token_request = api.sessions.get_access_token(verbose=True) print(access_token_request) print(api.access_token) user_list = api.users.list(verbose=True) print(user_list) '''
def list_study(): # manually supply the study-id study_id = '2A72D9CC-06B5-0078-B089-A5456C7A7024' # read configuration file for client id and client secret and other parameters config = DictFile('casi.config').read() # make an instance of the api api = CastorApi(config) # try to get an access-token api.sessions.get_access_token(verbose=False) #request the studies my_study = api.study.list(study_id, verbose=True) print(my_study) # display name if we have that if 'name' in my_study: print(my_study['name']) else: print('could not find a name for %s' % study_id)
def write_all_studies_to_file(): # read configuration file for client id and client secret and other parameters config = DictFile('casi.config').read() # make an instance of the api api = CastorApi(config) # try to get an access-token api.sessions.get_access_token(verbose=False) #request data about the studies all_studies = api.studies.list(verbose=False, complete_output=False) # define the columns for our output: these must match the names in the dict columns = [ 'name', 'study_id', 'version', 'crf_id', 'surveys_enabled', '_links', 'randomization_enabled', 'domain', 'created_on', 'expected_centers', 'created_by', 'expected_records', 'main_contact', 'gcp_enabled', 'premium_support_enabled', 'slug', 'live', 'duration' ] filename_with_path = "../output/studies.csv" with open(filename_with_path, 'w', newline='') as f: wr = csv.DictWriter(f, fieldnames=columns) wr.writeheader() wr.writerows(all_studies)
def list_records(): # start with reading the configuration file for this scriptlet with open('./list_records_multiple_studies.json') as script_conf_file: script_conf = json.load(script_conf_file) for api_conf in script_conf['configs']: print(api_conf['config_name']) # read configuration file for client id and client secret and other parameters config = DictFile(api_conf['config_name']).read() # make an instance of the api api = CastorApi(config) # try to get an access-token api.sessions.get_access_token(verbose=False) for study in api_conf['studies']: print(study['study_id']) #request the studies response = api.records.list(study_id=study['study_id'], verbose=False) # display the records in the response for one_record in response['records']: print(one_record)
def get_submissions_and_data(): # read the configuration in a dict configuration = DictFile('odk.conFig').read(verbose=False) # use the configuration to set up a connector to the odk-api odkapi = OdkApi(configuration) # now read all the forms we're interested in with open('../config/form_ids.json') as json_file: all_forms = json.load(json_file) # loop through the forms and collect the submissions for each for one_form in all_forms['forms']: # construct the file-name date_stamp = datetime.datetime.now().strftime("%Y%m%d") data_file_name = '../output_files/%s_%s.json' % (one_form['form_id'], date_stamp) # initiate the data-file data_file = FileWriter(data_file_name) # initiate all submission-data all_submission_data = [] # request the submission-ids complete_response = odkapi.submissions.list(one_form['form_id'], verbose=False) if complete_response.status_code != 200: print('for %s we did not get the expected 200, but %s.' % (one_form['form_id'], complete_response.status_code)) else: submissions_dict = xmltodict.parse(complete_response.text) # check if there are any submissions at all if not submissions_dict['idChunk']['idList'] is None: # we can have just one submission, or a list if type(submissions_dict['idChunk']['idList']['id']) is list: all_ids = submissions_dict['idChunk']['idList']['id'] for one_id in all_ids: data_response = odkapi.submissions.get_data( one_form['form_id'], one_form['group_name'], one_id, verbose=False) data_dict = xmltodict.parse((data_response.text)) data_dict_subset = data_dict['submission']['data'] all_submission_data.append(data_dict_subset) else: # we have only one submission in this form one_id = submissions_dict['idChunk']['idList']['id'] data_response = odkapi.submissions.get_data( one_form['form_id'], one_form['group_name'], one_id, verbose=False) data_dict = xmltodict.parse((data_response.text)) data_dict_subset = data_dict['submission']['data'] all_submission_data.append(data_dict_subset) # now write the collected data to the file if one_form['indent'] == 0: data_file.append_to_file(json.dumps(all_submission_data)) else: data_file.append_to_file( json.dumps(all_submission_data, indent=one_form['indent']))