Skip to content

Commit 352eb9e

Browse files
committed
Fix congress _get_url to conform to new Service.get requirements (42782a8). May close #13.
1 parent cf17554 commit 352eb9e

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

sunlight/services/congress.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,27 @@
2626
'fec',
2727
)
2828

29+
2930
# Stolen from http://codereview.stackexchange.com/a/21035/28391
3031
def flatten_dict(d):
3132
def flat_items():
3233
for k, v in d.items():
3334
if isinstance(v, dict):
3435
for kk, vv in flatten_dict(v).items():
35-
yield '{0}.{1}'.format(k,kk), vv
36+
yield '{0}.{1}'.format(k, kk), vv
3637
else:
3738
yield k, v
3839

3940
return dict(flat_items())
4041

42+
4143
def preencode_values(d):
4244
for k, v in d.items():
4345
if isinstance(v, bool):
4446
d[k] = str(v).lower()
4547
return d
4648

49+
4750
class Congress(sunlight.service.Service):
4851
"""
4952
Bindings to the `Congress API <http://sunlightlabs.github.io/congress/>`_.
@@ -61,7 +64,7 @@ def legislators(self, **kwargs):
6164
For details see `Legislators API docs
6265
<http://sunlightlabs.github.io/congress/legislators.html>`
6366
"""
64-
return self.get('legislators', **kwargs)
67+
return self.get(['legislators'], **kwargs)
6568

6669
@pageable
6770
def legislator(self, identifier, id_type=LEGISLATOR_ID_TYPES[0], **kwargs):
@@ -90,7 +93,7 @@ def legislator(self, identifier, id_type=LEGISLATOR_ID_TYPES[0], **kwargs):
9093
id_arg[id_key] = identifier
9194

9295
kwargs.update(id_arg)
93-
results = self.get('legislators', **kwargs)
96+
results = self.get(['legislators'], **kwargs)
9497
if len(results):
9598
return EntityDict(results[0], results._meta)
9699
return None
@@ -105,7 +108,7 @@ def all_legislators_in_office(self, **kwargs):
105108
kwargs.update({
106109
"per_page": "all"
107110
})
108-
return self.get('legislators', **kwargs)
111+
return self.get(['legislators'], **kwargs)
109112

110113
@pageable
111114
def locate_legislators_by_lat_lon(self, lat, lon, **kwargs):
@@ -119,7 +122,7 @@ def locate_legislators_by_lat_lon(self, lat, lon, **kwargs):
119122
"latitude": lat,
120123
"longitude": lon
121124
})
122-
return self.get('legislators/locate', **kwargs)
125+
return self.get(['legislators/locate'], **kwargs)
123126

124127
@pageable
125128
def locate_legislators_by_zip(self, zipcode, **kwargs):
@@ -132,7 +135,7 @@ def locate_legislators_by_zip(self, zipcode, **kwargs):
132135
kwargs.update({
133136
"zip": zipcode
134137
})
135-
return self.get('legislators/locate', **kwargs)
138+
return self.get(['legislators/locate'], **kwargs)
136139

137140
@pageable
138141
def bills(self, **kwargs):
@@ -142,7 +145,7 @@ def bills(self, **kwargs):
142145
For details see `Bills API docs
143146
<http://sunlightlabs.github.io/congress/bills.html>`_
144147
"""
145-
return self.get('bills', **kwargs)
148+
return self.get(['bills'], **kwargs)
146149

147150
@pageable
148151
def bill(self, bill_id, **kwargs):
@@ -155,7 +158,7 @@ def bill(self, bill_id, **kwargs):
155158
kwargs.update({
156159
"bill_id": bill_id
157160
})
158-
results = self.get('bills', **kwargs)
161+
results = self.get(['bills'], **kwargs)
159162
if len(results):
160163
return EntityDict(results[0], results._meta)
161164
return None
@@ -171,7 +174,7 @@ def search_bills(self, query, **kwargs):
171174
kwargs.update({
172175
"query": query
173176
})
174-
return self.get('bills/search', **kwargs)
177+
return self.get(['bills/search'], **kwargs)
175178

176179
@pageable
177180
def upcoming_bills(self, **kwargs):
@@ -184,7 +187,7 @@ def upcoming_bills(self, **kwargs):
184187
For details see `Upcoming Bills API docs
185188
<http://sunlightlabs.github.io/congress/upcoming_bills.html>`_
186189
"""
187-
return self.get('upcoming_bills', **kwargs)
190+
return self.get(['upcoming_bills'], **kwargs)
188191

189192
@pageable
190193
def locate_districts_by_lat_lon(self, lat, lon, **kwargs):
@@ -198,7 +201,7 @@ def locate_districts_by_lat_lon(self, lat, lon, **kwargs):
198201
"latitude": lat,
199202
"longitude": lon
200203
})
201-
return self.get('/districts/locate', **kwargs)
204+
return self.get(['/districts/locate'], **kwargs)
202205

203206
@pageable
204207
def locate_districts_by_zip(self, zipcode, **kwargs):
@@ -211,7 +214,7 @@ def locate_districts_by_zip(self, zipcode, **kwargs):
211214
kwargs.update({
212215
"zip": zipcode,
213216
})
214-
return self.get('/districts/locate', **kwargs)
217+
return self.get(['/districts/locate'], **kwargs)
215218

216219
@pageable
217220
def committees(self, **kwargs):
@@ -221,7 +224,7 @@ def committees(self, **kwargs):
221224
For details see `Committees API docs
222225
<http://sunlightlabs.github.io/congress/committees.html>`_
223226
"""
224-
return self.get('committees', **kwargs)
227+
return self.get(['committees'], **kwargs)
225228

226229
@pageable
227230
def amendments(self, **kwargs):
@@ -231,7 +234,7 @@ def amendments(self, **kwargs):
231234
For details see `Amendments API docs
232235
<http://sunlightlabs.github.io/congress/amendments.html>`_
233236
"""
234-
return self.get('amendments', **kwargs)
237+
return self.get(['amendments'], **kwargs)
235238

236239
@pageable
237240
def votes(self, **kwargs):
@@ -241,7 +244,7 @@ def votes(self, **kwargs):
241244
For details see `Votes API docs
242245
<http://sunlightlabs.github.io/congress/votes.html>`_
243246
"""
244-
return self.get('votes', **kwargs)
247+
return self.get(['votes'], **kwargs)
245248

246249
@pageable
247250
def floor_updates(self, **kwargs):
@@ -251,7 +254,7 @@ def floor_updates(self, **kwargs):
251254
For details see `Floor Updates API docs
252255
<http://sunlightlabs.github.io/congress/floor_updates.html>`_
253256
"""
254-
return self.get('floor_updates', **kwargs)
257+
return self.get(['floor_updates'], **kwargs)
255258

256259
@pageable
257260
def hearings(self, **kwargs):
@@ -261,7 +264,7 @@ def hearings(self, **kwargs):
261264
For details see `Hearings API docs
262265
<http://sunlightlabs.github.io/congress/hearings.html>`_
263266
"""
264-
return self.get('hearings', **kwargs)
267+
return self.get(['hearings'], **kwargs)
265268

266269
@pageable
267270
def nominations(self, **kwargs):
@@ -271,18 +274,27 @@ def nominations(self, **kwargs):
271274
For details see `Nominations API docs
272275
<http://sunlightlabs.github.io/congress/nominations.html>`_
273276
"""
274-
return self.get('nominations', **kwargs)
277+
return self.get(['nominations'], **kwargs)
275278

276279
# implementation methods
277-
def _get_url(self, endpoint, apikey, **kwargs):
278-
url_args = preencode_values( flatten_dict(kwargs) )
279-
url = "%s/%s?apikey=%s&%s" % (
280-
API_ROOT, endpoint, apikey,
281-
sunlight.service.safe_encode(url_args)
282-
)
280+
def _get_url(self, pathparts, apikey, **kwargs):
281+
url_args = preencode_values(flatten_dict(kwargs))
282+
283+
# join pieces by slashes and add a trailing slash
284+
endpoint_path = "/".join(pathparts)
285+
286+
url = "{api_root}/{path}?apikey={apikey}&{url_args}".format(
287+
api_root=API_ROOT,
288+
path=endpoint_path,
289+
apikey=apikey,
290+
url_args=sunlight.service.safe_encode(url_args)
291+
).strip('&')
283292
return url
284293

285294
def _decode_response(self, response):
286295
data = json.loads(response)
287-
results = data.pop('results')
288-
return EntityList(results, data)
296+
results = data.pop('results', None)
297+
if results:
298+
return EntityList(results, data)
299+
else:
300+
return data

0 commit comments

Comments
 (0)