Skip to content

Instantly share code, notes, and snippets.

@lvonlanthen
Created June 28, 2017 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvonlanthen/a11eb62838af3a294fc54e74d380a1ff to your computer and use it in GitHub Desktop.
Save lvonlanthen/a11eb62838af3a294fc54e74d380a1ff to your computer and use it in GitHub Desktop.
Example code to collect all Questionnaires using the QCAT API
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