Skip to content

Commit 7b26179

Browse files
committed
Add OpenCivic service
1 parent 1d6fc7a commit 7b26179

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

ChangeLog

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
2013-11-22 Paul Tagliamonte <[email protected]>
2+
3+
* sunlight/services/opencivic.py
4+
- New service for OpenCivic endpoints. Currently hardcoded to talk with
5+
opencivicdata.org.
6+
17
2013-11-20 Thom Neale <[email protected]>
28

39
* sunlight/debugcache.py:
4-
- Added a method decorator for caching API responses, base classes for adding more backends if need arises
10+
- Added a method decorator for caching API responses, base classes for
11+
adding more backends if need arises
512
* sunlight/__init__.py:
613
- Imports the cache instance and aliases it
714
* sunlight/services.py:

sunlight/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
import sunlight.services.openstates
1717
import sunlight.services.capitolwords
1818
import sunlight.services.congress
19+
import sunlight.services.opencivic
1920
import sunlight.services.congress_deprecated
2021

2122
openstates = sunlight.services.openstates.Openstates()
2223
capitolwords = sunlight.services.capitolwords.CapitolWords()
24+
opencivic = sunlight.services.opencivic.OpenCivic()
2325

2426
congress = sunlight.services.congress.Congress()
2527
congress_deprecated = sunlight.services.congress_deprecated.Congress()
@@ -67,4 +69,4 @@ def _attempt_to_load_apikey():
6769
_attempt_to_load_apikey()
6870

6971

70-
cache = sunlight.debugcache.debug_cache
72+
cache = sunlight.debugcache.debug_cache

sunlight/services/opencivic.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (c) Sunlight Labs, 2012 under the terms and conditions
2+
# of the LICENSE file.
3+
4+
import sunlight.service
5+
from sunlight.errors import BadRequestException
6+
from sunlight.service import EntityList, EntityDict
7+
import json
8+
9+
module_name = "opencivic"
10+
service_url = "http://api.opencivicdata.org"
11+
12+
13+
class OpenCivic(sunlight.service.Service):
14+
15+
def get_list(self, *args, **kwargs):
16+
17+
def _q(page, *args, **kwargs):
18+
ret = self.get(*args, page=page, **kwargs)
19+
page = ret['meta']['page']
20+
total_pages = ret['meta']['max_page']
21+
return ret, (page < total_pages)
22+
23+
page = 0
24+
if 'page' in kwargs:
25+
page = kwargs.pop('page')
26+
27+
has_more = True
28+
while has_more:
29+
ret, has_more = _q(page, *args, **kwargs)
30+
page = ret['meta']['page']
31+
for entry in ret['results']:
32+
yield EntityDict(data=entry, meta=ret['meta'])
33+
34+
def get_object(self, *args, **kwargs):
35+
ret = self.get(*args, **kwargs) # In case we get meta eventually.
36+
return EntityDict(data=ret)
37+
38+
def jurisdictions(self, **kwargs):
39+
return self.get_list(["jurisdictions"], **kwargs)
40+
41+
def divisions(self, **kwargs):
42+
return self.get_list(["divisions"], **kwargs)
43+
44+
def organizations(self, **kwargs):
45+
return self.get_list(["organizations"], **kwargs)
46+
47+
def people(self, **kwargs):
48+
return self.get_list(["people"], **kwargs)
49+
50+
def bills(self, **kwargs):
51+
return self.get_list(["bills"], **kwargs)
52+
53+
def votes(self, **kwargs):
54+
return self.get_list(["votes"], **kwargs)
55+
56+
def events(self, **kwargs):
57+
return self.get_list(["events"], **kwargs)
58+
59+
def info(self, ocd_id, **kwargs):
60+
return self.get_object([ocd_id], **kwargs)
61+
62+
def _get_url(self, objs, apikey, **kwargs):
63+
# Gate for any None's in the query. This is usually a problem.
64+
if None in objs:
65+
raise BadRequestException("`None' passed to the URL encoder (%s)" %
66+
(str(objs)))
67+
68+
# join pieces by slashes and add a trailing slash
69+
object_path = "/".join(objs)
70+
object_path += "/"
71+
72+
return "%s/%s?apikey=%s&%s" % (
73+
service_url,
74+
object_path,
75+
apikey,
76+
sunlight.service.safe_encode(kwargs)
77+
)
78+
79+
def _decode_response(self, response):
80+
return json.loads(response)

0 commit comments

Comments
 (0)