Skip to content

Instantly share code, notes, and snippets.

@natematias
Created July 28, 2014 17:49
Show Gist options
  • Save natematias/979841e8490d21ee9e14 to your computer and use it in GitHub Desktop.
Save natematias/979841e8490d21ee9e14 to your computer and use it in GitHub Desktop.
@natematias Popit Python Llibrary
#don't use this.
# Use https://github.com/mysociety/popit-python instead.
# But I didn't have the heart not to put it online
# after several hours of creating this, before realizing
# that a library already existed, and that
# it just hadn't appeared in my search results and
# the library wasn't listed in mySociety API documentation
import requests, json, yaml
class Popit:
def __init__(self):
self.conf = yaml.load(open('config.yaml'))
# get an entry from the Popit database
def get(self,section,id):
self.validate_section(section)
url = "{0}/{1}/{2}".format(self.conf['baseurl'], section, id)
request = requests.get(url)
result = request.json()
if 'errors' in result:
return []
return result['result']
# return the top popit result for a query
# raises HTTPError or JSONException on error
def search(self,section,query):
self.validate_section(section)
url = "{0}/search/{1}?q={2}".format(self.conf['baseurl'], section, query)
request = requests.get(url)
result = request.json()
if len(result[u'result']) > 0:
return result[u'result'][0]
return None
# create a popit entry
# raises HTTPError or JSONException on error
# raises IndexError if no ID is included
# the data can be any keys that are relevant
def create(self,section,data):
self.validate_section(section)
url = "{0}/{1}".format(self.conf['baseurl'], section, id)
request = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'}, auth=(self.conf['username'],self.conf['password']))
result = request.json()
return result
# update a popit entry
# raises HTTPError or JSONException on error
# raises IndexError if no ID is included
# the data should be the FULL DOCUMENT
# or it will overwrite fields
def update(self,section,data):
self.validate_section(section)
id = None
if "id" not in data.keys():
raise IndexError("no ID included. Cannot update record")
else:
id = data[u"id"]
url = "{0}/{1}/{2}".format(self.conf['baseurl'], section, id)
request = requests.put(url, data=json.dumps(data), headers={'Content-Type': 'application/json'},auth=(self.conf['username'],self.conf['password']))
result = request.json()
return result
# delete a popit entry
def delete(self, section, id):
self.validate_section(section)
url = "{0}/{1}/{2}".format(self.conf['baseurl'], section, id)
request = requests.delete(url, headers={'Content-Type': 'application/json'},auth=(self.conf['username'],self.conf['password']))
import pdb;pdb.stack_trace()
return request.json()
#search for an organization from Popit
def search_organization(self,query):
return self.search("organizations",query)
# get an organization from Popit
def get_organization(self,id):
return self.get("organizations",id)
# get an organization from Popit
def delete_organization(self,id):
return self.delete("organizations",id)
# get an organization from Popit
def update_organization(self,data):
return self.update("organizations", data)
#search for a person from Popit
def search_persons(self,query):
return self.search("persons",query)
# get a person from Popit
def get_person(self,id):
return self.get("persons",id)
# get a person from Popit
def delete_person(self,id):
return self.delete("persons",id)
# update a person from Popit
def update_person(self,data):
return self.update("persons",data)
#search for a membership from Popit
def search_memberships(self, query):
return self.search("memberships",query)
# get a membership from Popit
def get_membership(self,id):
return self.get("memberships",id)
# delete a membership from Popit
def delete_membership(self,id):
return self.delete("memberships",id)
# update a membership from Popit
def update_membership(self,data):
return self.update("memberships",data)
#validate a section
def validate_section(self,section):
if section not in ['organizations','persons','memberships']:
raise IndexError("Invalid Section: valid options are 'organizations', 'persons', 'memberships'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment