diff --git a/meetup/api.py b/meetup/api.py index 65d7fe9..29c5612 100644 --- a/meetup/api.py +++ b/meetup/api.py @@ -177,12 +177,11 @@ def _call(self, service_name, parameters=None, **kwargs): self.rate_limit.limit, self.rate_limit.reset)) - if response.status_code == 401: - raise exceptions.HttpUnauthorized - if response.status_code == 404: - raise exceptions.HttpNotFoundError - if response.status_code == 410: - raise exceptions.HttpNotAccessibleError + # We handle 429 (Too Many Requests) errors down below by sleeping for the cool down time, then trying again. + # Raise an exception for any other 4xx or 5xx errors here + # TODO: Optimize this logic + if response.status_code != requests.codes.too_many_requests: + response.raise_for_status() # If we have two or less remaining calls in the period, wait (if the wait flag is set). # I tried only waiting after a 429 error, and ended getting locked out doing parallel testing. @@ -194,7 +193,7 @@ def _call(self, service_name, parameters=None, **kwargs): logger.warning('Approaching the rate limit. Sleeping for {0} seconds'.format(sleep_time)) sleep(sleep_time) - if response.status_code == 429: + if response.status_code == requests.codes.too_many_requests: logger.error('Request limit exceeded.') if self.overlimit_wait: # We should have already waited diff --git a/tests/test_client.py b/tests/test_client.py index dfdfc88..94dc990 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,6 @@ import os import pytest +from requests import HTTPError from meetup import exceptions from meetup.api import API_KEY_ENV_NAME, Client, MeetupObject @@ -25,8 +26,9 @@ def test_empty_key(self, api_client): def test_invalid_key(self, api_client): # Same with invalid API Key api_client.api_key = 'foobarbaz' - with pytest.raises(exceptions.HttpUnauthorized): + with pytest.raises(HTTPError) as excinfo: api_client.GetDashboard() + assert '401 Client Error' in str(excinfo.value) @pytest.mark.incremental diff --git a/tests/test_groups.py b/tests/test_groups.py index 46488f4..d4b44f0 100644 --- a/tests/test_groups.py +++ b/tests/test_groups.py @@ -1,6 +1,6 @@ import pytest +from requests import HTTPError -from meetup import exceptions from meetup.api import Client, MeetupObject @@ -22,14 +22,16 @@ def test_get_valid_group(api_client, group_name): @pytest.mark.parametrize("group_name", invalid_groups) def test_get_invalid_group(api_client, group_name): - with pytest.raises(exceptions.HttpNotFoundError): + with pytest.raises(HTTPError) as excinfo: api_client.GetGroup({'urlname': group_name}) + assert '404 Client Error' in str(excinfo.value) @pytest.mark.parametrize("group_name", inaccessible_groups) def test_get_inaccessible_group(api_client, group_name): - with pytest.raises(exceptions.HttpNotAccessibleError): + with pytest.raises(HTTPError) as excinfo: api_client.GetGroup({'urlname': group_name}) + assert '410 Client Error' in str(excinfo.value) @pytest.mark.parametrize("group_name", valid_groups)