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
64 changes: 27 additions & 37 deletions src/unstract/sdk/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
from typing import Any, Optional

import requests
from requests.exceptions import ConnectionError, HTTPError

from unstract.sdk.constants import AdapterKeys, LogLevel, ToolEnv
from unstract.sdk.adapters.utils import AdapterUtils
from unstract.sdk.constants import AdapterKeys, ToolEnv
from unstract.sdk.exceptions import SdkError
from unstract.sdk.helper import SdkHelper
from unstract.sdk.platform import PlatformBase
from unstract.sdk.tool.base import BaseTool
Expand Down Expand Up @@ -37,52 +40,46 @@ def __init__(
tool=tool, platform_host=platform_host, platform_port=platform_port
)

def get_adapter_configuration(
def _get_adapter_configuration(
self,
adapter_instance_id: str,
) -> Optional[dict[str, Any]]:
) -> dict[str, Any]:
"""Get Adapter
1. Get the adapter config from platform service
using the adapter_instance_id

Args:
adapter_instance_id (str): adapter Instance Id
adapter_instance_id (str): Adapter instance ID

Returns:
Any: _description_
dict[str, Any]: Config stored for the adapter
"""
url = f"{self.base_url}/adapter_instance"
query_params = {AdapterKeys.ADAPTER_INSTANCE_ID: adapter_instance_id}
headers = {"Authorization": f"Bearer {self.bearer_token}"}
response = requests.get(url, headers=headers, params=query_params)
if response.status_code == 200:
try:
response = requests.get(url, headers=headers, params=query_params)
response.raise_for_status()
adapter_data: dict[str, Any] = response.json()

# TODO: Print config after redacting sensitive information
self.tool.stream_log(
"Successfully retrieved adapter config "
f"for adapter: {adapter_instance_id}"
"Successfully retrieved config "
f"for adapter instance {adapter_instance_id}"
)

return adapter_data

elif response.status_code == 404:
self.tool.stream_log(
f"adapter not found for: for adapter instance" f"{adapter_instance_id}",
level=LogLevel.ERROR,
except ConnectionError:
raise SdkError(
"Unable to connect to platform service, please contact the admin."
)
return None

else:
self.tool.stream_log(
(
f"Error while retrieving adapter "
"for adapter instance: "
f"{adapter_instance_id} / {response.reason}"
),
level=LogLevel.ERROR,
except HTTPError as e:
default_err = (
"Error while calling the platform service, please contact the admin."
)
msg = AdapterUtils.get_msg_from_request_exc(
err=e, message_key="error", default_err=default_err
)
return None
raise SdkError(f"Error while retrieving adapter. {msg}")
return adapter_data

@staticmethod
def get_adapter_config(
Expand All @@ -96,13 +93,13 @@ def get_adapter_config(
platform service to retrieve the configuration.

Args:
adapter_instance_id (str): ID of the adapter instance
tool (AbstractTool): Instance of AbstractTool
adapter_instance_id (str): ID of the adapter instance
Required env variables:
PLATFORM_HOST: Host of platform service
PLATFORM_PORT: Port of platform service
Returns:
Any: engine
dict[str, Any]: Config stored for the adapter
"""
# Check if the adapter ID matches any public adapter keys
if SdkHelper.is_public_adapter(adapter_id=adapter_instance_id):
Expand All @@ -120,11 +117,4 @@ def get_adapter_config(
platform_host=platform_host,
platform_port=platform_port,
)
adapter_metadata: Optional[
dict[str, Any]
] = tool_adapter.get_adapter_configuration(adapter_instance_id)
if not adapter_metadata:
tool.stream_error_and_exit(
f"Adapter not found for " f"adapter instance: {adapter_instance_id}"
)
return adapter_metadata
return tool_adapter._get_adapter_configuration(adapter_instance_id)
2 changes: 1 addition & 1 deletion src/unstract/sdk/adapters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_msg_from_request_exc(
if message_key in err_json:
return str(err_json[message_key])
elif err_response.headers["Content-Type"] == "text/plain":
return err.response.text # type: ignore
return err_response.text # type: ignore
return default_err

@staticmethod
Expand Down
8 changes: 5 additions & 3 deletions src/unstract/sdk/tool/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ def execute_run(self, args: argparse.Namespace) -> None:
self.tool.stream_error_and_exit("--settings are required for RUN command")
settings: dict[str, Any] = loads(args.settings)

tool_name = self.tool.properties["displayName"]
self.tool.stream_log(
f"Running tool with "
f"Running tool '{tool_name}' with "
f"Workflow ID: {self.tool.workflow_id}, "
f"Execution ID: {self.tool.execution_id}, "
f"SDK Version: {get_sdk_version()}"
Expand All @@ -72,7 +73,8 @@ def execute_run(self, args: argparse.Namespace) -> None:
output_dir=self.tool.get_output_dir(),
)
except Exception as e:
logger.error(f"Error while tool run: {e}", stack_info=True, exc_info=True)
self.tool.stream_error_and_exit(f"Error while running tool: {str(e)}")
msg = f"Error while running tool '{tool_name}': {str(e)}"
logger.error(msg, stack_info=True, exc_info=True)
self.tool.stream_error_and_exit(msg)

# TODO: Call tool method to validate if output was written
2 changes: 1 addition & 1 deletion src/unstract/sdk/tool/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def get_env_or_die(self, env_key: str) -> str:
"""
env_value = os.environ.get(env_key)
if env_value is None or env_value == "":
self.stream_error_and_exit(f"Env variable {env_key} is required")
self.stream_error_and_exit(f"Env variable '{env_key}' is required")
return env_value

@staticmethod
Expand Down