From 290424015d2c56aae58b7e14bfb295c6746b2e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20Jer=C5=A1e?= Date: Sat, 28 Oct 2023 22:13:34 +0200 Subject: [PATCH] Check for version compatibility on login --- docs/CHANGELOG.rst | 2 ++ src/resdk/resolwe.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/docs/CHANGELOG.rst b/docs/CHANGELOG.rst index 6a5f51b5..4a3e3cd4 100644 --- a/docs/CHANGELOG.rst +++ b/docs/CHANGELOG.rst @@ -13,6 +13,8 @@ Changed ------- - **BACKWARD INCOMPATIBLE:** Remove ``inherit_collection`` parameter in ``Data.duplicate()`` and ``Sample.duplicate()`` +- Minimal supported version is checked when connection to the server and + warning is printed on mismatch =================== diff --git a/src/resdk/resolwe.py b/src/resdk/resolwe.py index 674c3ee9..d402738d 100644 --- a/src/resdk/resolwe.py +++ b/src/resdk/resolwe.py @@ -16,11 +16,13 @@ import time import webbrowser from contextlib import suppress +from importlib.metadata import version as package_version from typing import Optional, TypedDict from urllib.parse import urlencode, urljoin, urlparse import requests import slumber +from packaging import version from resdk.uploader import Uploader @@ -48,6 +50,7 @@ DEFAULT_URL = "http://localhost:8000" AUTOMATIC_LOGIN_POSTFIX = "saml-auth/api-login/" INTERACTIVE_LOGIN_POSTFIX = "saml-auth/remote-login/" +MINIMAL_SUPPORTED_VERSION_POSTFIX = "api/resdk_minimal_supported_version" class ResolweResource(slumber.Resource): @@ -520,6 +523,9 @@ def __init__( self.automatic_login_url = urljoin(self.url, AUTOMATIC_LOGIN_POSTFIX) self.interactive_login_url = urljoin(self.url, INTERACTIVE_LOGIN_POSTFIX) + # Check minimal supported version. + self.version_check() + if not interactive and (username is None or password is None): # Anonymous authentication return @@ -531,6 +537,23 @@ def __init__( self.cookies = self.interactive_login() self.logger.info("Successfully logged in.") + def version_check(self): + """Check that the server is compatible with the client.""" + url = urljoin(self.url, MINIMAL_SUPPORTED_VERSION_POSTFIX) + minimal_version = version.parse( + requests.get(url).json["minimal_supported_version"] + ) + my_version = version.parse(package_version("resdk")) + if my_version < minimal_version: + message = ( + f"Warning: your version of ReSDK ('{my_version}') is not compatible with " + f"the server: minimal required version is '{minimal_version}'. " + "To update the package run\n\n" + "pip install --upgrade resdk\n\n" + "from the command line." + ) + print(message) + def automatic_login(self, username: str, password: str) -> AuthCookie: """Attempt to perform automatic SAML login.