Skip to content

Commit 6a3f683

Browse files
melange396rzats
andauthored
py client: drop delphi_utils requirement, log to stderr instead (#1486)
* py client: drop bloated delphi_utils requirement, log to stderr instead * Replace assertlogs with stderr capture --------- Co-authored-by: Rostyslav Zatserkovnyi <[email protected]>
1 parent 694d89a commit 6a3f683

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

integrations/client/test_delphi_epidata.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def localSetUp(self):
4949
secrets.db.host = 'delphi_database_epidata'
5050
secrets.db.epi = ('user', 'pass')
5151

52+
@pytest.fixture(autouse=True)
53+
def capsys(self, capsys):
54+
"""Hook capsys (stdout and stderr) into this test class."""
55+
56+
self.capsys = capsys
57+
5258
def test_covidcast(self):
5359
"""Test that the covidcast endpoint returns expected data."""
5460

@@ -238,46 +244,46 @@ def raise_for_status(self): pass
238244

239245
try:
240246
with self.subTest(name='test multiple GET'):
241-
with self.assertLogs('delphi_epidata_client', level='INFO') as logs:
242-
get.reset_mock()
243-
get.return_value = MockResponse(b'{"key": "value"}', 200)
244-
Epidata._request_with_retry("test_endpoint1", params={"key1": "value1"})
245-
Epidata._request_with_retry("test_endpoint2", params={"key2": "value2"})
247+
get.reset_mock()
248+
get.return_value = MockResponse(b'{"key": "value"}', 200)
249+
Epidata._request_with_retry("test_endpoint1", params={"key1": "value1"})
250+
Epidata._request_with_retry("test_endpoint2", params={"key2": "value2"})
246251

247-
output = logs.output
252+
captured = self.capsys.readouterr()
253+
output = captured.err.splitlines()
248254
self.assertEqual(len(output), 4) # [request, response, request, response]
249255
self.assertIn("Sending GET request", output[0])
250-
self.assertIn("\"url\": \"http://delphi_web_epidata/epidata/test_endpoint1/\"", output[0])
251-
self.assertIn("\"params\": {\"key1\": \"value1\"}", output[0])
256+
self.assertIn("\'url\': \'http://delphi_web_epidata/epidata/test_endpoint1/\'", output[0])
257+
self.assertIn("\'params\': {\'key1\': \'value1\'}", output[0])
252258
self.assertIn("Received response", output[1])
253-
self.assertIn("\"status_code\": 200", output[1])
254-
self.assertIn("\"len\": 16", output[1])
259+
self.assertIn("\'status_code\': 200", output[1])
260+
self.assertIn("\'len\': 16", output[1])
255261
self.assertIn("Sending GET request", output[2])
256-
self.assertIn("\"url\": \"http://delphi_web_epidata/epidata/test_endpoint2/\"", output[2])
257-
self.assertIn("\"params\": {\"key2\": \"value2\"}", output[2])
262+
self.assertIn("\'url\': \'http://delphi_web_epidata/epidata/test_endpoint2/\'", output[2])
263+
self.assertIn("\'params\': {\'key2\': \'value2\'}", output[2])
258264
self.assertIn("Received response", output[3])
259-
self.assertIn("\"status_code\": 200", output[3])
260-
self.assertIn("\"len\": 16", output[3])
265+
self.assertIn("\'status_code\': 200", output[3])
266+
self.assertIn("\'len\': 16", output[3])
261267

262268
with self.subTest(name='test GET and POST'):
263-
with self.assertLogs('delphi_epidata_client', level='INFO') as logs:
264-
get.reset_mock()
265-
get.return_value = MockResponse(b'{"key": "value"}', 414)
266-
post.reset_mock()
267-
post.return_value = MockResponse(b'{"key": "value"}', 200)
268-
Epidata._request_with_retry("test_endpoint3", params={"key3": "value3"})
269-
270-
output = logs.output
271-
self.assertEqual(len(output), 3) # [request, response, request, response]
269+
get.reset_mock()
270+
get.return_value = MockResponse(b'{"key": "value"}', 414)
271+
post.reset_mock()
272+
post.return_value = MockResponse(b'{"key": "value"}', 200)
273+
Epidata._request_with_retry("test_endpoint3", params={"key3": "value3"})
274+
275+
captured = self.capsys.readouterr()
276+
output = captured.err.splitlines()
277+
self.assertEqual(len(output), 3) # [request, retry, response]
272278
self.assertIn("Sending GET request", output[0])
273-
self.assertIn("\"url\": \"http://delphi_web_epidata/epidata/test_endpoint3/\"", output[0])
274-
self.assertIn("\"params\": {\"key3\": \"value3\"}", output[0])
279+
self.assertIn("\'url\': \'http://delphi_web_epidata/epidata/test_endpoint3/\'", output[0])
280+
self.assertIn("\'params\': {\'key3\': \'value3\'}", output[0])
275281
self.assertIn("Received 414 response, retrying as POST request", output[1])
276-
self.assertIn("\"url\": \"http://delphi_web_epidata/epidata/test_endpoint3/\"", output[1])
277-
self.assertIn("\"params\": {\"key3\": \"value3\"}", output[1])
282+
self.assertIn("\'url\': \'http://delphi_web_epidata/epidata/test_endpoint3/\'", output[1])
283+
self.assertIn("\'params\': {\'key3\': \'value3\'}", output[1])
278284
self.assertIn("Received response", output[2])
279-
self.assertIn("\"status_code\": 200", output[2])
280-
self.assertIn("\"len\": 16", output[2])
285+
self.assertIn("\'status_code\': 200", output[2])
286+
self.assertIn("\'len\': 16", output[2])
281287
finally: # make sure this global is always reset
282288
Epidata.debug = False
283289

@@ -288,12 +294,12 @@ def test_sandbox(self, get, post):
288294
Epidata.debug = True
289295
Epidata.sandbox = True
290296
try:
291-
with self.assertLogs('delphi_epidata_client', level='INFO') as logs:
292-
Epidata.covidcast('src', 'sig', 'day', 'county', 20200414, '01234')
293-
output = logs.output
297+
Epidata.covidcast('src', 'sig', 'day', 'county', 20200414, '01234')
298+
captured = self.capsys.readouterr()
299+
output = captured.err.splitlines()
294300
self.assertEqual(len(output), 1)
295301
self.assertIn("Sending GET request", output[0])
296-
self.assertIn("\"url\": \"http://delphi_web_epidata/epidata/covidcast/\"", output[0])
302+
self.assertIn("\'url\': \'http://delphi_web_epidata/epidata/covidcast/\'", output[0])
297303
get.assert_not_called()
298304
post.assert_not_called()
299305
finally: # make sure these globals are always reset

src/client/delphi_epidata.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- Compatible with Python 2 and 3.
99
"""
1010

11+
import sys
12+
1113
# External modules
1214
import requests
1315
import time
@@ -16,8 +18,6 @@
1618

1719
from aiohttp import ClientSession, TCPConnector, BasicAuth
1820

19-
from delphi_utils.logger import get_structured_logger
20-
2121
__version__ = "4.1.23"
2222

2323
_HEADERS = {"user-agent": "delphi_epidata/" + __version__ + " (Python)"}
@@ -45,10 +45,15 @@ class Epidata:
4545

4646
client_version = __version__
4747

48-
logger = get_structured_logger('delphi_epidata_client')
4948
debug = False # if True, prints extra logging statements
5049
sandbox = False # if True, will not execute any queries
5150

51+
@staticmethod
52+
def log(evt, **kwargs):
53+
kwargs['event'] = evt
54+
kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z")
55+
return sys.stderr.write(str(kwargs) + "\n")
56+
5257
# Helper function to cast values and/or ranges to strings
5358
@staticmethod
5459
def _listitem(value):
@@ -72,7 +77,7 @@ def _request_with_retry(endpoint, params={}):
7277
"""Make request with a retry if an exception is thrown."""
7378
request_url = f"{Epidata.BASE_URL}/{endpoint}/"
7479
if Epidata.debug:
75-
Epidata.logger.info("Sending GET request", url=request_url, params=params, headers=_HEADERS, auth=Epidata.auth)
80+
Epidata.log("Sending GET request", url=request_url, params=params, headers=_HEADERS, auth=Epidata.auth)
7681
if Epidata.sandbox:
7782
resp = requests.Response()
7883
resp._content = b'true'
@@ -81,10 +86,10 @@ def _request_with_retry(endpoint, params={}):
8186
req = requests.get(request_url, params, auth=Epidata.auth, headers=_HEADERS)
8287
if req.status_code == 414:
8388
if Epidata.debug:
84-
Epidata.logger.info("Received 414 response, retrying as POST request", url=request_url, params=params, headers=_HEADERS)
89+
Epidata.log("Received 414 response, retrying as POST request", url=request_url, params=params, headers=_HEADERS)
8590
req = requests.post(request_url, params, auth=Epidata.auth, headers=_HEADERS)
8691
if Epidata.debug:
87-
Epidata.logger.info(
92+
Epidata.log(
8893
"Received response",
8994
status_code=req.status_code,
9095
len=len(req.content),

src/client/packaging/pypi/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ classifiers = [
3737
"Natural Language :: English",
3838
"Topic :: Scientific/Engineering :: Bio-Informatics",
3939
]
40-
dependencies = ["aiohttp", "delphi-utils", "requests>=2.7.0", "tenacity"]
40+
dependencies = ["aiohttp", "requests>=2.7.0", "tenacity"]
4141

4242
[project.urls]
4343
"Homepage" = "https://github.com/cmu-delphi/delphi-epidata"

0 commit comments

Comments
 (0)