Created
April 13, 2018 20:21
-
-
Save coolsoftwaretyler/bb85d188e72be9b5467171751c09d849 to your computer and use it in GitHub Desktop.
Tyler Williams NationBuilder API Certification
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
Tyler | |
Ozy | |
Sarah | |
Jake | |
Brad | |
Drew | |
Annika | |
John | |
Brian | |
Amanda |
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
Tyler Williams NationBuilder Developer Certification | |
Submitted 4/13/18 | |
This python program completes the following developer exercises: | |
Create an event | |
Edit an event | |
Create a person | |
Edit a person | |
Delete a person | |
No data validation or good error handling, so when you edit fields on the event and the person, you'll have to provide clean data to the program. | |
Comments throughout the code show what's going on. | |
Set up with a developer API key instead of the OAuth flow at the moment. |
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# Load libraries and modules | |
try: | |
import os | |
except ImportError: | |
raise SystemExit('Missing os module') | |
try: | |
import json | |
except ImportError: | |
raise SystemExit('Missing json module') | |
try: | |
import datetime | |
except ImportError: | |
raise SystemExit('Missing datetime module') | |
try: | |
import requests | |
except ImportError: | |
raise SystemExit('Missing requests library - http://docs.python-requests.org') | |
try: | |
from random import * | |
except ImportError: | |
raise SystemExit('Missing random module') | |
#API key for access goes here. | |
API_KEY = '581aac0b45b50a4f5a76b02956c62f856d4cf4b0a491f5448d841e80bed676e2' | |
#Nation Slug | |
NATION = 'tylerwilliams' | |
#Paramaters for some API calls. | |
params = {'access_token': API_KEY} | |
#Keep the program running | |
while True: | |
selection = raw_input("""Welcome to my NationBuilder Developer Test! What would you like to do? Type a number and press enter. | |
1. Create a sample event. One will be automatically created for you. | |
2. Edit an event | |
3. Create a sample person. One will be automatically creadted for you | |
4. Edit a person | |
5. Delete a person | |
6. Exit\n""") | |
if selection == '1': | |
#Create a sample event. | |
#Set the endpoint for creating an event. | |
endpoint = 'https://'+ NATION + '.nationbuilder.com/api/v1/sites/'+ NATION +'/pages/events?access_token=' + API_KEY | |
#To avoid duplicates, we set the start time as one day before now, and the end time as right now. | |
#Use now variable for current datetime | |
now = datetime.datetime.now().isoformat() | |
#yesterday_raw is the datetime without isoformat | |
yesterday_raw = datetime.datetime.now() - datetime.timedelta(days=1) | |
#Isoformat for yesterday variable. | |
yesterday = yesterday_raw.isoformat() | |
#Create a sample event. Most of the information is static, since this is just a demo. We use a dynamic date, though. | |
event = {'event': | |
{ | |
'status': 'unlisted', | |
'name': 'The Unobtainable Event', | |
'intro': '<h1>This event is always scheduled for one day before you make it. You will have to edit it to attend.</h1>', | |
'time_zone': "Mountain Time (US & Canada)", | |
'start_time': str(yesterday), | |
'end_time': str(now), | |
'contact': | |
{ | |
'name': "Tyler Williams", | |
'contact_phone': "2036953299", | |
'show_phone': "True", | |
'email': "tyler@ogdenstudios.xyz", | |
'show_email': "True", | |
}, | |
'rsvp_form': | |
{ | |
'phone': "required", | |
'address': "required", | |
'allow_guests': "True", | |
'accept_rsvps': "True", | |
'gather_volunteers': "True", | |
}, | |
'show_guests': "True", | |
'capacity': "90", | |
'venue': | |
{ | |
'name': "The Space", | |
'address': | |
{ | |
'address1': "123 Cherry Lane", | |
'city': "Denver", | |
'state': "CO" | |
} | |
}, | |
} | |
} | |
#Post the event | |
response = requests.post(endpoint, params=params, json=event) | |
#Check the response | |
print response | |
if selection == '2': | |
#Edit an existing event. | |
# Set the endpoint to be the nation's events. | |
endpoint = 'https://'+ NATION + '.nationbuilder.com/api/v1/sites/'+ NATION +'/pages/events?access_token=' + API_KEY | |
# Get the events in the nation. | |
response = requests.get(endpoint) | |
# Assign the JSON to events variable | |
events = response.json().items()[1][1] | |
# Use limit to find out how many events there are and later list them in a for loop. | |
limit = len(events) | |
# Print out the event titles | |
print 'Which event would you like to edit?' | |
for x in range(0, limit): | |
print str(x + 1) + '.' + ' ' + events[x]['title'] | |
event_selection = int(raw_input('Type a number and press enter ')) | |
# Set the target_event | |
target_event = events[event_selection - 1] | |
#Use dummy_values dictionary to store the existing event details | |
dummy_values = { | |
'status': str(target_event['status']), | |
'start_time': str(target_event['start_time']), | |
'intro': str(target_event['intro']), | |
'show_guests': str(target_event['show_guests']), | |
'capacity': str(target_event['capacity']), | |
'name': str(target_event['name']), | |
'venue_name': str(target_event['venue']['name']), | |
'venue_carrier_route': str(target_event['venue']['address' | |
]['carrier_route']), | |
'venue_street_name': str(target_event['venue']['address' | |
]['street_name']), | |
'venue_county': str(target_event['venue']['address']['county' | |
]), | |
'venue_country_code': str(target_event['venue']['address' | |
]['country_code']), | |
'venue_lng': str(target_event['venue']['address']['lng']), | |
'venue_zip5': str(target_event['venue']['address']['zip5']), | |
'venue_zip4': str(target_event['venue']['address']['zip4']), | |
'venue_city': str(target_event['venue']['address']['city']), | |
'venue_street_number': str(target_event['venue']['address' | |
]['street_number']), | |
'venue_street_suffix': str(target_event['venue']['address' | |
]['street_suffix']), | |
'venue_state': str(target_event['venue']['address']['state']), | |
'venue_lot': str(target_event['venue']['address']['lot']), | |
'venue_street_prefix': str(target_event['venue']['address' | |
]['street_prefix']), | |
'venue_unit_number': str(target_event['venue']['address' | |
]['unit_number']), | |
'venue_sort_sequence': str(target_event['venue']['address' | |
]['sort_sequence']), | |
'venue_address_1': str(target_event['venue']['address' | |
]['address1']), | |
'venue_address_2': str(target_event['venue']['address' | |
]['address2']), | |
'venue_address_3': str(target_event['venue']['address' | |
]['address3']), | |
'venue_street_type': str(target_event['venue']['address' | |
]['street_type']), | |
'venue_fips': str(target_event['venue']['address']['fips']), | |
'venue_lat': str(target_event['venue']['address']['lat']), | |
'venue_zip': str(target_event['venue']['address']['zip']), | |
'venue_delivery_point': str(target_event['venue']['address' | |
]['delivery_point']), | |
'time_zone': str(target_event['time_zone']), | |
'contact_show_email': str(target_event['contact']['show_email' | |
]), | |
'contact_show_phone': str(target_event['contact']['show_phone' | |
]), | |
'contact_phone': str(target_event['contact']['phone']), | |
'contact_name': str(target_event['contact']['name']), | |
'contact_email': str(target_event['contact']['email']), | |
'end_time': str(target_event['end_time']), | |
'rsvp_gather_volunteers': str(target_event['rsvp_form' | |
]['gather_volunteers']), | |
'rsvp_phone': str(target_event['rsvp_form']['phone']), | |
'rsvp_accept_rsvps': str(target_event['rsvp_form' | |
]['accept_rsvps']), | |
'rsvp_address': str(target_event['rsvp_form']['address']), | |
'rsvp_allow_guests': str(target_event['rsvp_form' | |
]['allow_guests']), | |
} | |
# Ask for which field they want to edit. | |
field_to_edit = raw_input("Which attribute do you want to edit? ") | |
# Get the new value | |
new_value = raw_input("What's the new value? ") | |
# Set the value in the dummy_values dictionariy | |
dummy_values[field_to_edit] = new_value | |
# Create the payload using dummy_values | |
event = {'event': { | |
'status': dummy_values['status'], | |
'start_time': dummy_values['start_time'], | |
'intro': dummy_values['intro'], | |
'show_guests': dummy_values['show_guests'], | |
'capacity': dummy_values['capacity'], | |
'name': dummy_values['name'], | |
'venue': {'name': dummy_values['venue_name'], 'address': { | |
'carrier_route': dummy_values['venue_carrier_route'], | |
'street_name': dummy_values['venue_street_name'], | |
'county': dummy_values['venue_county'], | |
'country_code': dummy_values['venue_country_code'], | |
'lng': dummy_values['venue_lng'], | |
'zip5': dummy_values['venue_zip5'], | |
'zip4': dummy_values['venue_zip4'], | |
'city': dummy_values['venue_city'], | |
'street_number': dummy_values['venue_street_number'], | |
'street_suffix': dummy_values['venue_street_suffix'], | |
'state': dummy_values['venue_state'], | |
'lot': dummy_values['venue_lot'], | |
'street_prefix': dummy_values['venue_street_prefix'], | |
'unit_number': dummy_values['venue_unit_number'], | |
'sort_sequence': dummy_values['venue_sort_sequence'], | |
'address1': dummy_values['venue_address_1'], | |
'address2': dummy_values['venue_address_2'], | |
'address3': dummy_values['venue_address_3'], | |
'street_type': dummy_values['venue_street_type'], | |
'fips': dummy_values['venue_fips'], | |
'lat': dummy_values['venue_lat'], | |
'zip': dummy_values['venue_zip4'], | |
'delivery_point': dummy_values['venue_delivery_point'], | |
}}, | |
'time_zone': dummy_values['time_zone'], | |
'contact': { | |
'show_email': dummy_values['contact_show_email'], | |
'show_phone': dummy_values['contact_show_phone'], | |
'contact_phone': dummy_values['contact_phone'], | |
'name': dummy_values['contact_name'], | |
'email': dummy_values['contact_email'], | |
}, | |
'end_time': dummy_values['end_time'], | |
'rsvp_form': { | |
'gather_volunteers': dummy_values['rsvp_gather_volunteers' | |
], | |
'phone': dummy_values['rsvp_phone'], | |
'accept_rsvps': dummy_values['rsvp_accept_rsvps'], | |
'address': dummy_values['rsvp_address'], | |
'allow_guests': dummy_values['rsvp_allow_guests'], | |
}, | |
}} | |
#Set the endpoint to the specific event to be updated. | |
endpoint = 'https://'+ NATION + '.nationbuilder.com/api/v1/sites/'+ NATION +'/pages/events/' + str(target_event['id']) +'?access_token=' + API_KEY | |
#Put the new information | |
response = requests.put(endpoint, params=params, json=event) | |
#Get the response | |
print response | |
if selection == '3': | |
#Create a person | |
#Set endpoint to the nation's people. | |
endpoint = 'https://'+ NATION + '.nationbuilder.com/api/v1/people?access_token=' + API_KEY | |
#Pull random list of first names. | |
li = [i.strip().split() for i in open("names.txt").readlines()] | |
#Random number | |
selector = randint(0,9) | |
#Create a new person with a random first name. | |
person = { | |
"person": | |
{ | |
"first_name": li[selector][0], | |
"last_name": "Test", | |
"phone": "null", | |
} | |
} | |
# Post the person | |
response = requests.post(endpoint, params=params, json=person) | |
# Get the response | |
print response | |
if selection == '4': | |
#Edit a person | |
endpoint = 'https://'+ NATION +'.nationbuilder.com/api/v1/people?__proto__&access_token=' + API_KEY | |
#Lists out the first 10 people - I've got a lot of test data so now rendering it all in the console. | |
people_list = requests.get(endpoint).json().items()[1][1] | |
limit = len(people_list) | |
for x in range(0, limit): | |
print str(x + 1) + '.' + ' ' + people_list[x]['first_name'] + ' ' + people_list[x]['last_name'] | |
# Set person to change | |
person_to_edit = raw_input("Who would you like to change? ") | |
# Get the id by referencing the name | |
person_to_edit_id = people_list[int(person_to_edit) - 1]['id'] | |
# Set the field to edit | |
field_to_edit = raw_input("What field would you like to edit? ") | |
# Set the new value | |
new_value = raw_input("What's the new value? ") | |
# Create the person using the new value | |
person = { | |
"person" : | |
{ | |
field_to_edit : new_value | |
} | |
} | |
# Put the new person | |
response = requests.put(endpoint, params=params, json=person) | |
# Print the response | |
print response | |
if selection == '5': | |
#Delete a person | |
#Set the endpoint to our people | |
endpoint = 'https://'+ NATION +'.nationbuilder.com/api/v1/people?__proto__&access_token=' + API_KEY | |
#Just returning 10 people - there's a few hundred in my test database right now, don't need to render them all. | |
people_list = requests.get(endpoint).json().items()[1][1] | |
limit = len(people_list) | |
for x in range(0, limit): | |
print str(x + 1) + '.' + ' ' + people_list[x]['first_name'] + ' ' + people_list[x]['last_name'] | |
# Set person to delete | |
person_to_edit = raw_input("Who would you like to delete? ") | |
# Get the id by the name | |
person_to_edit_id = people_list[int(person_to_edit) - 1]['id'] | |
#Set the endpoint to the person's profile | |
endpoint = 'https://'+ NATION + '.nationbuilder.com/api/v1/people/' + str(person_to_edit_id) +'?access_token=' + API_KEY | |
#Delete | |
response = requests.delete(endpoint) | |
# Print response | |
print response | |
if selection == '6': | |
#Exit the program | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment