-
-
Save lvonlanthen/a11eb62838af3a294fc54e74d380a1ff to your computer and use it in GitHub Desktop.
Example code to collect all Questionnaires using the QCAT API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
API_URL = 'https://qcat.wocat.net/en/api/v2/questionnaires/' | |
API_TOKEN = '' # Your API token needed here | |
# Request headers, including authorization and content type of response | |
headers = { | |
'Authorization': 'Token {}'.format(API_TOKEN), | |
'Accept': 'application/json' # or application/xml or text/csv | |
} | |
def get_questionnaires(url): | |
""" | |
Query the url and return a list of questionnaire dicts. If there is a next | |
url available, query further questionnaires. | |
Args: | |
url: The (initial) URL to query. | |
Return: | |
list. A list of questionnaires (as dict). | |
""" | |
print("Querying {}".format(url)) | |
r = requests.get(url, headers=headers) | |
# Raise exception if request was not successful | |
r.raise_for_status() | |
r_json = r.json() | |
results = r_json.get('results', []) | |
# If there are more questionnaires, query the next page | |
next_url = r_json.get('next') | |
if next_url: | |
results.extend(get_questionnaires(next_url)) | |
return results | |
# Get all questionnaires from the API | |
questionnaires = get_questionnaires(API_URL) | |
# Do something meaningful with the questionnaires | |
print("{} questionnaires collected".format(len(questionnaires))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment