Skip to content

Commit

Permalink
track changes in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
BerndSchuller committed Mar 22, 2024
1 parent 70cfdba commit b063c2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ repos:
]

- repo: https://github.com/psf/black
rev: 22.10.0
rev: 24.3.0
hooks:
- id: black
args: ["--config", "pyproject.toml"]
Expand Down
26 changes: 20 additions & 6 deletions pyunicore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ def __init__(
self.verify = verify
self.use_security_sessions = use_security_sessions
self.last_session_id = None
self.preferences = None
self._preferences = None
self.timeout = timeout
self.settings_changed = True

def _clone(self):
"""create a copy of this transport"""
tr = Transport(self.credential)
tr.preferences = self.preferences
tr._preferences = self._preferences
tr.use_security_sessions = self.use_security_sessions
tr.last_session_id = self.last_session_id
tr.timeout = self.timeout
Expand All @@ -120,15 +121,25 @@ def _headers(self, kwargs):
if self.use_security_sessions and self.last_session_id is not None:
headers["X-UNICORE-SecuritySession"] = self.last_session_id

if self.preferences is not None:
headers["X-UNICORE-User-Preferences"] = self.preferences
if self._preferences is not None:
headers["X-UNICORE-User-Preferences"] = self._preferences

if "headers" in kwargs:
headers.update(kwargs["headers"])
del kwargs["headers"]

return headers

@property
def preferences(self):
return self._preferences

@preferences.setter
def preferences(self, value):
self._preferences = value
self.last_session_id = None
self.settings_changed = True

def check_error(self, res):
"""checks for error and extracts any error info sent by the server"""
if 400 <= res.status_code < 600:
Expand Down Expand Up @@ -163,6 +174,7 @@ def run_method(self, method, **args):
self.check_error(res)
if self.use_security_sessions:
self.last_session_id = res.headers.get("X-UNICORE-SecuritySession", None)
self.settings_changed = False
return res

def get(self, to_json=True, **kwargs):
Expand Down Expand Up @@ -221,8 +233,10 @@ def __init__(self, security, resource_url, cache_time=_DEFAULT_CACHE_TIME):
def properties(self):
"""get resource properties (these are cached for cache_time seconds)"""
now = datetime.now()
if self.cache_time <= 0 or (
timedelta(seconds=self.cache_time) < now - self._last_retrieved
if (
self.transport.settings_changed
or self.cache_time <= 0
or (timedelta(seconds=self.cache_time) < now - self._last_retrieved)
):
self._last_properties = self.transport.get(url=self.resource_url)
self._last_retrieved = now
Expand Down

0 comments on commit b063c2d

Please sign in to comment.