Skip to content

Commit 08a3233

Browse files
committed
fix: Update rest session manager when logging in
If the user is not logged in when the client is created, the rest client does not get a session manager. When the user calls login, a new session manager is created to cache the new token but if the rest client is not updated, any subsequent calls remain unauthenticated. Setting the rest client's session manager to the new instance when a login is successful means the tokens can be used without recreating the client. Now that the login method works as expected, the client created in the `just repl` task can now automatically refresh its login if required.
1 parent f6eebe6 commit 08a3233

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ repl:
4040
uv run --with ptpython ptpython -i <(cat << EOF
4141
from blueapi.client import BlueapiClient
4242
bc = BlueapiClient.from_config_file("tests/system_tests/config.yaml").with_instrument_session("cm12345-1")
43+
bc.login()
4344
EOF
4445
)

src/blueapi/client/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,11 @@ def login(self, token_path: Path | None = None):
770770
auth = SessionManager(
771771
oidc, cache_manager=SessionCacheManager(token_path)
772772
)
773-
auth.start_device_flow()
773+
try:
774+
auth.start_device_flow()
775+
self._rest.session_manager = auth
776+
except KeyboardInterrupt:
777+
pass
774778
else:
775779
print("Server is not configured to use authentication!")
776780

src/blueapi/client/rest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def _response_json(response: requests.Response) -> Any:
186186

187187
class BlueapiRestClient:
188188
_config: RestConfig
189-
_session_manager: SessionManager | None
189+
session_manager: SessionManager | None
190190
_pool: requests.Session
191191

192192
def __init__(
@@ -195,7 +195,7 @@ def __init__(
195195
session_manager: SessionManager | None = None,
196196
) -> None:
197197
self._config = config or RestConfig()
198-
self._session_manager = session_manager
198+
self.session_manager = session_manager
199199
self._pool = requests.Session()
200200

201201
def get_plans(self) -> PlanResponse:
@@ -315,7 +315,7 @@ def _request_and_deserialize(
315315
json=data,
316316
params=params,
317317
headers=carr,
318-
auth=JWTAuth(self._session_manager),
318+
auth=JWTAuth(self.session_manager),
319319
)
320320
except requests.exceptions.ConnectionError as ce:
321321
raise ServiceUnavailableError() from ce

0 commit comments

Comments
 (0)