1
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2026-03-30 13:28:07 +02:00

refactor(atlas-http-service): Rename AtlasEndpoints

This commit is contained in:
2026-03-16 16:15:03 +01:00
committed by Christian Appel
parent 0c6f3f8352
commit 2b0f575733
3 changed files with 25 additions and 27 deletions

View File

@@ -31,7 +31,7 @@ from bec_widgets.utils.toolbars.actions import (
from bec_widgets.utils.toolbars.bundles import ToolbarBundle
from bec_widgets.utils.toolbars.toolbar import ModularToolBar
from bec_widgets.widgets.services.bec_atlas_admin_view.bec_atlas_http_service import (
ATLAS_ENDPOINTS,
AtlasEndpoints,
AuthenticatedUserInfo,
BECAtlasHTTPService,
HTTPResponse,
@@ -475,10 +475,10 @@ class BECAtlasAdminView(BECWidget, QWidget):
logger.debug(
f"HTTP Response received: {response.request_url} with status {response.status}"
)
if ATLAS_ENDPOINTS.REALMS_EXPERIMENTS in response.request_url:
if AtlasEndpoints.REALMS_EXPERIMENTS in response.request_url:
experiments = response.data if isinstance(response.data, list) else []
self.experiment_selection.set_experiment_infos(experiments)
elif ATLAS_ENDPOINTS.SET_EXPERIMENT in response.request_url:
elif AtlasEndpoints.SET_EXPERIMENT in response.request_url:
self._on_overview_selected()
@SafeSlot(dict)

View File

@@ -29,7 +29,7 @@ from bec_widgets.utils.error_popups import SafeSlot
logger = bec_logger.logger
class ATLAS_ENDPOINTS(StrEnum):
class AtlasEndpoints(StrEnum):
"""Constants for BEC Atlas API endpoints."""
LOGIN = "/user/login"
@@ -202,7 +202,7 @@ class BECAtlasHTTPService(QWidget):
f"Expected response data to be a dict, list, or str for {request_url}, but got {type(data)}. Response content: {data}"
)
if ATLAS_ENDPOINTS.LOGIN.value in request_url:
if AtlasEndpoints.LOGIN.value in request_url:
# If it's a login response, don't forward the token
# but extract the expiration time and emit it
token = data.get("access_token")
@@ -211,11 +211,11 @@ class BECAtlasHTTPService(QWidget):
# Now we set the auth info, and then fetch the user info to get the groups
self.__set_auth_info(data)
self.get_user_info() # Fetch groups, then emit authenticated once groups are set on auth_user
elif ATLAS_ENDPOINTS.LOGOUT.value in request_url:
elif AtlasEndpoints.LOGOUT.value in request_url:
self._auth_timer.stop() # Stop the timer if it was running
self.__clear_login_info(skip_logout=True) # Skip calling logout again
self.authenticated.emit({})
elif ATLAS_ENDPOINTS.USER_INFO.value in request_url:
elif AtlasEndpoints.USER_INFO.value in request_url:
groups = data.get("groups", [])
email = data.get("email", "")
# Second step of authentication: We also have all groups now
@@ -229,7 +229,7 @@ class BECAtlasHTTPService(QWidget):
self.get_deployment_info(
deployment_id=self._current_deployment_info.deployment_id
)
elif ATLAS_ENDPOINTS.DEPLOYMENT_INFO.value in request_url:
elif AtlasEndpoints.DEPLOYMENT_INFO.value in request_url:
owner_groups = data.get("owner_groups", [])
if self.auth_user_info is not None and not self.auth_user_info.groups.isdisjoint(
owner_groups
@@ -331,14 +331,14 @@ class BECAtlasHTTPService(QWidget):
password (str): The password for authentication.
"""
self._post_request(
endpoint=ATLAS_ENDPOINTS.LOGIN.value,
endpoint=AtlasEndpoints.LOGIN.value,
payload={"username": username, "password": password},
)
@SafeSlot(popup_error=True)
def logout(self):
"""Logout from BEC Atlas."""
self._post_request(endpoint=ATLAS_ENDPOINTS.LOGOUT.value)
self._post_request(endpoint=AtlasEndpoints.LOGOUT.value)
@SafeSlot(str, popup_error=True)
def get_experiments_for_realm(self, realm_id: str):
@@ -349,7 +349,7 @@ class BECAtlasHTTPService(QWidget):
realm_id (str): The ID of the realm to retrieve experiments for.
"""
self._get_request(
endpoint=ATLAS_ENDPOINTS.REALMS_EXPERIMENTS.value,
endpoint=AtlasEndpoints.REALMS_EXPERIMENTS.value,
query_parameters={"realm_id": realm_id},
)
@@ -363,14 +363,14 @@ class BECAtlasHTTPService(QWidget):
deployment_id (str): The ID of the deployment associated with the experiment.
"""
self._post_request(
endpoint=ATLAS_ENDPOINTS.SET_EXPERIMENT.value,
endpoint=AtlasEndpoints.SET_EXPERIMENT.value,
query_parameters={"experiment_id": experiment_id, "deployment_id": deployment_id},
)
@SafeSlot(popup_error=True)
def get_user_info(self):
"""Get the current user information from BEC Atlas. Requires authentication."""
self._get_request(endpoint=ATLAS_ENDPOINTS.USER_INFO.value)
self._get_request(endpoint=AtlasEndpoints.USER_INFO.value)
@SafeSlot(str, popup_error=True)
def get_deployment_info(self, deployment_id: str):
@@ -381,6 +381,6 @@ class BECAtlasHTTPService(QWidget):
deployment_id (str): The ID of the deployment to retrieve information for.
"""
self._get_request(
endpoint=ATLAS_ENDPOINTS.DEPLOYMENT_INFO.value,
endpoint=AtlasEndpoints.DEPLOYMENT_INFO.value,
query_parameters={"deployment_id": deployment_id},
)

View File

@@ -17,7 +17,7 @@ from qtpy.QtNetwork import QNetworkRequest
from bec_widgets.widgets.services.bec_atlas_admin_view.bec_atlas_admin_view import BECAtlasAdminView
from bec_widgets.widgets.services.bec_atlas_admin_view.bec_atlas_http_service import (
ATLAS_ENDPOINTS,
AtlasEndpoints,
AuthenticatedUserInfo,
BECAtlasHTTPError,
BECAtlasHTTPService,
@@ -102,7 +102,7 @@ class TestBECAtlasHTTPService:
with mock.patch.object(http_service.network_manager, "get") as mock_get:
http_service._get_request(
endpoint=ATLAS_ENDPOINTS.REALMS_EXPERIMENTS.value,
endpoint=AtlasEndpoints.REALMS_EXPERIMENTS.value,
query_parameters={"realm_id": "realm-1"},
)
@@ -118,8 +118,7 @@ class TestBECAtlasHTTPService:
with mock.patch.object(http_service.network_manager, "post") as mock_post:
http_service._post_request(
endpoint=ATLAS_ENDPOINTS.LOGIN.value,
payload={"username": "alice", "password": "pw"},
endpoint=AtlasEndpoints.LOGIN.value, payload={"username": "alice", "password": "pw"}
)
mock_post.assert_called_once()
@@ -133,13 +132,13 @@ class TestBECAtlasHTTPService:
with mock.patch.object(http_service, "_get_request") as mock_get:
# User info
http_service.get_user_info()
mock_get.assert_called_once_with(endpoint=ATLAS_ENDPOINTS.USER_INFO.value)
mock_get.assert_called_once_with(endpoint=AtlasEndpoints.USER_INFO.value)
mock_get.reset_mock()
# Deployment info
http_service.get_deployment_info("dep-1")
mock_get.assert_called_once_with(
endpoint=ATLAS_ENDPOINTS.DEPLOYMENT_INFO.value,
endpoint=AtlasEndpoints.DEPLOYMENT_INFO.value,
query_parameters={"deployment_id": "dep-1"},
)
@@ -147,28 +146,27 @@ class TestBECAtlasHTTPService:
# Realms experiments
http_service.get_experiments_for_realm("realm-1")
mock_get.assert_called_once_with(
endpoint=ATLAS_ENDPOINTS.REALMS_EXPERIMENTS.value,
endpoint=AtlasEndpoints.REALMS_EXPERIMENTS.value,
query_parameters={"realm_id": "realm-1"},
)
with mock.patch.object(http_service, "_post_request") as mock_post:
# Logout
http_service.logout()
mock_post.assert_called_once_with(endpoint=ATLAS_ENDPOINTS.LOGOUT.value)
mock_post.assert_called_once_with(endpoint=AtlasEndpoints.LOGOUT.value)
mock_post.reset_mock()
# Login
http_service.login("alice", "pw")
mock_post.assert_called_once_with(
endpoint=ATLAS_ENDPOINTS.LOGIN.value,
payload={"username": "alice", "password": "pw"},
endpoint=AtlasEndpoints.LOGIN.value, payload={"username": "alice", "password": "pw"}
)
mock_post.reset_mock()
# Set experiment
http_service.set_experiment("exp-1", "dep-1")
mock_post.assert_called_once_with(
endpoint=ATLAS_ENDPOINTS.SET_EXPERIMENT.value,
endpoint=AtlasEndpoints.SET_EXPERIMENT.value,
query_parameters={"experiment_id": "exp-1", "deployment_id": "dep-1"},
)
@@ -700,7 +698,7 @@ class TestBECAtlasAdminView:
):
"""Test that _on_http_response_received correctly handles HTTP responses and updates the UI accordingly."""
realms = HTTPResponse(
request_url=f"{admin_view._atlas_url}/{ATLAS_ENDPOINTS.REALMS_EXPERIMENTS}/experiments?realm_id=TestBeamline",
request_url=f"{admin_view._atlas_url}/{AtlasEndpoints.REALMS_EXPERIMENTS}/experiments?realm_id=TestBeamline",
status=200,
headers={"content-type": "application/json"},
data=experiment_info_list,
@@ -712,7 +710,7 @@ class TestBECAtlasAdminView:
mock_set_experiment_infos.assert_called_once_with(experiment_info_list)
set_experiment = HTTPResponse(
request_url=f"{admin_view._atlas_url}/{ATLAS_ENDPOINTS.SET_EXPERIMENT}",
request_url=f"{admin_view._atlas_url}/{AtlasEndpoints.SET_EXPERIMENT}",
status=200,
headers={"content-type": "application/json"},
data={},