-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Optional | ||
|
||
import requests | ||
|
||
from .urls import Url | ||
|
||
|
||
class Settings: | ||
def __init__(self, session: requests.Session, url: Url) -> None: | ||
self.session = session | ||
self.url = url | ||
|
||
@property | ||
def version(self) -> Optional[str]: | ||
return self.settings.get("version") | ||
|
||
@property | ||
def settings(self) -> dict: | ||
url = self.url + "server_settings" | ||
try: | ||
response = self.session.get(url) | ||
return response.json() | ||
except requests.exceptions.RequestException as e: | ||
import logging | ||
|
||
logging.debug( | ||
f"Failed to retrieve server settings from {url}. Error: {str(e)}", exc_info=True | ||
) | ||
|
||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from functools import wraps | ||
|
||
from packaging.version import Version | ||
|
||
from .exceptions import VersionUpgradeRequiredException | ||
|
||
|
||
def requires_version(expected: str): | ||
""" | ||
Decorator to enforce a minimum version requirement for a method. | ||
This decorator checks the `version` attribute of the class instance and raises a `VersionUpgradeRequiredException` if the version is lower than the specified `expected` version. | ||
Parameters | ||
---------- | ||
expected : str | ||
The minimum version required for the decorated method to execute. It is compared against the class instance's `version` attribute. | ||
Returns | ||
------- | ||
function | ||
The wrapped function that enforces the version check. | ||
Raises | ||
------ | ||
VersionUpgradeRequiredException | ||
If the version specified in the class instance's `version` attribute is lower than the `expected` version. | ||
Examples | ||
-------- | ||
To use this decorator, apply it to any method that requires a minimum version: | ||
>>> class Client: | ||
>>> version = "2024.07.0" | ||
>>> | ||
>>> @requires_version("2024.08.0") | ||
>>> def some_method(self): | ||
>>> pass | ||
>>> | ||
>>> client = Client() | ||
>>> client.some_method() | ||
Traceback (most recent call last): | ||
... | ||
VersionUpgradeRequiredException: This API is not available in Connect version 2024.07.0. Please upgrade to version 2024.08.0 or later." | ||
""" | ||
|
||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(self, *args, **kwargs): | ||
if hasattr(self, "version"): | ||
version = getattr(self, "version") | ||
if not version: | ||
return | ||
if Version(version) < Version(expected): | ||
raise VersionUpgradeRequiredException(version, expected) | ||
return func(self, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters