Skip to content
Open
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: 1 addition & 1 deletion src/blueapi/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def from_config(cls, config: ApplicationConfig) -> Self:
session_manager = SessionManager.from_cache(config.auth_token_path)
except Exception:
... # Swallow exceptions
rest = BlueapiRestClient(config.api, session_manager=session_manager)
rest = BlueapiRestClient(config.api, token_retreiver=session_manager)
if config.stomp.enabled:
assert config.stomp.url.host is not None, "Stomp URL missing host"
assert config.stomp.url.port is not None, "Stomp URL missing port"
Expand Down
10 changes: 5 additions & 5 deletions src/blueapi/client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from blueapi import __version__
from blueapi.config import RestConfig
from blueapi.service.authentication import JWTAuth, SessionManager
from blueapi.service.authentication import JWTAuth, TokenRetriever
from blueapi.service.model import (
DeviceModel,
DeviceResponse,
Expand Down Expand Up @@ -157,16 +157,16 @@ def _response_json(response: requests.Response) -> Any:

class BlueapiRestClient:
_config: RestConfig
_session_manager: SessionManager | None
_token_retreiver: TokenRetriever | None
_pool: requests.Session

def __init__(
self,
config: RestConfig | None = None,
session_manager: SessionManager | None = None,
token_retreiver: TokenRetriever | None = None,
) -> None:
self._config = config or RestConfig()
self._session_manager = session_manager
self._token_retreiver = token_retreiver
self._pool = requests.Session()

def get_plans(self) -> PlanResponse:
Expand Down Expand Up @@ -283,7 +283,7 @@ def _request_and_deserialize(
json=data,
params=params,
headers=carr,
auth=JWTAuth(self._session_manager),
auth=JWTAuth(self._token_retreiver),
)
except requests.exceptions.ConnectionError as ce:
raise ServiceUnavailableError() from ce
Expand Down
8 changes: 6 additions & 2 deletions src/blueapi/service/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from functools import cached_property
from http import HTTPStatus
from pathlib import Path
from typing import Any, cast
from typing import Any, Protocol, cast

import httpx
import jwt
Expand All @@ -24,6 +24,10 @@
SCOPES = "openid offline_access"


class TokenRetriever(Protocol):
def get_valid_access_token(self) -> str: ...
Comment thread
tpoliaw marked this conversation as resolved.
Dismissed


class CacheManager(ABC):
@abstractmethod
def can_access_cache(self) -> bool: ...
Expand Down Expand Up @@ -238,7 +242,7 @@ def start_device_flow(self):


class JWTAuth(AuthBase):
def __init__(self, session_manager: SessionManager | None):
def __init__(self, session_manager: TokenRetriever | None):
self.token: str = (
session_manager.get_valid_access_token() if session_manager else ""
)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/client/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def rest() -> BlueapiRestClient:
@pytest.fixture
def rest_with_auth(oidc_config: OIDCConfig, tmp_path) -> BlueapiRestClient:
return BlueapiRestClient(
session_manager=SessionManager(
token_retreiver=SessionManager(
server_config=oidc_config,
cache_manager=SessionCacheManager(tmp_path / "blueapi_cache"),
)
Expand Down
Loading