Skip to content

Commit

Permalink
Check for version compatibility on login
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Nov 22, 2023
1 parent ee27284 commit 62ecc31
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 is
established and warning is printed on mismatch


===================
Expand Down
30 changes: 30 additions & 0 deletions src/resdk/resolwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -162,8 +165,35 @@ def __init__(self, username=None, password=None, url=None):
password = os.environ.get("RESOLWE_API_PASSWORD", None)

self.url = url

# Check minimal supported version.
self.version_check()

self._login(username=username, password=password)

def version_check(self):
"""Check that the server is compatible with the client."""
url = urljoin(self.url, MINIMAL_SUPPORTED_VERSION_POSTFIX)
try:
response = requests.get(url)
minimal_version = version.parse(
response.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 supported version is '{minimal_version}'. "
"To update the package run\n\n"
"python -m pip install --upgrade resdk\n\n"
"from the command line."
)
self.logger.warning(message)
except Exception:
self.logger.warning(
"Warning: unable to read the minimal supported version from the server."
)

def _validate_url(self, url):
if not re.match(r"https?://", url):
raise ValueError("Server url must start with http(s)://")
Expand Down

0 comments on commit 62ecc31

Please sign in to comment.