Skip to content

Instantly share code, notes, and snippets.

@kmclaugh
Created January 27, 2019 18:29
Show Gist options
  • Save kmclaugh/ef6478d926dc016ada95219c2240d86f to your computer and use it in GitHub Desktop.
Save kmclaugh/ef6478d926dc016ada95219c2240d86f to your computer and use it in GitHub Desktop.
Use nationbuilder API and rauth to create, a survey, add a survey response, and add a contact when someone completes the survey
from rauth import OAuth2Service
nation_slug = "aura"
REDIRECT_URI = "aura_redirect"
## Authenticate using OAuth
access_token_url = "http://" + nation_slug + ".nationbuilder.com/oauth/token"
authorize_url = nation_slug + ".nationbuilder.com/oauth/authorize"
service = OAuth2Service(
client_id = "aaaaaaaa",
client_secret = "bbbbbbb",
name = "any name",
authorize_url = authorize_url,
access_token_url = access_token_url,
base_url = nation_slug + ".nationbuilder.com")
token = service.get_access_token(decoder=json.loads, data = {"code": code,
"redirect_uri": REDIRECT_URI, "grant_type": "authorization_code"})
session = service.get_session(token)
def endpoint(endpoint):
"""
Helper for endpoints
"""
return 'https://' + nation_slug + ".nationbuilder.com/api/v1/" + endpoint
## Create the survey
survey = {
"survey": {
"slug": "survey",
"name": "Survey",
"tags": ["funny"],
"status":"published",
"questions": [{
"prompt": "What issue is more important to you?",
"external_id": null,
"slug": "important_issue",
"type": "multiple",
"status": "published",
"choices": [{
"name": "Daily mail service to all homes. Yeah right....",
"feedback": "really now...?"
}, {
"name": "Opposition to any change in our naturalization laws",
"feedback": null
}, {
"name": "foobar",
"feedback": "foobar"
}, {
"name": "Kansas should be admitted as a state",
"feedback": null
}]
}]
}
}
response = session.post(endpoint("sites/aura/pages/surveys"),
params={'format': 'json'},
json=survey)
assert 201 == response.status_code, "Could not create survey!"
survey_id = repr(response.json().get('survey').get('id'))
question_id = repr(response.json().get('survey').get("questions")[0].get('id'))
## When someone answers the survey, log it as a contact
## NOTE, I'm assuming that the person has filled out a form or something and that this API is first responsible for posting the survey response, then logging the contact. IE I'm not creating a webhook.
person_id = 19
survey_response = {
"survey_response": {
"survey_id": survey_id,
"person_id": person_id,
"question_responses": [{
"question_id": question_id,
"response": 3 ## assuming this is the correct id for the multiple choice response
}
}
}
response = session.put(endpoint('survey_responses'), params={'format': 'json'}, json=survey_response)
assert 200 == response.status_code, "Could not add survey response!"
survey_contact = {
"contact": {
"status": "answered",
"method": "other",
"type_id": 2, ## assume this is the type for survey responses
"note": "Responded to survey {}".format(survey_id)
}
}
response = session.put(endpoint("people/{}/contacts".format(person_id)), params={'format': 'json'}, json=survey_contact)
assert 200 == response.status_code, "Could not add contact!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment