Skip to content

Commit

Permalink
py client version check fixes and cleanup (#1497)
Browse files Browse the repository at this point in the history
* move _version_check() call outside class definition

* more py client version check cleanup/fixes

* version check test update

---------

Co-authored-by: minhkhul <[email protected]>
Co-authored-by: Dmitry Shemetov <[email protected]>
Co-authored-by: minhkhul <[email protected]>
  • Loading branch information
4 people authored Jul 25, 2024
1 parent af96e25 commit 72b4050
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
8 changes: 2 additions & 6 deletions integrations/client/test_delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,15 @@ def raise_for_status(self): pass
def json(self): return json.loads(self.content)
get.reset_mock()
get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200)

Epidata._version_check()

captured = self.capsys.readouterr()
output = captured.err.splitlines()
self.assertEqual(len(output), 1)
self.assertIn("Client version not up to date", output[0])
self.assertIn("\'latest_version\': \'0.0.1\'", output[0])

@patch('delphi.epidata.client.delphi_epidata.Epidata._version_check')
def test_version_check_once(self, version_check):
"""Test that the _version_check() function is only called once on initial module import."""
from delphi.epidata.client.delphi_epidata import Epidata
version_check.assert_not_called()

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

Expand Down
30 changes: 19 additions & 11 deletions src/client/delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,24 @@ def log(evt, **kwargs):
kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z")
return sys.stderr.write(str(kwargs) + "\n")

# Check that this client's version matches the most recent available, runs just once per program execution (on initial module load).
# Check that this client's version matches the most recent available. This
# is intended to run just once per program execution, on initial module load.
# See the bottom of this file for the ultimate call to this method.
@staticmethod
def _version_check():
try:
latest_version = requests.get('https://pypi.org/pypi/delphi-epidata/json').json()['info']['version']
if latest_version != __version__:
Epidata.log(
"Client version not up to date",
client_version=__version__,
latest_version=latest_version
)
request = requests.get('https://pypi.org/pypi/delphi-epidata/json', timeout=5)
latest_version = request.json()['info']['version']
except Exception as e:
Epidata.log("Error getting latest client version", exception=str(e))
return

# Run this once on module load. Use dunder method for Python <= 3.9 compatibility
# https://stackoverflow.com/a/12718272
_version_check.__func__()
if latest_version != __version__:
Epidata.log(
"Client version not up to date",
client_version=__version__,
latest_version=latest_version
)

# Helper function to cast values and/or ranges to strings
@staticmethod
Expand Down Expand Up @@ -708,3 +709,10 @@ async def async_make_calls(param_combos):
future = asyncio.ensure_future(async_make_calls(param_list))
responses = loop.run_until_complete(future)
return responses



# This should only run once per program execution, on initial module load,
# as a result of how Python's module system works:
# https://docs.python.org/3/reference/import.html#the-module-cache
Epidata._version_check()

0 comments on commit 72b4050

Please sign in to comment.