Skip to content

Commit

Permalink
1.2.0 - Account for API rate limits - closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Whalen committed May 15, 2018
1 parent 844be62 commit 01b19cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 46 deletions.
31 changes: 16 additions & 15 deletions pyopenstates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sys import version_info
from datetime import datetime
from requests import Session
from time import sleep

"""Copyright 2016 Sean Whalen
Expand All @@ -22,7 +23,7 @@
See the License for the specific language governing permissions and
limitations under the License."""

__version__ = "1.1.1"
__version__ = "1.2.0"

API_ROOT = "https://openstates.org/api/v1/"
DEFAULT_USER_AGENT = "pyopenstates/{0}".format(__version__)
Expand Down Expand Up @@ -200,13 +201,13 @@ def search_bills(**kwargs):
- ``search_window``- By default all bills are searched, but if a time
window is desired the following options can be passed to
``search_window``:
- ``search_window=all`` - Default, include all sessions.
- ``search_window=term`` - Only bills from sessions within the current
term.
- ``search_window=session`` - Only bills from the current session.
- ``search_window=session:2009`` - Only bills from the session named
- ``search_window="all"`` - Default, include all sessions.
- ``search_window="term"`` - Only bills from sessions within the
current term.
- ``search_window="session"`` - Only bills from the current session.
- ``search_window="session:2009"`` - Only bills from the session named
``2009``.
- ``search_window=term:2009-2011`` - Only bills from the sessions in
- ``search_window="term:2009-2011"`` - Only bills from the sessions in
the ``2009-2011`` session.
- ``updated_since`` - Only bills updated since a provided date (provided in
``YYYY-MM-DD`` format)
Expand Down Expand Up @@ -240,16 +241,16 @@ def search_bills(**kwargs):
return.
"""
uri = "bills/"
if "per_page" in kwargs.keys():
results = []
if len(kwargs ) > 0:
kwargs["per_page"] = 500
kwargs["page"] = 1
results = []
new_results = _get(uri, params=kwargs)
while len(new_results) > 0:
results += new_results
kwargs["page"] += 1
sleep(1)
new_results = _get(uri, params=kwargs)
while len(new_results) > 0:
results += new_results
kwargs["page"] += 1
new_results = _get(uri, params=kwargs)
else:
results = _get(uri, params=kwargs)

return results

Expand Down
37 changes: 6 additions & 31 deletions test_pyopenstates.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@


class Test(unittest.TestCase):
"""A test suite for openstatesclient"""
"""A test suite for pyopenstates"""

def setUp(self):
pyopenstates.set_user_agent("test-suite")

def tearDown(self):
# Wait between tests to avoid hitting the API limit
sleep(1)
sleep(0.5)

def testOpenStatesMetadata(self):
"""Calling the metadata method without specifying a state returns a
list of 52 dictionaries: One for each state, plus DC and Puerto Rico"""
metadata = pyopenstates.get_metadata()
self.assertEquals(len(metadata), 52)
self.assertEqual(len(metadata), 52)
for obj in metadata:
self.assertEquals(type(obj), dict)
self.assertEqual(type(obj), dict)

def testStateMetadata(self):
"""All default state metadata fields are returned"""
Expand Down Expand Up @@ -143,17 +143,11 @@ def testBillDetailInputs(self):
bill_id)
self.assertRaises(ValueError, pyopenstates.get_bill, _id, state)

def testPaginatedResults(self):
"""Paginated results"""
per_page = 200
results = pyopenstates.search_bills(state="dc", per_page=per_page)
self.assertEqual(len(results), per_page)

def testBillSearchSort(self):
"""Sorting bill search results"""
sorted_bills = pyopenstates.search_bills(state="dc",
sort="created_at",
per_page=100)
search_window="term",
sort="created_at")
self.assertGreater(sorted_bills[0]["created_at"],
sorted_bills[-1]["created_at"])

Expand Down Expand Up @@ -203,25 +197,6 @@ def testCommitteeDetails(self):
self.assertEqual(pyopenstates.get_committee(_id)["committee"],
comittee)

@unittest.skip("Events are missing for every state in the dataset as of "
"2017-01-27")
def testEventSearch(self):
"""Event search"""
state = "tx"
results = pyopenstates.search_events(state=state)
self.assertGreater(len(results), 2)
for event in results:
self.assertEqual(event["state"], state.lower())

@unittest.skip("Events are missing for every state in the dataset as of "
"2017-12-27")
def testEventDetails(self):
"""Event details"""
_id = "TXE00026474"
description = "TSpecial Purpose Districts"
self.assertEqual(pyopenstates.get_event(_id)['description'],
description)

def testDistrictSearch(self):
"""District search"""
state = "nc"
Expand Down

0 comments on commit 01b19cf

Please sign in to comment.