Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions predicthq/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion predicthq/endpoints/v1/events/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 8 additions & 4 deletions tests/client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/endpoints/v1/events_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down