Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for version compatibility on login #329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading