From 9237a6077ec9ccc339d37e51a49c70ca95fb5b38 Mon Sep 17 00:00:00 2001 From: Robert Stein Date: Sat, 3 Feb 2024 17:27:10 -0800 Subject: [PATCH] Check minimal server version --- pyproject.toml | 3 ++- winterapi/endpoints.py | 1 + winterapi/messenger.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 99a3777..069ac1b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "winterapi" -version = "0.3.0" +version = "1.0.0" description = "" authors = [ {name = "Robert Stein", email = "rdstein@caltech.edu"} @@ -29,6 +29,7 @@ classifiers = [ ] dependencies = [ "pandas", + "packaging", "keyring", "cryptography", "pre-commit", diff --git a/winterapi/endpoints.py b/winterapi/endpoints.py index 60ef514..3472459 100644 --- a/winterapi/endpoints.py +++ b/winterapi/endpoints.py @@ -7,6 +7,7 @@ run_local = os.getenv("WINTER_API_LOCAL", "0") in ["True", "true", "1"] BASE_URL = "http://127.0.0.1:7000" if run_local else "http://winter.caltech.edu:82" PING_URL = BASE_URL + "/ping" +VERSION_URL = BASE_URL + "/validation/version" USER_URL = BASE_URL + "/validation/user" PROGRAM_URL = BASE_URL + "/validation/program" WINTER_TOO_URL = BASE_URL + "/too/winter" diff --git a/winterapi/messenger.py b/winterapi/messenger.py index 8e1ffe0..8bdc203 100644 --- a/winterapi/messenger.py +++ b/winterapi/messenger.py @@ -4,12 +4,14 @@ import getpass import logging +from importlib import metadata from typing import Optional import pandas as pd import requests from astropy import units as u from astropy.time import Time +from packaging import version from wintertoo.models import ( DEFAULT_IMAGE_TYPE, ConeImageQuery, @@ -42,6 +44,7 @@ SCHEDULE_SUMMARY_URL, SUMMER_TOO_URL, USER_URL, + VERSION_URL, WINTER_TOO_URL, ) from winterapi.fidelius import Fidelius @@ -56,8 +59,12 @@ class WinterAPI(BaseAPI): def __init__(self): self.fidelius = Fidelius() - logger.info(f"API ping success is {self.ping()}") + ping = self.ping() + if not ping: + logger.warning("Could not successfully ping server") + logger.info(f"API ping success is {ping}") self.auth = (None, None) + self.check_version() @staticmethod def ping(): @@ -73,6 +80,28 @@ def ping(): except requests.exceptions.ConnectionError: return False + @staticmethod + def check_version(): + """ + Check the version of the API. + + :return: API response + """ + res = requests.get(VERSION_URL, timeout=MAX_TIMEOUT) + if res.status_code == 200: + server_version = version.parse(res.json()["body"]) + local_version = version.parse(metadata.version("winterapi")) + logger.info(f"Server requires minimum winterapi version: {server_version}") + logger.info(f"Local winterapi version: {local_version}") + if server_version > local_version: + logger.warning( + f"Local winterapi version ({local_version}) is out of date! " + f"Server requires a minimum of {server_version}. " + f"Please update winterapi." + ) + else: + logger.warning("Could not check minimum version of winterapi for server") + @staticmethod def clear_cache(): """