Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make DataFrame and Spec clients compatible with SystemLink Client http configuration #61

Merged
merged 6 commits into from
Jun 18, 2024
31 changes: 31 additions & 0 deletions nisystemlink/clients/core/_http_configuration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class HttpConfigurationManager:
HTTP_LOCALHOST_CONFIGURATION_ID = "SYSTEMLINK_LOCALHOST"
"""The default ID of the SystemLink Server's configuration on the SystemLink Server itself."""

_HTTP_JUPYTER_CONFIGURATION_ID = "SYSTEMLINK_VIRTUAL_JUPYTER"
"""Virtual ID of SystemLink Server's configuration for Jupyter Notebook execution on SLE."""

_configs = None
_virtual_configs = None

@classmethod
def get_configuration(
Expand Down Expand Up @@ -78,14 +82,41 @@ def _fallback(cls) -> Optional[core.HttpConfiguration]:
"""
if cls._configs is None:
cls._configs = cls._read_configurations()
if cls._virtual_configs is None:
cls._virtual_configs = cls._read_virtual_configurations()

master_config = cls._configs.get(cls.HTTP_MASTER_CONFIGURATION_ID)
if master_config is not None:
return master_config
localhost_config = cls._configs.get(cls.HTTP_LOCALHOST_CONFIGURATION_ID)
if localhost_config is not None:
return localhost_config

jupyter_config = cls._virtual_configs.get(cls._HTTP_JUPYTER_CONFIGURATION_ID)
if jupyter_config is not None:
return jupyter_config

return None

@classmethod
def _read_virtual_configurations(cls) -> Dict[str, core.HttpConfiguration]:
"""Loads the virtual HTTP configurations.

Returns:
A dictionary mapping each loaded configuration ID to its corresponding
:class:`HttpConfiguration`.
"""
configurations = {} # type: Dict[str, core.HttpConfiguration]
try:
configurations[cls._HTTP_JUPYTER_CONFIGURATION_ID] = (
core.JupyterHttpConfiguration()
)
except KeyError:
# Env variables for Jupyter notebook execution are not available.
pass

return configurations

@classmethod
def _read_configurations(cls) -> Dict[str, core.HttpConfiguration]:
"""Discover and loads the HTTP configuration files.
Expand Down
8 changes: 4 additions & 4 deletions nisystemlink/clients/dataframe/_data_frame_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def __init__(self, configuration: Optional[core.HttpConfiguration] = None):

Args:
configuration: Defines the web server to connect to and information about
how to connect. If not provided, an instance of
:class:`JupyterHttpConfiguration <nisystemlink.clients.core.JupyterHttpConfiguration>`
is used.
how to connect. If not provided, the
:class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>`
is used to obtain the configuration.

Raises:
ApiException: if unable to communicate with the DataFrame Service.
"""
if configuration is None:
configuration = core.JupyterHttpConfiguration()
configuration = core.HttpConfigurationManager.get_configuration()

super().__init__(configuration, "/nidataframe/v1/")

Expand Down
14 changes: 13 additions & 1 deletion nisystemlink/clients/spec/_spec_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@

class SpecClient(BaseClient):
def __init__(self, configuration: Optional[core.HttpConfiguration]):
"""Initialize an instance.

Args:
configuration: Defines the web server to connect to and information about
how to connect. If not provided, the
:class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>`
is used to obtain the configuration.

Raises:
ApiException: if unable to communicate with the Spec Service.
"""
if configuration is None:
configuration = core.JupyterHttpConfiguration()
configuration = core.HttpConfigurationManager.get_configuration()

super().__init__(configuration, base_path="/nispec/v1/")

@get("")
Expand Down
Loading