This module provides the opencivicdata.package
module, a set of bindings
to work with the
Open Civic Data
API.
Most users will want to use the pre-configured Sunlight OCD API client, which reads your Sunlight API key from the same location as python-sunlight does.
Just set either the SUNLIGHT_API_KEY
envvar, or write out your key to
~/.sunlight.key
.
from opencivicdata.api import SunlightOCDAPI
api = SunlightOCDAPI()
for person in api.people_by_lat_lon(42.35, -71.06):
print(person)
There are 3 built-in clients to the Open Civic Data API bindings, one without
any built-in configuration (the opencivicdata.api.OCDAPI
class) as a general
use client, one for the Open Civic Data Vagrant deploy client
(the opencivicdata.api.client.VagrantOCDAPI
class), and one for the Sunlight
Open Civic Data API (the opencivicdata.api.SunlightOCDAPI
class)
This client takes two arguments, host
and apikey
. If the API doesn't
require an API Key, feel free to not pass in apikey
.
# To test the API server running locally
from opencivicdata.api import OCDAPI
api = OCDAPI(host="http://localhost:8000")
This client takes only one optional argument, apikey
, with a hardcoded host
pointing to http://10.42.2.102
, the Vagrant API host as seen in the
vagrant-opencivicdata.org
config.
This client takes a single optional argument, apikey
, with a hardcoded host
pointing to https://api.opencivicdata.org
.
Internally, this will use SUNLIGHT_API_KEY
, if set, as the API Key. If no
such environment variable is set, it will use the content of
~/.sunlight.key
as the API key.
If an API key is passed in to apikey
, this will override all of the above.
Register for a Sunlight API Key,
and put the API key in a file at ~/.sunlight.key
, or export it into the env
as SUNLIGHT_API_KEY
.
from opencivicdata.api import SunlightOCDAPI
api = SunlightOCDAPI()
for person in api.people():
print(person['name'])
from opencivicdata.api import SunlightOCDAPI
api = SunlightOCDAPI()
people = api.people()
while people.has_next():
for person in people:
print(person['name'])
people = people.next()
from opencivicdata.api import SunlightOCDAPI
api = SunlightOCDAPI()
for person in api.people_by_lat_lon(42.35, -71.06):
print(person)