Skip to content

Instantly share code, notes, and snippets.

@speg

speg/nbapi.py Secret

Created November 8, 2014 00:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save speg/29906676dfcad63732f3 to your computer and use it in GitHub Desktop.
Save speg/29906676dfcad63732f3 to your computer and use it in GitHub Desktop.
NationBuilder Person API
# This python script requires the requests HTTP library
# See http://docs.python-requests.org for more information.
import os
try:
import requests
except ImportError:
raise SystemExit("Missing requests library - http://docs.python-requests.org")
API_KEY = os.environ.get('API_KEY')
NATION = os.environ.get('NATION')
params = {"access_token": API_KEY}
if API_KEY is None:
raise SystemExit("Missing API_KEY environment variable. Please set your NationBuilder API key.")
if NATION is None:
raise SystemExit("Missing NATION environment variable. Please set your NationBuilder slug.")
# A simple helper to build the uri of the desired endpoint
def endpoint(endpoint):
return 'https://' + NATION + ".nationbuilder.com/api/v1/" + endpoint
person = {'person':{
'first_name': 'Bozo',
'last_name': 'Clown',
'email': 'bozo@clown.com'
}}
response = requests.post(endpoint('people'), params=params, json=person)
assert 201 == response.status_code, "Could not create person!"
id = repr(response.json().get('person').get('id'))
#update the person with a birthday
birthday = {'person': {'birthdate': '1984/02/23'}}
response = requests.put(endpoint('people/'+id), params=params, json=birthday)
assert 200 == response.status_code, "Could not update person!"
#remove person
response = requests.delete(endpoint('people/'+id), params=params)
assert 204 == response.status_code, "Could not remove person!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment