from __future__ import annotations
import streamlit as st
from imednet import ImednetSDK
_KEY_API_KEY = "_imednet_api_key"
_KEY_SECURITY_KEY = "_imednet_security_key"
_KEY_STUDY_KEY = "_imednet_study_key"
_KEY_SDK = "_imednet_sdk"
_KEY_CONNECTED = "_imednet_connected"
__all__ = ["render_auth_sidebar", "get_sdk", "get_study_key", "clear_credentials"]
def _build_sdk(api_key: str, security_key: str) -> None:
"""Construct and cache an authenticated SDK instance."""
st.session_state[_KEY_SDK] = ImednetSDK(
api_key=api_key,
security_key=security_key,
)
def _mark_disconnected() -> None:
st.session_state[_KEY_CONNECTED] = False
st.session_state.pop(_KEY_SDK, None)
[docs]def get_sdk() -> ImednetSDK:
"""Return the authenticated SDK from session state.
Returns:
The connected :class:`imednet.ImednetSDK` instance.
Raises:
RuntimeError: If the user is not connected or no SDK is stored.
"""
sdk = st.session_state.get(_KEY_SDK)
if not st.session_state.get(_KEY_CONNECTED) or sdk is None:
raise RuntimeError(
"SDK is not connected. Ensure credentials are entered and Connect is clicked."
)
return sdk
[docs]def get_study_key() -> str:
"""Return the selected study key from session state.
Returns:
The selected study key string.
Raises:
RuntimeError: If no study key is present in session state.
"""
study_key = st.session_state.get(_KEY_STUDY_KEY)
if not isinstance(study_key, str) or not study_key:
raise RuntimeError("Study key is not set. Call render_auth_sidebar() first.")
return study_key
[docs]def clear_credentials() -> None:
"""Remove all authentication state from Streamlit session storage.
This removes the cached SDK instance, connection flag, and any credential
input values currently held in ``st.session_state``.
"""
for key in (_KEY_API_KEY, _KEY_SECURITY_KEY, _KEY_STUDY_KEY, _KEY_SDK, _KEY_CONNECTED):
st.session_state.pop(key, None)