Skip to content

Commit 7859094

Browse files
committed
Initial implementation of including metadata in API results via custom classes in service.py. See congress.py for example implementation. This is a possible solution to the problem described in issue #6.
1 parent ada255d commit 7859094

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

sunlight/service.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
from urllib.request import urlopen
1919
from urllib.error import HTTPError
2020
_str_type = str
21+
from collections import UserDict
22+
from collections import UserList
2123
else:
2224
from urllib import urlencode
2325
from urllib2 import urlopen
2426
from urllib2 import HTTPError
2527
_str_type = basestring
28+
from UserList import UserList
29+
from UserDict import IterableUserDict as UserDict
2630

2731

2832
def safe_encode(kwargs):
@@ -83,3 +87,25 @@ def get(self, top_level_object, **kwargs):
8387
ex.code = code
8488

8589
raise ex
90+
91+
92+
class EntityList(UserList):
93+
"""
94+
EntityList provides an iterable of API entities along with a _meta
95+
dictionary that contains information about the query results. This could
96+
include result count and pagination details.
97+
"""
98+
def __init__(self, data=[], meta=None):
99+
UserList.__init__(self, data)
100+
self._meta = meta
101+
102+
103+
class EntityDict(UserDict):
104+
"""
105+
EntityDict provides a dictionary representation of an API entity along
106+
with a _meta dictionary that contains information about the query results.
107+
This could include result count and pagination details.
108+
"""
109+
def __init__(self, data={}, meta=None):
110+
UserDict.__init__(self, data)
111+
self._meta = meta

sunlight/services/congress.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"""
1010

1111
import sunlight.service
12+
from sunlight.service import EntityList
13+
from sunlight.service import EntityDict
1214
import json
1315

1416

@@ -86,7 +88,7 @@ def legislator(self, identifier, id_type=LEGISLATOR_ID_TYPES[0], **kwargs):
8688
kwargs.update(id_arg)
8789
results = self.get('legislators', **kwargs)
8890
if len(results):
89-
return results[0]
91+
return EntityDict(results[0], results._meta)
9092
return None
9193

9294

@@ -148,7 +150,7 @@ def bill(self, bill_id, **kwargs):
148150
})
149151
results = self.get('bills', **kwargs)
150152
if len(results):
151-
return results[0]
153+
return EntityDict(results[0], results._meta)
152154
return None
153155

154156
def search_bills(self, query, **kwargs):
@@ -264,4 +266,6 @@ def _get_url(self, endpoint, apikey, **kwargs):
264266
return url
265267

266268
def _decode_response(self, response):
267-
return json.loads(response)['results']
269+
data = json.loads(response)
270+
results = data.pop('results')
271+
return EntityList(results, data)

0 commit comments

Comments
 (0)