Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of PHP alias in the Python client #1288

Merged
merged 18 commits into from
Oct 12, 2023
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
6 changes: 2 additions & 4 deletions docs/new_endpoint_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ Here's what we add to each client:
def fluview_meta():
"""Fetch FluView metadata."""
# Set up request
params = {
'endpoint': 'fluview_meta',
}
params = {}
# Make the API call
return Epidata._request(params)
return Epidata._request("fluview_meta", params)
```

- [`delphi_epidata.R`](https://github.com/cmu-delphi/delphi-epidata/blob/dev/src/client/delphi_epidata.R)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):
self.test_utils = UnitTestUtils(__file__)

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

# use the local instance of the epidata database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setUp(self):
self.test_utils = UnitTestUtils(__file__)

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

# use the local instance of the epidata database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):
self.test_utils = UnitTestUtils(__file__)

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

# use the local instance of the epidata database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)

# use the local instance of the Epidata API
BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
BASE_URL = 'http://delphi_web_epidata/epidata'


class CovidcastMetaCacheTests(unittest.TestCase):
Expand Down Expand Up @@ -82,8 +82,8 @@ def tearDown(self):

@staticmethod
def _make_request():
params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
response = requests.get(Epidata.BASE_URL, params=params, auth=Epidata.auth)
params = {'cached': 'true'}
response = requests.get(f"{Epidata.BASE_URL}/covidcast_meta", params=params, auth=Epidata.auth)
response.raise_for_status()
return response.json()

Expand Down
17 changes: 13 additions & 4 deletions integrations/acquisition/covidcast/test_csv_uploading.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def setUp(self):
epidata_cnx.close()

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

def tearDown(self):
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_uploading(self):
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values = pd.concat([values, pd.DataFrame({ "time_value": [20200419] * 3, "signal": [signal_name] * 3, "direction": [None] * 3})], axis=1).rename(columns=uploader_column_rename).to_dict(orient="records")
expected_values = pd.concat([values, pd.DataFrame({ "geo_type": "state", "source": "src-name", "time_type": "day", "time_value": [20200419] * 3, "signal": [signal_name] * 3, "direction": [None] * 3})], axis=1).rename(columns=uploader_column_rename).to_dict(orient="records")
expected_response = {'result': 1, 'epidata': self.apply_lag(expected_values), 'message': 'success'}

self.assertEqual(response, expected_response)
Expand Down Expand Up @@ -161,6 +161,9 @@ def test_uploading(self):
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values = pd.concat([values, pd.DataFrame({
"geo_type": "state",
"source": "src-name",
"time_type": "day",
"time_value": [20200419] * 3,
"signal": [signal_name] * 3,
"direction": [None] * 3,
Expand Down Expand Up @@ -194,7 +197,7 @@ def test_uploading(self):
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_response = {'result': -2, 'message': 'no results'}
expected_response = {'epidata': [], 'result': -2, 'message': 'no results'}

self.assertEqual(response, expected_response)
self.verify_timestamps_and_defaults()
Expand All @@ -220,6 +223,9 @@ def test_uploading(self):
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values_df = pd.concat([values, pd.DataFrame({
"geo_type": "state",
"source": "src-name",
"time_type": "day",
"time_value": [20200419],
"signal": [signal_name],
"direction": [None]})], axis=1).rename(columns=uploader_column_rename)
Expand Down Expand Up @@ -253,6 +259,9 @@ def test_uploading(self):
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_values = pd.concat([values, pd.DataFrame({
"geo_type": "state",
"source": "src-name",
"time_type": "day",
"time_value": [20200419],
"signal": [signal_name],
"direction": [None]
Expand Down Expand Up @@ -283,7 +292,7 @@ def test_uploading(self):
main(args)
response = Epidata.covidcast('src-name', signal_name, 'day', 'state', 20200419, '*')

expected_response = {'result': -2, 'message': 'no results'}
expected_response = {'epidata': [], 'result': -2, 'message': 'no results'}

self.assertEqual(response, expected_response)
self.verify_timestamps_and_defaults()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def setUp(self):
secrets.db.epi = ('user', 'pass')

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

def tearDown(self):
Expand Down
42 changes: 22 additions & 20 deletions integrations/client/test_delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
def fake_epidata_endpoint(func):
"""This can be used as a decorator to enable a bogus Epidata endpoint to return 404 responses."""
def wrapper(*args):
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/fake_api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/fake_epidata'
func(*args)
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
return wrapper

class DelphiEpidataPythonClientTests(CovidcastBase):
Expand All @@ -39,7 +39,7 @@ def localSetUp(self):
self._db._cursor.execute('update covidcast_meta_cache set timestamp = 0, epidata = "[]"')

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

# use the local instance of the epidata database
Expand All @@ -65,8 +65,8 @@ def test_covidcast(self):
)

expected = [
row_latest_issue.as_api_compatibility_row_dict(),
rows[-1].as_api_compatibility_row_dict()
row_latest_issue.as_api_row_dict(),
rows[-1].as_api_row_dict()
]

self.assertEqual(response['epidata'], expected)
Expand All @@ -85,10 +85,10 @@ def test_covidcast(self):

expected = [{
rows[0].signal: [
row_latest_issue.as_api_compatibility_row_dict(ignore_fields=['signal']),
row_latest_issue.as_api_row_dict(ignore_fields=['signal']),
],
rows[-1].signal: [
rows[-1].as_api_compatibility_row_dict(ignore_fields=['signal']),
rows[-1].as_api_row_dict(ignore_fields=['signal']),
],
}]

Expand All @@ -105,7 +105,7 @@ def test_covidcast(self):
**self.params_from_row(rows[0])
)

expected = [row_latest_issue.as_api_compatibility_row_dict()]
expected = [row_latest_issue.as_api_row_dict()]

# check result
self.assertEqual(response_1, {
Expand All @@ -120,7 +120,7 @@ def test_covidcast(self):
**self.params_from_row(rows[0], as_of=rows[1].issue)
)

expected = [rows[1].as_api_compatibility_row_dict()]
expected = [rows[1].as_api_row_dict()]

# check result
self.maxDiff=None
Expand All @@ -137,8 +137,8 @@ def test_covidcast(self):
)

expected = [
rows[0].as_api_compatibility_row_dict(),
rows[1].as_api_compatibility_row_dict()
rows[0].as_api_row_dict(),
rows[1].as_api_row_dict()
]

# check result
Expand All @@ -154,7 +154,7 @@ def test_covidcast(self):
**self.params_from_row(rows[0], lag=2)
)

expected = [row_latest_issue.as_api_compatibility_row_dict()]
expected = [row_latest_issue.as_api_row_dict()]

# check result
self.assertDictEqual(response_3, {
Expand All @@ -170,7 +170,7 @@ def test_covidcast(self):
)

# check result
self.assertEqual(response_1, {'message': 'no results', 'result': -2})
self.assertEqual(response_1, {'epidata': [], 'message': 'no results', 'result': -2})

@patch('requests.post')
@patch('requests.get')
Expand All @@ -196,7 +196,7 @@ def test_retry_request(self, get):
mock_response = MagicMock()
mock_response.status_code = 200
get.side_effect = [JSONDecodeError('Expecting value', "", 0), mock_response]
response = Epidata._request(None)
response = Epidata._request("")
self.assertEqual(get.call_count, 2)
self.assertEqual(response, mock_response.json())

Expand All @@ -207,7 +207,7 @@ def test_retry_request(self, get):
get.side_effect = [JSONDecodeError('Expecting value', "", 0),
JSONDecodeError('Expecting value', "", 0),
mock_response]
response = Epidata._request(None)
response = Epidata._request("")
self.assertEqual(get.call_count, 2) # 2 from previous test + 2 from this one
self.assertEqual(response,
{'result': 0, 'message': 'error: Expecting value: line 1 column 1 (char 0)'}
Expand All @@ -228,7 +228,7 @@ def test_geo_value(self):
self._insert_rows(rows)

counties = [
rows[i].as_api_compatibility_row_dict() for i in range(N)
rows[i].as_api_row_dict() for i in range(N)
]

def fetch(geo):
Expand Down Expand Up @@ -339,13 +339,15 @@ def test_async_epidata(self):
self.params_from_row(rows[0], source='covidcast'),
self.params_from_row(rows[1], source='covidcast')
]*12, batch_size=10)
responses = [i[0] for i in test_output]
# check response is same as standard covidcast call, using 24 calls to test batch sizing
responses = [i[0]["epidata"] for i in test_output]
# check response is same as standard covidcast call (minus fields omitted by the api.php endpoint),
# using 24 calls to test batch sizing
ignore_fields = CovidcastTestRow._api_row_compatibility_ignore_fields
self.assertEqual(
responses,
[
Epidata.covidcast(**self.params_from_row(rows[0])),
Epidata.covidcast(**self.params_from_row(rows[1])),
[{k: row[k] for k in row.keys() - ignore_fields} for row in Epidata.covidcast(**self.params_from_row(rows[0]))["epidata"]],
[{k: row[k] for k in row.keys() - ignore_fields} for row in Epidata.covidcast(**self.params_from_row(rows[1]))["epidata"]],
]*12
)

Expand Down
4 changes: 2 additions & 2 deletions integrations/client/test_nowcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self):
self.cur = cnx.cursor()

# use the local instance of the Epidata API
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata/api.php'
Epidata.BASE_URL = 'http://delphi_web_epidata/epidata'
Epidata.auth = ('epidata', 'key')

# use the local instance of the epidata database
Expand Down Expand Up @@ -133,4 +133,4 @@ def test_covidcast_nowcast(self):
response = Epidata.covidcast_nowcast(
'src', 'sig1', 'sensor', 'day', 'county', 22222222, '01001')

self.assertEqual(response, {'result': -2, 'message': 'no results'})
self.assertEqual(response, {'epidata': [], 'result': -2, 'message': 'no results'})
Loading
Loading