Skip to content
Merged
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
22 changes: 13 additions & 9 deletions aas_http_client/classes/client/aas_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,8 @@ def create_by_dict(
:return: An instance of AasHttpClient initialized with the provided parameters or None if validation fails
"""
_logger.info("Create AAS server http client from dictionary.")
config_string = json.dumps(configuration, indent=4)

return _create_client(config_string, basic_auth_password, o_auth_client_secret, bearer_auth_token)
return _create_client(configuration, basic_auth_password, o_auth_client_secret, bearer_auth_token)


def create_by_config(
Expand All @@ -393,31 +392,36 @@ def create_by_config(
config_file = config_file.resolve()
_logger.info(f"Create AAS server http client from configuration file '{config_file}'.")
if not config_file.exists():
config_string = "{}"
configuration = {}
_logger.warning(f"Configuration file '{config_file}' not found. Using default configuration.")
else:
config_string = config_file.read_text(encoding="utf-8")
try:
configuration = json.loads(config_string)
except json.JSONDecodeError as e:
_logger.error(f"Configuration file '{config_file}' is not a valid JSON file: {e}")
return None
_logger.debug(f"Configuration file '{config_file}' found.")

return _create_client(config_string, basic_auth_password, o_auth_client_secret, bearer_auth_token)
return _create_client(configuration, basic_auth_password, o_auth_client_secret, bearer_auth_token)


def _create_client(config_string: str, basic_auth_password: str, o_auth_client_secret: str, bearer_auth_token: str) -> AasHttpClient | None:
"""Create and initialize an AAS HTTP client from configuration string.
def _create_client(config_dict: dict, basic_auth_password: str, o_auth_client_secret: str, bearer_auth_token: str) -> AasHttpClient | None:
"""Create and initialize an AAS HTTP client from configuration dictionary.

This internal method validates the configuration, sets authentication credentials,
initializes the client, and tests the connection to the AAS server.

:param config_string: JSON configuration string containing AAS server settings
:param config_dict: Dictionary containing AAS server settings
:param basic_auth_password: Password for basic authentication, defaults to ""
:param o_auth_client_secret: Client secret for OAuth authentication, defaults to ""
:param bearer_auth_token: Bearer token for authentication, defaults to ""
:return: An initialized and connected AasHttpClient instance or None if connection fails
:raises ValidationError: If the configuration string is invalid
:raises ValidationError: If the configuration dictionary is invalid
:raises TimeoutError: If connection to the server times out
"""
try:
client = AasHttpClient.model_validate_json(config_string)
client = AasHttpClient.model_validate(config_dict)
except ValidationError as ve:
raise ValidationError(f"Invalid BaSyx server configuration file: {ve}") from ve

Expand Down
18 changes: 11 additions & 7 deletions aas_http_client/classes/wrapper/sdk_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ class SdkWrapper:
_client: AasHttpClient
base_url: str = ""

def __init__(self, config_string: str, basic_auth_password: str = "", o_auth_client_secret: str = "", bearer_auth_token: str = ""):
def __init__(self, configuration: dict, basic_auth_password: str = "", o_auth_client_secret: str = "", bearer_auth_token: str = ""):
"""Initializes the wrapper with the given configuration.

:param config_string: Configuration string for the BaSyx server connection.
:param configuration: Dictionary containing the BaSyx server connection settings.
:param basic_auth_password: Password for the BaSyx server interface client, defaults to "".
:param o_auth_client_secret: Client secret for OAuth authentication, defaults to "".
:param bearer_auth_token: Bearer token for authentication, defaults to "".
"""
client = _create_client(config_string, basic_auth_password, o_auth_client_secret, bearer_auth_token)
client = _create_client(configuration, basic_auth_password, o_auth_client_secret, bearer_auth_token)

if not client:
raise ValueError("Failed to create AAS HTTP client with the provided configuration.")
Expand Down Expand Up @@ -944,8 +944,7 @@ def create_by_dict(
:return: An instance of SdkWrapper initialized with the provided parameters or None if initialization fails
"""
_logger.debug("Create AAS server wrapper from dictionary.")
config_string = json.dumps(configuration, indent=4)
return SdkWrapper(config_string, basic_auth_password, o_auth_client_secret, bearer_auth_token)
return SdkWrapper(configuration, basic_auth_password, o_auth_client_secret, bearer_auth_token)


def create_by_config(
Expand All @@ -961,12 +960,17 @@ def create_by_config(
"""
_logger.debug(f"Create AAS wrapper client from configuration file '{config_file}'.")
if not config_file.exists():
config_string = "{}"
configuration = {}
_logger.warning(f"Configuration file '{config_file}' not found. Using default config.")
else:
config_string = config_file.read_text(encoding="utf-8")
try:
configuration = json.loads(config_string)
except json.JSONDecodeError as e:
_logger.error(f"Configuration file '{config_file}' is not a valid JSON file: {e}")
return None
_logger.debug(f"Configuration file '{config_file}' found.")
return SdkWrapper(config_string, basic_auth_password, o_auth_client_secret, bearer_auth_token)
return SdkWrapper(configuration, basic_auth_password, o_auth_client_secret, bearer_auth_token)


# endregion