Skip to content

Commit 58fc8cb

Browse files
authored
Merge pull request #309 from chinandrew/patch-1
Change requests from GET to POST
2 parents 41de06b + 67dfb15 commit 58fc8cb

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

integrations/client/test_delphi_epidata.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# standard library
44
import unittest
5+
from unittest.mock import patch, MagicMock
56

67
# third party
78
import mysql.connector
@@ -231,6 +232,29 @@ def test_covidcast(self):
231232
}],
232233
'message': 'success',
233234
})
235+
with self.subTest(name='long request'):
236+
# fetch data, without specifying issue or lag
237+
response_1 = Epidata.covidcast(
238+
'src', 'sig'*3000, 'day', 'county', 20200414, '01234')
239+
240+
# check result
241+
self.assertEqual(response_1, {'message': 'no results', 'result': -2})
242+
243+
@patch('requests.post')
244+
@patch('requests.get')
245+
def test_request_method(self, get, post):
246+
"""Test that a GET request is default and POST is used if a 414 is returned."""
247+
with self.subTest(name='get request'):
248+
Epidata.covidcast('src', 'sig', 'day', 'county', 20200414, '01234')
249+
get.assert_called_once()
250+
post.assert_not_called()
251+
with self.subTest(name='post request'):
252+
mock_response = MagicMock()
253+
mock_response.status_code = 414
254+
get.return_value = mock_response
255+
Epidata.covidcast('src', 'sig', 'day', 'county', 20200414, '01234')
256+
self.assertEqual(get.call_count, 2) # one from post test and one from get test
257+
post.assert_called_once()
234258

235259
def test_geo_value(self):
236260
"""test different variants of geo types: single, *, multi."""

integrations/server/test_covidcast.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,41 @@ def test_round_trip(self):
7979
'message': 'success',
8080
})
8181

82+
def test_uri_too_long(self):
83+
"""Test that a long request yields a 414 with GET but works with POST."""
84+
85+
# insert dummy data
86+
self.cur.execute('''
87+
insert into covidcast values
88+
(0, 'src', 'sig', 'day', 'county', 20200414, '01234',
89+
123, 1.5, 2.5, 3.5, 456, 4, 20200414, 0, 1, False)
90+
''')
91+
self.cnx.commit()
92+
93+
# make the request with GET
94+
response = requests.get(BASE_URL, {
95+
'source': 'covidcast',
96+
'data_source': 'src'*10000,
97+
'signal': 'sig',
98+
'time_type': 'day',
99+
'geo_type': 'county',
100+
'time_values': 20200414,
101+
'geo_value': '01234',
102+
})
103+
self.assertEqual(response.status_code, 414)
104+
105+
# make request with POST
106+
response = requests.post(BASE_URL, {
107+
'source': 'covidcast',
108+
'data_source': 'src'*10000,
109+
'signal': 'sig',
110+
'time_type': 'day',
111+
'geo_type': 'county',
112+
'time_values': 20200414,
113+
'geo_value': '01234',
114+
})
115+
116+
self.assertEqual(response.status_code, 200)
82117

83118
def test_csv_format(self):
84119
"""Test generate csv data."""

src/client/delphi_epidata.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ def _list(values):
5252
# Helper function to request and parse epidata
5353
@staticmethod
5454
def _request(params):
55-
"""Request and parse epidata."""
55+
"""Request and parse epidata.
56+
57+
We default to GET since it has better caching and logging
58+
capabilities, but fall back to POST if the request is too
59+
long and returns a 414.
60+
"""
5661
try:
5762
# API call
58-
return requests.get(Epidata.BASE_URL, params, headers=_HEADERS).json()
63+
req = requests.get(Epidata.BASE_URL, params, headers=_HEADERS)
64+
if req.status_code == 414:
65+
req = requests.post(Epidata.BASE_URL, params, headers=_HEADERS)
66+
return req.json()
5967
except Exception as e:
6068
# Something broke
6169
return {'result': 0, 'message': 'error: ' + str(e)}

0 commit comments

Comments
 (0)