diff --git a/predicthq/client.py b/predicthq/client.py index b9aff76..6ddcaf4 100644 --- a/predicthq/client.py +++ b/predicthq/client.py @@ -48,10 +48,15 @@ def request(self, method, path, **kwargs): try: response.raise_for_status() except requests.HTTPError: + try: + error = response.json() + except ValueError: + error = response.content + if 400 <= response.status_code <= 499: - raise ClientError(response.json()) + raise ClientError(error) else: - raise ServerError(response.json()) + raise ServerError(error) try: result = response.json() or None diff --git a/predicthq/endpoints/v1/events/schemas.py b/predicthq/endpoints/v1/events/schemas.py index eca2cd0..0956624 100644 --- a/predicthq/endpoints/v1/events/schemas.py +++ b/predicthq/endpoints/v1/events/schemas.py @@ -68,8 +68,8 @@ class TopEventsSearchParams(SortableMixin, Model): class CalendarParams(SearchParams): - dates = ModelType(DateTimeRange) top_events = ModelType(TopEventsSearchParams) + view = StringType(choices=('active',)) class CalendarDay(Model): diff --git a/tests/client_tests.py b/tests/client_tests.py index a218649..4b7d56b 100644 --- a/tests/client_tests.py +++ b/tests/client_tests.py @@ -46,14 +46,18 @@ def test_request(self, responses): self.client.get("/server-error/") self.assertEqual(ctx.exception.message, responses.calls[4].response.json()) + with self.assertRaises(ServerError) as ctx: + self.client.get("/no-json/") + self.assertEqual(ctx.exception.message, responses.calls[5].response.content) + # Test headers self.client.authenticate(client_id='client_id', client_secret='client_secret', scope=['account']) - self.assertEqual(responses.calls[5].request.headers['Authorization'], 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=') - self.assertEqual(responses.calls[5].request.headers['Content-Type'], 'application/x-www-form-urlencoded') + self.assertEqual(responses.calls[6].request.headers['Authorization'], 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=') + self.assertEqual(responses.calls[6].request.headers['Content-Type'], 'application/x-www-form-urlencoded') self.client.accounts.self() - self.assertEqual(responses.calls[6].request.headers['Authorization'], 'Bearer token123') - self.assertEqual(responses.calls[6].request.headers['Accept'], 'application/json') + self.assertEqual(responses.calls[7].request.headers['Authorization'], 'Bearer token123') + self.assertEqual(responses.calls[7].request.headers['Accept'], 'application/json') @with_mock_client(request_returns={"result": "value"}) def test_get(self, client): diff --git a/tests/endpoints/v1/events_tests.py b/tests/endpoints/v1/events_tests.py index f2c0337..f8d691f 100644 --- a/tests/endpoints/v1/events_tests.py +++ b/tests/endpoints/v1/events_tests.py @@ -46,7 +46,7 @@ def test_count(self, client, responses): @with_client() @with_mock_responses() def test_calendar(self, client, responses): - result = client.events.calendar(start__gte="2015-12-24", start__lte="2015-12-26", country="NZ", top_events__limit=1, top_events__sort=["rank"], dates__tz="Pacific/Auckland") + result = client.events.calendar(active__gte="2015-12-24", active__lte="2015-12-26", country="NZ", top_events__limit=1, top_events__sort=["rank"], active__tz="Pacific/Auckland") self.assertIsInstance(result, CalendarResultSet) self.assertEqual(result.count, 60) self.assertEqual(3, len(list(result.iter_all()))) diff --git a/tests/fixtures/requests_responses/client_test/test_request.json b/tests/fixtures/requests_responses/client_test/test_request.json index 417c2e8..e09d2cd 100644 --- a/tests/fixtures/requests_responses/client_test/test_request.json +++ b/tests/fixtures/requests_responses/client_test/test_request.json @@ -37,6 +37,14 @@ "content_type": "application/json", "body": {"error": "server error"} }, + { + "method": "GET", + "match_querystring": true, + "url": "/no-json/", + "status": 500, + "content_type": "text/plain", + "body": "server error" + }, { "method": "POST", "match_querystring": true,