Skip to content

Commit 2cb225c

Browse files
committed
chore: remove dead code and add config validation
1 parent a16d227 commit 2cb225c

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

src/sap_cloud_sdk/agentgateway/_token_cache.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def _parse_response_expires_at(expires_at: object) -> float | None:
9292
return parsed.timestamp()
9393

9494

95-
def _monotonic_expiry_from_epoch(expiry_epoch_seconds: float, buffer: float) -> float:
95+
def _monotonic_expiry_from_epoch(expiry_epoch_seconds: float,
96+
buffer: float) -> float:
9697
"""Translate a wall-clock expiry into a monotonic deadline."""
9798
return time.monotonic() + (expiry_epoch_seconds - time.time()) - buffer
9899

@@ -160,9 +161,6 @@ def __setitem__(self, scope_key: str, url: str) -> None:
160161
while len(self._cache) > self._max_size:
161162
self._cache.popitem(last=False)
162163

163-
def __contains__(self, scope_key: str) -> bool:
164-
return scope_key in self._cache
165-
166164

167165
class _TokenCache:
168166
"""Per-client token cache with TTL and LRU eviction.
@@ -188,13 +186,14 @@ def get_system_token(self, scope_key: str) -> str | None:
188186
del self._system_tokens[scope_key]
189187
return None
190188

191-
def set_system_token(self, token: str, expires_at: float, scope_key: str) -> None:
189+
def set_system_token(self, token: str, expires_at: float,
190+
scope_key: str) -> None:
192191
"""Cache a system token under `scope_key`; evict LRU once size exceeds limit."""
193-
self._system_tokens[scope_key] = _CachedToken(
194-
token=token, expires_at=expires_at
195-
)
192+
self._system_tokens[scope_key] = _CachedToken(token=token,
193+
expires_at=expires_at)
196194
self._system_tokens.move_to_end(scope_key)
197-
while len(self._system_tokens) > self._config.max_system_token_cache_size:
195+
while len(self._system_tokens
196+
) > self._config.max_system_token_cache_size:
198197
evicted, _ = self._system_tokens.popitem(last=False)
199198
logger.debug("System token cache full — evicted '%s'", evicted)
200199

@@ -225,7 +224,8 @@ def set_user_token(
225224
) -> None:
226225
"""Cache an exchanged user token; evict LRU once size exceeds limit."""
227226
key = self._hash_key(user_jwt, scope_key)
228-
self._user_tokens[key] = _CachedToken(token=token, expires_at=expires_at)
227+
self._user_tokens[key] = _CachedToken(token=token,
228+
expires_at=expires_at)
229229
self._user_tokens.move_to_end(key)
230230
while len(self._user_tokens) > self._config.max_user_token_cache_size:
231231
evicted, _ = self._user_tokens.popitem(last=False)
@@ -251,11 +251,8 @@ def compute_expires_at_from_bearer(self, auth_header: str) -> float:
251251
"""
252252
buffer = self._config.token_expiry_buffer_seconds
253253

254-
jwt = (
255-
auth_header[7:]
256-
if auth_header.lower().startswith("bearer ")
257-
else auth_header
258-
)
254+
jwt = (auth_header[7:]
255+
if auth_header.lower().startswith("bearer ") else auth_header)
259256
exp = _parse_jwt_exp(jwt)
260257
if exp is not None:
261258
return _monotonic_expiry_from_epoch(float(exp), buffer)

src/sap_cloud_sdk/agentgateway/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ class ClientConfig:
2929
token_expiry_buffer_seconds: float = DEFAULT_TOKEN_EXPIRY_BUFFER_SECONDS
3030
max_system_token_cache_size: int = DEFAULT_MAX_SYSTEM_TOKEN_CACHE_SIZE
3131
max_user_token_cache_size: int = DEFAULT_MAX_USER_TOKEN_CACHE_SIZE
32+
33+
def __post_init__(self) -> None:
34+
if self.token_expiry_buffer_seconds >= self.fallback_token_ttl_seconds:
35+
raise ValueError(
36+
f"token_expiry_buffer_seconds ({self.token_expiry_buffer_seconds}) "
37+
f"must be less than fallback_token_ttl_seconds ({self.fallback_token_ttl_seconds})"
38+
)

0 commit comments

Comments
 (0)