Skip to content

Commit b954c0c

Browse files
committed
Complete methods for new congress api wrapper. Docs file needs updating.
1 parent 0b6da22 commit b954c0c

File tree

1 file changed

+193
-1
lines changed

1 file changed

+193
-1
lines changed

sunlight/services/congress.py

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,226 @@
1414

1515
API_ROOT = "http://congress.api.sunlightfoundation.com"
1616

17+
LEGISLATOR_ID_TYPES = (
18+
'bioguide',
19+
'thomas',
20+
'lis',
21+
'govtrack',
22+
'votesmart',
23+
'crp',
24+
'fec',
25+
)
26+
1727

1828
class Congress(sunlight.service.Service):
1929
"""
30+
Bindings to the `Congress API <http://sunlightlabs.github.io/congress/>`_.
31+
Keep in mind this is a thin wrapper around the API so the API documentation
32+
is the place to look for help on field names and examples.
2033
"""
2134

2235
def legislators(self, **kwargs):
36+
"""
37+
Search and filter for members of Congress.
38+
39+
For details see `Legislators API docs
40+
<http://sunlightlabs.github.io/congress/legislators.html>`
41+
"""
42+
return self.get('legislators', **kwargs)
43+
44+
def legislator(self, identifier, id_type=LEGISLATOR_ID_TYPES[0], **kwargs):
45+
"""
46+
Retrieve a member of Congress by a unique identifier. Defaults to
47+
bioguide. Choices are:
48+
49+
* bioguide
50+
* thomas
51+
* lis
52+
* govtrack
53+
* votesmart
54+
* crp
55+
* fec
56+
57+
For details see `Legislators API docs
58+
<http://sunlightlabs.github.io/congress/legislators.html>`
59+
"""
60+
if id_type not in LEGISLATOR_ID_TYPES:
61+
id_type = LEGISLATOR_ID_TYPES[0]
62+
id_arg = {}
63+
if id_type == 'fec':
64+
id_arg['fec_ids'] = identifier
65+
else:
66+
id_key = '{0}_id'.format(id_type)
67+
id_arg[id_key] = identifier
68+
69+
kwargs.update(id_arg)
70+
results = self.get('legislators', **kwargs)
71+
if len(results):
72+
return results[0]
73+
return None
74+
75+
76+
def all_legislators_in_office(self, **kwargs):
77+
"""
78+
Returns all legislators currently in office (non-paginated response).
79+
80+
For details see `Legislators API docs
81+
<http://sunlightlabs.github.io/congress/legislators.html>`
82+
"""
83+
kwargs.update({
84+
"per_page": "all"
85+
})
2386
return self.get('legislators', **kwargs)
2487

2588
def locate_legislators_by_lat_lon(self, lat, lon, **kwargs):
89+
"""
90+
Find members of Congress by a latitude and longitude.
91+
92+
For details see `Legislators API docs
93+
<http://sunlightlabs.github.io/congress/legislators.html#methods/legislators-locate>`
94+
"""
2695
kwargs.update({
2796
"latitude": lat,
2897
"longitude": lon
2998
})
3099
return self.get('legislators/locate', **kwargs)
31100

32101
def locate_legislators_by_zip(self, zipcode, **kwargs):
102+
"""
103+
Find members of Congress by zip code.
104+
105+
For details see `Legislators API docs
106+
<http://sunlightlabs.github.io/congress/legislators.html#methods/legislators-locate>`
107+
"""
33108
kwargs.update({
34109
"zip": zipcode
35110
})
36111
return self.get('legislators/locate', **kwargs)
37112

113+
def bills(self, **kwargs):
114+
"""
115+
Search and filter through bills in Congress.
116+
117+
For details see `Bills API docs
118+
<http://sunlightlabs.github.io/congress/bills.html>`
119+
"""
120+
return self.get('bills', **kwargs)
121+
122+
def bill(self, bill_id, **kwargs):
123+
"""
124+
Retrieve a bill by bill_id.
125+
126+
For details see `Bills API docs
127+
<http://sunlightlabs.github.io/congress/bills.html>`
128+
"""
129+
kwargs.update({
130+
"bill_id": bill_id
131+
})
132+
results = self.get('bills', **kwargs)
133+
if len(results):
134+
return results[0]
135+
return None
136+
137+
def search_bills(self, query, **kwargs):
138+
"""
139+
Search the full text of legislation, and other fields.
140+
141+
For details see `Bills API docs
142+
<http://sunlightlabs.github.io/congress/bills.html#methods/bills-search>`
143+
"""
144+
kwargs.update({
145+
"query": query
146+
})
147+
return self.get('bills/search', **kwargs)
148+
149+
def upcoming_bills(self, **kwargs):
150+
"""
151+
Search and filter through upcoming bills in the House and Senate.
152+
153+
This will return bills that have been scheduled by
154+
party leadership for upcoming House and Senate floor action.
155+
156+
For details see `Upcoming Bills API docs
157+
<http://sunlightlabs.github.io/congress/upcoming_bills.html>`
158+
"""
159+
return self.get('upcoming_bills', **kwargs)
160+
161+
def locate_districts_by_lat_lon(self, lat, lon, **kwargs):
162+
"""
163+
Find congressional districts by a latitude and longitude.
164+
165+
For details see `Districts API docs
166+
<http://sunlightlabs.github.io/congress/districts.html>`
167+
"""
168+
kwargs.update({
169+
"latitude": lat,
170+
"longitude": lon
171+
})
172+
return self.get('/districts/locate', **kwargs)
173+
174+
def locate_districts_by_zip(self, zipcode, **kwargs):
175+
"""
176+
Find congressional districts by a latitude and longitude.
177+
178+
For details see `Districts API docs
179+
<http://sunlightlabs.github.io/congress/districts.html>`
180+
"""
181+
kwargs.update({
182+
"zip": zipcode,
183+
})
184+
return self.get('/districts/locate', **kwargs)
185+
186+
def committees(self, **kwargs):
187+
"""
188+
Search and filter through committees in the House and Senate.
189+
190+
For details see `Bill API docs
191+
<http://sunlightlabs.github.io/congress/committees.html>`
192+
"""
193+
return self.get('committees', **kwargs)
194+
195+
def amendments(self, **kwargs):
196+
"""
197+
Search and filter through committees in Congress.
198+
199+
For details see `Amendments API docs
200+
<http://sunlightlabs.github.io/congress/committees.html>`
201+
"""
202+
return self.get('amendments', **kwargs)
203+
204+
def votes(self, **kwargs):
205+
"""
206+
Search and filter through votes in Congress.
207+
208+
For details see `Votes API docs
209+
<http://sunlightlabs.github.io/congress/votes.html>`
210+
"""
211+
return self.get('votes', **kwargs)
212+
213+
def floor_updates(self, **kwargs):
214+
"""
215+
Search and filter through floor updates in the House and Senate.
216+
217+
For details see `Floor Updates API docs
218+
<http://sunlightlabs.github.io/congress/floor_updates.html>`
219+
"""
220+
return self.get('floor_updates', **kwargs)
221+
222+
def hearings(self, **kwargs):
223+
"""
224+
Search and filter through committee hearings in the House and Senate.
225+
226+
For details see `Hearings API docs
227+
<http://sunlightlabs.github.io/congress/hearings.html>`
228+
"""
229+
return self.get('hearings', **kwargs)
230+
38231
# implementation methods
39232
def _get_url(self, endpoint, apikey, **kwargs):
40233
return "%s/%s?apikey=%s&%s" % (
41234
API_ROOT, endpoint, apikey,
42235
sunlight.service.safe_encode(kwargs)
43236
)
44237

45-
46238
def _decode_response(self, response):
47239
return json.loads(response)['results']

0 commit comments

Comments
 (0)