From 3cd2ce2f69fbf8c8ea3b4bca742dc0ab4765871a Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 18 Feb 2025 11:52:27 -0500 Subject: [PATCH 01/12] Edit autogenerated endpoints to fix server_url issue --- .../_hooks/custom/clean_server_url_hook.py | 62 +++++--- src/unstructured_client/destinations.py | 111 +++++++++------ src/unstructured_client/general.py | 24 ++-- src/unstructured_client/jobs.py | 67 +++++---- src/unstructured_client/sources.py | 111 +++++++++------ src/unstructured_client/workflows.py | 133 +++++++++++------- 6 files changed, 332 insertions(+), 176 deletions(-) diff --git a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py index 07ce1ca0..eb8f6580 100644 --- a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py +++ b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py @@ -7,31 +7,61 @@ from unstructured_client.httpclient import HttpClient -class CleanServerUrlSDKInitHook(SDKInitHook): - """Hook fixing common mistakes by users in defining `server_url` in the unstructured-client""" +def clean_server_url(base_url: str) -> str: + """Fix url scheme and remove the '/general/v0/general' path.""" + + if not base_url: + return "" + # -- add a url scheme if not present (urllib.parse does not work reliably without it) + if "http" not in base_url: + base_url = "http://" + base_url + + parsed_url: ParseResult = urlparse(base_url) + + if "api.unstructuredapp.io" in base_url: + if parsed_url.scheme != "https": + parsed_url = parsed_url._replace(scheme="https") + + # -- path should always be empty + return urlunparse(parsed_url._replace(path="")) - def clean_server_url(self, base_url: str) -> str: - """Fix url scheme and remove the '/general/v0/general' path.""" - if not base_url: - return "" - # -- add a url scheme if not present (urllib.parse does not work reliably without it) - if "http" not in base_url: - base_url = "http://" + base_url +def choose_server_url(endpoint_url, client_url, default_endpoint_url) -> str: + """ + Helper function to fix a breaking change in the SDK past 0.30.0. + When we merged the partition and platform specs, the client server_url stopped working, + and users need to pass it in the endpoint function. + For now, call this helper in the generated code to set the correct url. + """ - parsed_url: ParseResult = urlparse(base_url) + # First, see if the endpoint has a url: + # s.general.partition(server_url=...) + if endpoint_url is not None: + url = endpoint_url - if "api.unstructuredapp.io" in base_url: - if parsed_url.scheme != "https": - parsed_url = parsed_url._replace(scheme="https") + # Next, try the base client url: + # s = UnstructuredClient(server_url=...) + # (If not set it's an empty string) + elif client_url != "": + url = client_url - # -- path should always be empty - return urlunparse(parsed_url._replace(path="")) + # Finally, take the url defined in the spec: + # operations.PARTITION_SERVERS[...] + else: + url = default_endpoint_url + + # Make sure we drop the path if it's provided anywhere + # (The endpoint url will be set after we've done the init hooks) + return clean_server_url(url) + + +class CleanServerUrlSDKInitHook(SDKInitHook): + """Hook fixing common mistakes by users in defining `server_url` in the unstructured-client""" def sdk_init( self, base_url: str, client: HttpClient ) -> Tuple[str, HttpClient]: """Concrete implementation for SDKInitHook.""" - cleaned_url = self.clean_server_url(base_url) + cleaned_url = clean_server_url(base_url) return cleaned_url, client diff --git a/src/unstructured_client/destinations.py b/src/unstructured_client/destinations.py index 3f6d39f5..3c838d45 100644 --- a/src/unstructured_client/destinations.py +++ b/src/unstructured_client/destinations.py @@ -4,6 +4,7 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext +from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -34,12 +35,15 @@ def create_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CREATE_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CREATE_DESTINATION_SERVERS[ operations.CREATE_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateDestinationRequest) @@ -152,12 +156,15 @@ async def create_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CREATE_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CREATE_DESTINATION_SERVERS[ operations.CREATE_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateDestinationRequest) @@ -270,12 +277,15 @@ def delete_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.DELETE_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.DELETE_DESTINATION_SERVERS[ operations.DELETE_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteDestinationRequest) @@ -379,12 +389,15 @@ async def delete_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.DELETE_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=DELETE_DESTINATION_SERVERS[ operations.DELETE_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteDestinationRequest) @@ -487,12 +500,15 @@ def get_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_DESTINATION_SERVERS[ operations.GET_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetDestinationRequest) @@ -597,12 +613,15 @@ async def get_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_DESTINATION_SERVERS[ operations.GET_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetDestinationRequest) @@ -708,12 +727,15 @@ def list_destinations( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_DESTINATIONS_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_DESTINATIONS_SERVERS[ operations.LIST_DESTINATIONS_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListDestinationsRequest) @@ -820,12 +842,15 @@ async def list_destinations_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_DESTINATIONS_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_DESTINATIONS_SERVERS[ operations.LIST_DESTINATIONS_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListDestinationsRequest) @@ -932,12 +957,15 @@ def update_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.UPDATE_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.UPDATE_DESTINATION_SERVERS[ operations.UPDATE_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateDestinationRequest) @@ -1050,12 +1078,15 @@ async def update_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.UPDATE_DESTINATION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.UPDATE_DESTINATION_SERVERS[ operations.UPDATE_DESTINATION_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateDestinationRequest) diff --git a/src/unstructured_client/general.py b/src/unstructured_client/general.py index d3b7a04a..eadad0a8 100644 --- a/src/unstructured_client/general.py +++ b/src/unstructured_client/general.py @@ -5,6 +5,7 @@ from typing import Any, Dict, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext +from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -43,12 +44,15 @@ def partition( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.PARTITION_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.PARTITION_SERVERS[ operations.PARTITION_SERVER_SAAS_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.PartitionRequest) @@ -171,12 +175,16 @@ async def partition_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.PARTITION_SERVERS[ + + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.PARTITION_SERVERS[ operations.PARTITION_SERVER_SAAS_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.PartitionRequest) diff --git a/src/unstructured_client/jobs.py b/src/unstructured_client/jobs.py index feb5c168..060a898f 100644 --- a/src/unstructured_client/jobs.py +++ b/src/unstructured_client/jobs.py @@ -4,6 +4,7 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext +from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -33,12 +34,15 @@ def cancel_job( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CANCEL_JOB_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CANCEL_JOB_SERVERS[ operations.CANCEL_JOB_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CancelJobRequest) @@ -141,12 +145,15 @@ async def cancel_job_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CANCEL_JOB_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CANCEL_JOB_SERVERS[ operations.CANCEL_JOB_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CancelJobRequest) @@ -247,12 +254,15 @@ def get_job( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_JOB_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_JOB_SERVERS[ operations.GET_JOB_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetJobRequest) @@ -355,12 +365,15 @@ async def get_job_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_JOB_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_JOB_SERVERS[ operations.GET_JOB_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetJobRequest) @@ -463,12 +476,15 @@ def list_jobs( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_JOBS_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_JOBS_SERVERS[ operations.LIST_JOBS_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListJobsRequest) @@ -571,12 +587,15 @@ async def list_jobs_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_JOBS_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_JOBS_SERVERS[ operations.LIST_JOBS_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListJobsRequest) diff --git a/src/unstructured_client/sources.py b/src/unstructured_client/sources.py index fc9a17cb..a69618de 100644 --- a/src/unstructured_client/sources.py +++ b/src/unstructured_client/sources.py @@ -4,6 +4,7 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext +from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -33,12 +34,15 @@ def create_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CREATE_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CREATE_SOURCE_SERVERS[ operations.CREATE_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateSourceRequest) @@ -150,12 +154,15 @@ async def create_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CREATE_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CREATE_SOURCE_SERVERS[ operations.CREATE_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateSourceRequest) @@ -267,12 +274,15 @@ def delete_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.DELETE_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.DELETE_SOURCE_SERVERS[ operations.DELETE_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteSourceRequest) @@ -375,12 +385,15 @@ async def delete_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.DELETE_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.DELETE_SOURCE_SERVERS[ operations.DELETE_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteSourceRequest) @@ -483,12 +496,15 @@ def get_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_SOURCE_SERVERS[ operations.GET_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetSourceRequest) @@ -593,12 +609,15 @@ async def get_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_SOURCE_SERVERS[ operations.GET_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetSourceRequest) @@ -703,12 +722,15 @@ def list_sources( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_SOURCES_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_SOURCES_SERVERS[ operations.LIST_SOURCES_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListSourcesRequest) @@ -813,12 +835,15 @@ async def list_sources_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_SOURCES_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_SOURCES_SERVERS[ operations.LIST_SOURCES_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListSourcesRequest) @@ -923,12 +948,15 @@ def update_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.UPDATE_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.UPDATE_SOURCE_SERVERS[ operations.UPDATE_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateSourceRequest) @@ -1040,12 +1068,15 @@ async def update_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.UPDATE_SOURCE_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.UPDATE_SOURCE_SERVERS[ operations.UPDATE_SOURCE_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateSourceRequest) diff --git a/src/unstructured_client/workflows.py b/src/unstructured_client/workflows.py index f7293cd7..80cada82 100644 --- a/src/unstructured_client/workflows.py +++ b/src/unstructured_client/workflows.py @@ -4,6 +4,7 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext +from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -33,12 +34,15 @@ def create_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CREATE_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CREATE_WORKFLOW_SERVERS[ operations.CREATE_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateWorkflowRequest) @@ -146,12 +150,15 @@ async def create_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.CREATE_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.CREATE_WORKFLOW_SERVERS[ operations.CREATE_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateWorkflowRequest) @@ -259,12 +266,15 @@ def delete_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.DELETE_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.DELETE_WORKFLOW_SERVERS[ operations.DELETE_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteWorkflowRequest) @@ -367,12 +377,15 @@ async def delete_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.DELETE_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.DELETE_WORKFLOW_SERVERS[ operations.DELETE_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteWorkflowRequest) @@ -475,12 +488,15 @@ def get_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_WORKFLOW_SERVERS[ operations.GET_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetWorkflowRequest) @@ -585,12 +601,15 @@ async def get_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.GET_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.GET_WORKFLOW_SERVERS[ operations.GET_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetWorkflowRequest) @@ -695,12 +714,15 @@ def list_workflows( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_WORKFLOWS_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_WORKFLOWS_SERVERS[ operations.LIST_WORKFLOWS_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListWorkflowsRequest) @@ -805,12 +827,15 @@ async def list_workflows_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.LIST_WORKFLOWS_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.LIST_WORKFLOWS_SERVERS[ operations.LIST_WORKFLOWS_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListWorkflowsRequest) @@ -915,12 +940,15 @@ def run_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.RUN_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.RUN_WORKFLOW_SERVERS[ operations.RUN_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.RunWorkflowRequest) @@ -1025,12 +1053,15 @@ async def run_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.RUN_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.RUN_WORKFLOW_SERVERS[ operations.RUN_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.RunWorkflowRequest) @@ -1135,12 +1166,15 @@ def update_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.UPDATE_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.UPDATE_WORKFLOW_SERVERS[ operations.UPDATE_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateWorkflowRequest) @@ -1248,12 +1282,15 @@ async def update_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - if server_url is not None: - base_url = server_url - else: - base_url = operations.UPDATE_WORKFLOW_SERVERS[ + client_url, *_ = self.sdk_configuration.get_server_details() + + base_url = choose_server_url( + endpoint_url=server_url, + client_url=client_url, + default_endpoint_url=operations.UPDATE_WORKFLOW_SERVERS[ operations.UPDATE_WORKFLOW_SERVER_PLATFORM_API ] + ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateWorkflowRequest) From 8c5b8f8f4938828917e9ddd92c7e460ef4a3db2a Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 18 Feb 2025 11:52:49 -0500 Subject: [PATCH 02/12] Add unit tests for all server_url cases --- .../unit/test_server_urls.py | 245 ++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 _test_unstructured_client/unit/test_server_urls.py diff --git a/_test_unstructured_client/unit/test_server_urls.py b/_test_unstructured_client/unit/test_server_urls.py new file mode 100644 index 00000000..cf3a9926 --- /dev/null +++ b/_test_unstructured_client/unit/test_server_urls.py @@ -0,0 +1,245 @@ +import httpx +import pytest + +from unstructured_client.models import operations +from unstructured_client import UnstructuredClient, basesdk, utils + + +# Raise from our mock so we can get out of the client code +class StopClientException(Exception): + pass + + +@pytest.mark.parametrize( + "method_name", + [ + ("general.partition"), + ("destinations.create_destination"), + ("destinations.delete_destination"), + ("destinations.get_destination"), + ("destinations.list_destinations"), + ("destinations.update_destination"), + ("jobs.cancel_job"), + ("jobs.get_job"), + ("jobs.list_jobs"), + ("sources.create_source"), + ("sources.delete_source"), + ("sources.get_source"), + ("sources.list_sources"), + ("sources.update_source"), + ("workflows.create_workflow"), + ("workflows.delete_workflow"), + ("workflows.get_workflow"), + ("workflows.list_workflows"), + ("workflows.run_workflow"), + ("workflows.update_workflow"), + ], +) +def test_endpoint_uses_correct_url(monkeypatch, method_name): + # Mock this to get past param validation + def mock_unmarshal(*args, **kwargs): + return {} + + monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) + + print(method_name) + # Use these in the mock + server_url = "http://localhost:8000" + assertion_message = "" + + # Assert that the correct base_url makes it to here + def mock_build_request(*args, base_url, **kwargs): + nonlocal assertion_message + nonlocal server_url + + assert base_url == server_url, assertion_message + raise StopClientException # We're good, let's bail + + endpoint_class_name, endpoint_method_name = method_name.split(".") + + # Test 1 + # Pass server_url to the client, no path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to client and ignored" + s = UnstructuredClient(server_url="http://localhost:8000") + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + endpoint_method(request={}) + + # Test 2 + # Pass server_url to the client, with path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to client and was not stripped" + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + endpoint_method(request={}) + + # Test 3 + # Pass server_url to the endpoint, no path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to endpoint and ignored" + s = UnstructuredClient() + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + endpoint_method( + request={}, + server_url="http://localhost:8000", + ) + + # Test 4 + # Pass server_url to the endpoint, with path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to endpoint and was not stripped" + s = UnstructuredClient() + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + endpoint_method( + request={}, + server_url="http://localhost:8000/my/endpoint", + ) + + # Test 5 + # No server_url, should take the default + with pytest.raises(StopClientException): + assertion_message = "Default url was not used" + server_url = "https://api.unstructuredapp.io" if method_name == "general.partition" else "https://platform.unstructuredapp.io" + s = UnstructuredClient() + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + endpoint_method( + request={}, + ) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "method_name", + [ + ("general.partition_async"), + ("destinations.create_destination_async"), + ("destinations.delete_destination_async"), + ("destinations.get_destination_async"), + ("destinations.list_destinations_async"), + ("destinations.update_destination_async"), + ("jobs.cancel_job_async"), + ("jobs.get_job_async"), + ("jobs.list_jobs_async"), + ("sources.create_source_async"), + ("sources.delete_source_async"), + ("sources.get_source_async"), + ("sources.list_sources_async"), + ("sources.update_source_async"), + ("workflows.create_workflow_async"), + ("workflows.delete_workflow_async"), + ("workflows.get_workflow_async"), + ("workflows.list_workflows_async"), + ("workflows.run_workflow_async"), + ("workflows.update_workflow_async"), + ], +) +async def test_async_endpoint_uses_correct_url(monkeypatch, method_name): + # Mock this to get past param validation + def mock_unmarshal(*args, **kwargs): + return {} + + monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) + + print(method_name) + # Use these in the mock + server_url = "http://localhost:8000" + assertion_message = "" + + # Assert that the correct base_url makes it to here + def mock_build_request(*args, base_url, **kwargs): + nonlocal assertion_message + nonlocal server_url + + assert base_url == server_url, assertion_message + raise StopClientException # We're good, let's bail + + endpoint_class_name, endpoint_method_name = method_name.split(".") + + # Test 1 + # Pass server_url to the client, no path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to client and ignored" + s = UnstructuredClient(server_url="http://localhost:8000") + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + await endpoint_method(request={}) + + # Test 2 + # Pass server_url to the client, with path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to client and was not stripped" + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + await endpoint_method(request={}) + + # Test 3 + # Pass server_url to the endpoint, no path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to endpoint and ignored" + s = UnstructuredClient() + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + await endpoint_method( + request={}, + server_url="http://localhost:8000", + ) + + # Test 4 + # Pass server_url to the endpoint, with path + with pytest.raises(StopClientException): + assertion_message = "server_url was passed to endpoint and was not stripped" + s = UnstructuredClient() + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + await endpoint_method( + request={}, + server_url="http://localhost:8000/my/endpoint", + ) + + # Test 5 + # No server_url, should take the default + with pytest.raises(StopClientException): + assertion_message = "Default url was not used" + server_url = "https://api.unstructuredapp.io" if method_name == "general.partition" else "https://platform.unstructuredapp.io" + s = UnstructuredClient() + + endpoint_class = getattr(s, endpoint_class_name) + endpoint_method = getattr(endpoint_class, endpoint_method_name) + + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + await endpoint_method( + request={}, + ) From 94b3b9fe55fd2a25e1804c17f18c2e244f7bb98f Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 18 Feb 2025 11:59:12 -0500 Subject: [PATCH 03/12] Fix typo --- src/unstructured_client/destinations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unstructured_client/destinations.py b/src/unstructured_client/destinations.py index 3c838d45..a2dfe022 100644 --- a/src/unstructured_client/destinations.py +++ b/src/unstructured_client/destinations.py @@ -394,7 +394,7 @@ async def delete_destination_async( base_url = choose_server_url( endpoint_url=server_url, client_url=client_url, - default_endpoint_url=DELETE_DESTINATION_SERVERS[ + default_endpoint_url=operations.DELETE_DESTINATION_SERVERS[ operations.DELETE_DESTINATION_SERVER_PLATFORM_API ] ) From a38e44663a17b77bed05c6e6e27718102677de72 Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 18 Feb 2025 12:22:46 -0500 Subject: [PATCH 04/12] Use the correct url in contract tests --- _test_contract/platform_api/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_test_contract/platform_api/conftest.py b/_test_contract/platform_api/conftest.py index d4ae7134..0915031b 100644 --- a/_test_contract/platform_api/conftest.py +++ b/_test_contract/platform_api/conftest.py @@ -17,7 +17,7 @@ def platform_api_url(): def client(platform_api_url) -> UnstructuredClient: _client = UnstructuredClient( api_key_auth=FAKE_API_KEY, - server_url="platform-api", + server_url=platform_api_url, retry_config=RetryConfig( strategy="backoff", retry_connection_errors=False, From 700a0e393603cfcea703625a46546c03477cc7a3 Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 18 Feb 2025 13:37:43 -0500 Subject: [PATCH 05/12] Make server_url tests reusable --- .../unit/test_server_urls.py | 344 ++++++++++-------- 1 file changed, 192 insertions(+), 152 deletions(-) diff --git a/_test_unstructured_client/unit/test_server_urls.py b/_test_unstructured_client/unit/test_server_urls.py index cf3a9926..ab45f2e6 100644 --- a/_test_unstructured_client/unit/test_server_urls.py +++ b/_test_unstructured_client/unit/test_server_urls.py @@ -5,13 +5,57 @@ from unstructured_client import UnstructuredClient, basesdk, utils -# Raise from our mock so we can get out of the client code -class StopClientException(Exception): +# Raise one of these from our mock to return to the test code +class BaseUrlCorrect(Exception): pass +class BaseUrlIncorrect(Exception): + pass + + +def get_client_method_with_mock( + sdk_endpoint_name, + client_instance, + mocked_server_url, + monkeypatch +): + """ + Given an endpoint name, e.g. "general.partition", return a reference + to that method off of the given client instance. + + The client's _build_request will have the following mock: + Assert that the provided server_url is passed into _build_request. + Raise a custom exception to get back to the test. + """ + # Mock this to get past param validation + def mock_unmarshal(*args, **kwargs): + return {} + + monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) + + # Assert that the correct base_url makes it to here + def mock_build_request(*args, base_url, **kwargs): + if base_url == mocked_server_url: + raise BaseUrlCorrect + else: + raise BaseUrlIncorrect(base_url) + + # Find the method from the given string + class_name, method_name = sdk_endpoint_name.split(".") + endpoint_class = getattr(client_instance, class_name) + endpoint_method = getattr(endpoint_class, method_name) + + if "async" in method_name: + monkeypatch.setattr(endpoint_class, "_build_request_async", mock_build_request) + else: + monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) + + return endpoint_method + + @pytest.mark.parametrize( - "method_name", + "sdk_endpoint_name", [ ("general.partition"), ("destinations.create_destination"), @@ -35,101 +79,99 @@ class StopClientException(Exception): ("workflows.update_workflow"), ], ) -def test_endpoint_uses_correct_url(monkeypatch, method_name): - # Mock this to get past param validation - def mock_unmarshal(*args, **kwargs): - return {} - - monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) - - print(method_name) - # Use these in the mock - server_url = "http://localhost:8000" - assertion_message = "" - - # Assert that the correct base_url makes it to here - def mock_build_request(*args, base_url, **kwargs): - nonlocal assertion_message - nonlocal server_url - - assert base_url == server_url, assertion_message - raise StopClientException # We're good, let's bail - - endpoint_class_name, endpoint_method_name = method_name.split(".") - +def test_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): # Test 1 # Pass server_url to the client, no path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to client and ignored" - s = UnstructuredClient(server_url="http://localhost:8000") - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) - - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - endpoint_method(request={}) + s = UnstructuredClient(server_url="http://localhost:8000") + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + client_method(request={}) + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to client and ignored, got {e}") # Test 2 # Pass server_url to the client, with path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to client and was not stripped" - s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) - - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - endpoint_method(request={}) + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + client_method(request={}) + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to client and was not stripped, got {e}") # Test 3 # Pass server_url to the endpoint, no path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to endpoint and ignored" - s = UnstructuredClient() - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) - - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - endpoint_method( - request={}, - server_url="http://localhost:8000", - ) + s = UnstructuredClient() + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + client_method(request={}, server_url="http://localhost:8000") + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") # Test 4 # Pass server_url to the endpoint, with path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to endpoint and was not stripped" - s = UnstructuredClient() - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) + s = UnstructuredClient() + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + client_method(request={}, server_url="http://localhost:8000/my/endpoint") + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - endpoint_method( - request={}, - server_url="http://localhost:8000/my/endpoint", - ) # Test 5 # No server_url, should take the default - with pytest.raises(StopClientException): - assertion_message = "Default url was not used" - server_url = "https://api.unstructuredapp.io" if method_name == "general.partition" else "https://platform.unstructuredapp.io" - s = UnstructuredClient() + server_url = "https://api.unstructuredapp.io" if "partition" in sdk_endpoint_name else "https://platform.unstructuredapp.io" - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) + s = UnstructuredClient() + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + server_url, + monkeypatch + ) - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - endpoint_method( - request={}, - ) + try: + client_method(request={}) + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"Default url not used, got {e}") @pytest.mark.asyncio @pytest.mark.parametrize( - "method_name", + "sdk_endpoint_name", [ ("general.partition_async"), ("destinations.create_destination_async"), @@ -153,93 +195,91 @@ def mock_build_request(*args, base_url, **kwargs): ("workflows.update_workflow_async"), ], ) -async def test_async_endpoint_uses_correct_url(monkeypatch, method_name): - # Mock this to get past param validation - def mock_unmarshal(*args, **kwargs): - return {} - - monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) - - print(method_name) - # Use these in the mock - server_url = "http://localhost:8000" - assertion_message = "" - - # Assert that the correct base_url makes it to here - def mock_build_request(*args, base_url, **kwargs): - nonlocal assertion_message - nonlocal server_url - - assert base_url == server_url, assertion_message - raise StopClientException # We're good, let's bail - - endpoint_class_name, endpoint_method_name = method_name.split(".") - +async def test_async_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): # Test 1 # Pass server_url to the client, no path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to client and ignored" - s = UnstructuredClient(server_url="http://localhost:8000") - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) - - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - await endpoint_method(request={}) + s = UnstructuredClient(server_url="http://localhost:8000") + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + await client_method(request={}) + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to client and ignored, got {e}") # Test 2 # Pass server_url to the client, with path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to client and was not stripped" - s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) - - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - await endpoint_method(request={}) + s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + await client_method(request={}) + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to client and was not stripped, got {e}") # Test 3 # Pass server_url to the endpoint, no path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to endpoint and ignored" - s = UnstructuredClient() - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) - - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - await endpoint_method( - request={}, - server_url="http://localhost:8000", - ) + s = UnstructuredClient() + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + await client_method(request={}, server_url="http://localhost:8000") + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") # Test 4 # Pass server_url to the endpoint, with path - with pytest.raises(StopClientException): - assertion_message = "server_url was passed to endpoint and was not stripped" - s = UnstructuredClient() - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) + s = UnstructuredClient() + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + "http://localhost:8000", + monkeypatch + ) + + try: + await client_method(request={}, server_url="http://localhost:8000/my/endpoint") + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - await endpoint_method( - request={}, - server_url="http://localhost:8000/my/endpoint", - ) # Test 5 # No server_url, should take the default - with pytest.raises(StopClientException): - assertion_message = "Default url was not used" - server_url = "https://api.unstructuredapp.io" if method_name == "general.partition" else "https://platform.unstructuredapp.io" - s = UnstructuredClient() - - endpoint_class = getattr(s, endpoint_class_name) - endpoint_method = getattr(endpoint_class, endpoint_method_name) - - monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) - await endpoint_method( - request={}, - ) + server_url = "https://api.unstructuredapp.io" if "partition" in sdk_endpoint_name else "https://platform.unstructuredapp.io" + + s = UnstructuredClient() + client_method = get_client_method_with_mock( + sdk_endpoint_name, + s, + server_url, + monkeypatch + ) + + try: + await client_method(request={}) + except BaseUrlCorrect: + pass + except BaseUrlIncorrect as e: + pytest.fail(f"Default url not used, got {e}") From 7e9247710b78c834939c83688e5f9c99630f386a Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 18 Feb 2025 14:22:28 -0500 Subject: [PATCH 06/12] Make sure Speakeasy generates the partition spec second --- .speakeasy/workflow.yaml | 2 +- docs/models/errors/httpvalidationerror.md | 6 +++--- src/unstructured_client/models/errors/__init__.py | 3 ++- .../models/errors/httpvalidationerror.py | 15 +++++++++++++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.speakeasy/workflow.yaml b/.speakeasy/workflow.yaml index 0878575d..1d5e8ede 100644 --- a/.speakeasy/workflow.yaml +++ b/.speakeasy/workflow.yaml @@ -3,8 +3,8 @@ speakeasyVersion: latest sources: my-source: inputs: - - location: https://api.unstructured.io/general/openapi.json - location: https://platform.unstructuredapp.io/openapi.json + - location: https://api.unstructured.io/general/openapi.json overlays: - location: ./overlay_client.yaml registry: diff --git a/docs/models/errors/httpvalidationerror.md b/docs/models/errors/httpvalidationerror.md index 69a4ce9b..234e8630 100644 --- a/docs/models/errors/httpvalidationerror.md +++ b/docs/models/errors/httpvalidationerror.md @@ -3,6 +3,6 @@ ## Fields -| Field | Type | Required | Description | -| ---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------- | -| `detail` | List[[shared.ValidationError](../../models/shared/validationerror.md)] | :heavy_minus_sign: | N/A | \ No newline at end of file +| Field | Type | Required | Description | +| -------------------------------------------------------- | -------------------------------------------------------- | -------------------------------------------------------- | -------------------------------------------------------- | +| `detail` | [Optional[errors.Detail]](../../models/errors/detail.md) | :heavy_minus_sign: | N/A | \ No newline at end of file diff --git a/src/unstructured_client/models/errors/__init__.py b/src/unstructured_client/models/errors/__init__.py index 6fed7e26..199aa300 100644 --- a/src/unstructured_client/models/errors/__init__.py +++ b/src/unstructured_client/models/errors/__init__.py @@ -1,11 +1,12 @@ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" -from .httpvalidationerror import HTTPValidationError, HTTPValidationErrorData +from .httpvalidationerror import Detail, HTTPValidationError, HTTPValidationErrorData from .sdkerror import SDKError from .servererror import ServerError, ServerErrorData __all__ = [ + "Detail", "HTTPValidationError", "HTTPValidationErrorData", "SDKError", diff --git a/src/unstructured_client/models/errors/httpvalidationerror.py b/src/unstructured_client/models/errors/httpvalidationerror.py index 73bee857..d27c466d 100644 --- a/src/unstructured_client/models/errors/httpvalidationerror.py +++ b/src/unstructured_client/models/errors/httpvalidationerror.py @@ -1,14 +1,25 @@ """Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" from __future__ import annotations -from typing import List, Optional +from typing import List, Optional, Union +from typing_extensions import TypeAliasType from unstructured_client import utils from unstructured_client.models.shared import validationerror as shared_validationerror from unstructured_client.types import BaseModel +DetailTypedDict = TypeAliasType( + "DetailTypedDict", Union[List[shared_validationerror.ValidationErrorTypedDict], str] +) + + +Detail = TypeAliasType( + "Detail", Union[List[shared_validationerror.ValidationError], str] +) + + class HTTPValidationErrorData(BaseModel): - detail: Optional[List[shared_validationerror.ValidationError]] = None + detail: Optional[Detail] = None class HTTPValidationError(Exception): From bb503ba76fe66b8b876819210f238c0d6f1f5e2f Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Tue, 18 Feb 2025 15:15:20 -0500 Subject: [PATCH 07/12] Add endpoint files to .genignore for now --- .genignore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.genignore b/.genignore index 185d456e..f9d2c69c 100644 --- a/.genignore +++ b/.genignore @@ -8,3 +8,10 @@ _test_unstructured_client # ignore Makefile Makefile + +# Ignore the endpoint files until we can fix the base_url issue +src/unstructured_client/destinations.py +src/unstructured_client/general.py +src/unstructured_client/jobs.py +src/unstructured_client/sources.py +src/unstructured_client/workflows.py From f21ac1831e392e4518bbb5cd45dfa9128b2dba84 Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Wed, 19 Feb 2025 09:16:43 -0500 Subject: [PATCH 08/12] Undo server_url changes for platform --- .genignore | 6 +- .../unit/test_server_urls.py | 38 ----- src/unstructured_client/destinations.py | 151 ++++++++--------- src/unstructured_client/jobs.py | 79 ++++----- src/unstructured_client/sources.py | 151 ++++++++--------- src/unstructured_client/workflows.py | 157 ++++++++---------- 6 files changed, 249 insertions(+), 333 deletions(-) diff --git a/.genignore b/.genignore index f9d2c69c..9d61a0ac 100644 --- a/.genignore +++ b/.genignore @@ -9,9 +9,5 @@ _test_unstructured_client # ignore Makefile Makefile -# Ignore the endpoint files until we can fix the base_url issue -src/unstructured_client/destinations.py +# Ignore the general.partition code until we can fix the base_url issue src/unstructured_client/general.py -src/unstructured_client/jobs.py -src/unstructured_client/sources.py -src/unstructured_client/workflows.py diff --git a/_test_unstructured_client/unit/test_server_urls.py b/_test_unstructured_client/unit/test_server_urls.py index ab45f2e6..0d0ab2c8 100644 --- a/_test_unstructured_client/unit/test_server_urls.py +++ b/_test_unstructured_client/unit/test_server_urls.py @@ -58,25 +58,6 @@ def mock_build_request(*args, base_url, **kwargs): "sdk_endpoint_name", [ ("general.partition"), - ("destinations.create_destination"), - ("destinations.delete_destination"), - ("destinations.get_destination"), - ("destinations.list_destinations"), - ("destinations.update_destination"), - ("jobs.cancel_job"), - ("jobs.get_job"), - ("jobs.list_jobs"), - ("sources.create_source"), - ("sources.delete_source"), - ("sources.get_source"), - ("sources.list_sources"), - ("sources.update_source"), - ("workflows.create_workflow"), - ("workflows.delete_workflow"), - ("workflows.get_workflow"), - ("workflows.list_workflows"), - ("workflows.run_workflow"), - ("workflows.update_workflow"), ], ) def test_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): @@ -174,25 +155,6 @@ def test_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): "sdk_endpoint_name", [ ("general.partition_async"), - ("destinations.create_destination_async"), - ("destinations.delete_destination_async"), - ("destinations.get_destination_async"), - ("destinations.list_destinations_async"), - ("destinations.update_destination_async"), - ("jobs.cancel_job_async"), - ("jobs.get_job_async"), - ("jobs.list_jobs_async"), - ("sources.create_source_async"), - ("sources.delete_source_async"), - ("sources.get_source_async"), - ("sources.list_sources_async"), - ("sources.update_source_async"), - ("workflows.create_workflow_async"), - ("workflows.delete_workflow_async"), - ("workflows.get_workflow_async"), - ("workflows.list_workflows_async"), - ("workflows.run_workflow_async"), - ("workflows.update_workflow_async"), ], ) async def test_async_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): diff --git a/src/unstructured_client/destinations.py b/src/unstructured_client/destinations.py index a2dfe022..e152390a 100644 --- a/src/unstructured_client/destinations.py +++ b/src/unstructured_client/destinations.py @@ -4,7 +4,6 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext -from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -22,7 +21,9 @@ def create_destination( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.CreateDestinationResponse: - r"""Create Destination + r"""Create destination connector + + Create a new destination connector using the provided configuration and name. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -35,15 +36,12 @@ def create_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CREATE_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CREATE_DESTINATION_SERVERS[ operations.CREATE_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateDestinationRequest) @@ -143,7 +141,9 @@ async def create_destination_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.CreateDestinationResponse: - r"""Create Destination + r"""Create destination connector + + Create a new destination connector using the provided configuration and name. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -156,15 +156,12 @@ async def create_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CREATE_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CREATE_DESTINATION_SERVERS[ operations.CREATE_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateDestinationRequest) @@ -264,7 +261,9 @@ def delete_destination( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.DeleteDestinationResponse: - r"""Delete Destination + r"""Delete destination connector + + Delete a specific destination connector by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -277,15 +276,12 @@ def delete_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.DELETE_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.DELETE_DESTINATION_SERVERS[ operations.DELETE_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteDestinationRequest) @@ -376,7 +372,9 @@ async def delete_destination_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.DeleteDestinationResponse: - r"""Delete Destination + r"""Delete destination connector + + Delete a specific destination connector by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -389,15 +387,12 @@ async def delete_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.DELETE_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.DELETE_DESTINATION_SERVERS[ operations.DELETE_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteDestinationRequest) @@ -487,7 +482,9 @@ def get_destination( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.GetDestinationResponse: - r"""Get Destination + r"""Get destination connector + + Retrieve detailed information for a specific destination connector by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -500,15 +497,12 @@ def get_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_DESTINATION_SERVERS[ operations.GET_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetDestinationRequest) @@ -600,7 +594,9 @@ async def get_destination_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.GetDestinationResponse: - r"""Get Destination + r"""Get destination connector + + Retrieve detailed information for a specific destination connector by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -613,15 +609,12 @@ async def get_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_DESTINATION_SERVERS[ operations.GET_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetDestinationRequest) @@ -714,7 +707,9 @@ def list_destinations( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.ListDestinationsResponse: - r"""List Destinations + r"""List destination connectors + + Retrieve a list of available destination connectors. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -727,15 +722,12 @@ def list_destinations( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_DESTINATIONS_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_DESTINATIONS_SERVERS[ operations.LIST_DESTINATIONS_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListDestinationsRequest) @@ -829,7 +821,9 @@ async def list_destinations_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.ListDestinationsResponse: - r"""List Destinations + r"""List destination connectors + + Retrieve a list of available destination connectors. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -842,15 +836,12 @@ async def list_destinations_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_DESTINATIONS_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_DESTINATIONS_SERVERS[ operations.LIST_DESTINATIONS_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListDestinationsRequest) @@ -944,7 +935,9 @@ def update_destination( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.UpdateDestinationResponse: - r"""Update Destination + r"""Update destination connector + + Update the configuration of an existing destination connector. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -957,15 +950,12 @@ def update_destination( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.UPDATE_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.UPDATE_DESTINATION_SERVERS[ operations.UPDATE_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateDestinationRequest) @@ -1065,7 +1055,9 @@ async def update_destination_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.UpdateDestinationResponse: - r"""Update Destination + r"""Update destination connector + + Update the configuration of an existing destination connector. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -1078,15 +1070,12 @@ async def update_destination_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.UPDATE_DESTINATION_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.UPDATE_DESTINATION_SERVERS[ operations.UPDATE_DESTINATION_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateDestinationRequest) diff --git a/src/unstructured_client/jobs.py b/src/unstructured_client/jobs.py index 060a898f..820c4c93 100644 --- a/src/unstructured_client/jobs.py +++ b/src/unstructured_client/jobs.py @@ -4,7 +4,6 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext -from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -23,6 +22,8 @@ def cancel_job( ) -> operations.CancelJobResponse: r"""Cancel Job + Cancel the specified job. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -34,15 +35,12 @@ def cancel_job( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CANCEL_JOB_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CANCEL_JOB_SERVERS[ operations.CANCEL_JOB_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CancelJobRequest) @@ -134,6 +132,8 @@ async def cancel_job_async( ) -> operations.CancelJobResponse: r"""Cancel Job + Cancel the specified job. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -145,15 +145,12 @@ async def cancel_job_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CANCEL_JOB_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CANCEL_JOB_SERVERS[ operations.CANCEL_JOB_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CancelJobRequest) @@ -243,6 +240,8 @@ def get_job( ) -> operations.GetJobResponse: r"""Get Job + Retrieve detailed information for a specific job by its ID. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -254,15 +253,12 @@ def get_job( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_JOB_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_JOB_SERVERS[ operations.GET_JOB_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetJobRequest) @@ -354,6 +350,8 @@ async def get_job_async( ) -> operations.GetJobResponse: r"""Get Job + Retrieve detailed information for a specific job by its ID. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -365,15 +363,12 @@ async def get_job_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_JOB_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_JOB_SERVERS[ operations.GET_JOB_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetJobRequest) @@ -465,6 +460,8 @@ def list_jobs( ) -> operations.ListJobsResponse: r"""List Jobs + Retrieve a list of jobs with optional filtering by workflow ID or job status. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -476,15 +473,12 @@ def list_jobs( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_JOBS_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_JOBS_SERVERS[ operations.LIST_JOBS_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListJobsRequest) @@ -576,6 +570,8 @@ async def list_jobs_async( ) -> operations.ListJobsResponse: r"""List Jobs + Retrieve a list of jobs with optional filtering by workflow ID or job status. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -587,15 +583,12 @@ async def list_jobs_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_JOBS_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_JOBS_SERVERS[ operations.LIST_JOBS_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListJobsRequest) diff --git a/src/unstructured_client/sources.py b/src/unstructured_client/sources.py index a69618de..79f78f3a 100644 --- a/src/unstructured_client/sources.py +++ b/src/unstructured_client/sources.py @@ -4,7 +4,6 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext -from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -21,7 +20,9 @@ def create_source( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.CreateSourceResponse: - r"""Create Source + r"""Create source connector + + Create a new source connector using the provided configuration and name. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -34,15 +35,12 @@ def create_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CREATE_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CREATE_SOURCE_SERVERS[ operations.CREATE_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateSourceRequest) @@ -141,7 +139,9 @@ async def create_source_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.CreateSourceResponse: - r"""Create Source + r"""Create source connector + + Create a new source connector using the provided configuration and name. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -154,15 +154,12 @@ async def create_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CREATE_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CREATE_SOURCE_SERVERS[ operations.CREATE_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateSourceRequest) @@ -261,7 +258,9 @@ def delete_source( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.DeleteSourceResponse: - r"""Delete Source + r"""Delete source connector + + Delete a specific source connector identified by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -274,15 +273,12 @@ def delete_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.DELETE_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.DELETE_SOURCE_SERVERS[ operations.DELETE_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteSourceRequest) @@ -372,7 +368,9 @@ async def delete_source_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.DeleteSourceResponse: - r"""Delete Source + r"""Delete source connector + + Delete a specific source connector identified by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -385,15 +383,12 @@ async def delete_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.DELETE_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.DELETE_SOURCE_SERVERS[ operations.DELETE_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteSourceRequest) @@ -483,7 +478,9 @@ def get_source( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.GetSourceResponse: - r"""Get Source + r"""Get source connector + + Retrieve detailed information for a specific source connector by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -496,15 +493,12 @@ def get_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_SOURCE_SERVERS[ operations.GET_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetSourceRequest) @@ -596,7 +590,9 @@ async def get_source_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.GetSourceResponse: - r"""Get Source + r"""Get source connector + + Retrieve detailed information for a specific source connector by its ID. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -609,15 +605,12 @@ async def get_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_SOURCE_SERVERS[ operations.GET_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetSourceRequest) @@ -709,7 +702,9 @@ def list_sources( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.ListSourcesResponse: - r"""List Sources + r"""List available source connectors + + Retrieve a list of available source connectors. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -722,15 +717,12 @@ def list_sources( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_SOURCES_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_SOURCES_SERVERS[ operations.LIST_SOURCES_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListSourcesRequest) @@ -822,7 +814,9 @@ async def list_sources_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.ListSourcesResponse: - r"""List Sources + r"""List available source connectors + + Retrieve a list of available source connectors. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -835,15 +829,12 @@ async def list_sources_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_SOURCES_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_SOURCES_SERVERS[ operations.LIST_SOURCES_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListSourcesRequest) @@ -935,7 +926,9 @@ def update_source( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.UpdateSourceResponse: - r"""Update Source + r"""Update source connector + + Update the configuration of an existing source connector. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -948,15 +941,12 @@ def update_source( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.UPDATE_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.UPDATE_SOURCE_SERVERS[ operations.UPDATE_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateSourceRequest) @@ -1055,7 +1045,9 @@ async def update_source_async( timeout_ms: Optional[int] = None, http_headers: Optional[Mapping[str, str]] = None, ) -> operations.UpdateSourceResponse: - r"""Update Source + r"""Update source connector + + Update the configuration of an existing source connector. :param request: The request object to send. :param retries: Override the default retry configuration for this method @@ -1068,15 +1060,12 @@ async def update_source_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.UPDATE_SOURCE_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.UPDATE_SOURCE_SERVERS[ operations.UPDATE_SOURCE_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateSourceRequest) diff --git a/src/unstructured_client/workflows.py b/src/unstructured_client/workflows.py index 80cada82..0b59f9c9 100644 --- a/src/unstructured_client/workflows.py +++ b/src/unstructured_client/workflows.py @@ -4,7 +4,6 @@ from typing import Any, List, Mapping, Optional, Union, cast from unstructured_client import utils from unstructured_client._hooks import HookContext -from unstructured_client._hooks.custom.clean_server_url_hook import choose_server_url from unstructured_client.models import errors, operations, shared from unstructured_client.types import BaseModel, OptionalNullable, UNSET @@ -23,6 +22,8 @@ def create_workflow( ) -> operations.CreateWorkflowResponse: r"""Create Workflow + Create a new workflow, either custom or auto, and configure its settings. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -34,15 +35,12 @@ def create_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CREATE_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CREATE_WORKFLOW_SERVERS[ operations.CREATE_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateWorkflowRequest) @@ -139,6 +137,8 @@ async def create_workflow_async( ) -> operations.CreateWorkflowResponse: r"""Create Workflow + Create a new workflow, either custom or auto, and configure its settings. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -150,15 +150,12 @@ async def create_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.CREATE_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.CREATE_WORKFLOW_SERVERS[ operations.CREATE_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.CreateWorkflowRequest) @@ -255,6 +252,8 @@ def delete_workflow( ) -> operations.DeleteWorkflowResponse: r"""Delete Workflow + Delete a workflow by its ID. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -266,15 +265,12 @@ def delete_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.DELETE_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.DELETE_WORKFLOW_SERVERS[ operations.DELETE_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteWorkflowRequest) @@ -366,6 +362,8 @@ async def delete_workflow_async( ) -> operations.DeleteWorkflowResponse: r"""Delete Workflow + Delete a workflow by its ID. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -377,15 +375,12 @@ async def delete_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.DELETE_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.DELETE_WORKFLOW_SERVERS[ operations.DELETE_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.DeleteWorkflowRequest) @@ -477,6 +472,8 @@ def get_workflow( ) -> operations.GetWorkflowResponse: r"""Get Workflow + Retrieve detailed information for a specific workflow by its ID. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -488,15 +485,12 @@ def get_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_WORKFLOW_SERVERS[ operations.GET_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetWorkflowRequest) @@ -590,6 +584,8 @@ async def get_workflow_async( ) -> operations.GetWorkflowResponse: r"""Get Workflow + Retrieve detailed information for a specific workflow by its ID. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -601,15 +597,12 @@ async def get_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.GET_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.GET_WORKFLOW_SERVERS[ operations.GET_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.GetWorkflowRequest) @@ -703,6 +696,8 @@ def list_workflows( ) -> operations.ListWorkflowsResponse: r"""List Workflows + Retrieve a list of workflows, optionally filtered by source, destination, or state. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -714,15 +709,12 @@ def list_workflows( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_WORKFLOWS_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_WORKFLOWS_SERVERS[ operations.LIST_WORKFLOWS_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListWorkflowsRequest) @@ -816,6 +808,8 @@ async def list_workflows_async( ) -> operations.ListWorkflowsResponse: r"""List Workflows + Retrieve a list of workflows, optionally filtered by source, destination, or state. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -827,15 +821,12 @@ async def list_workflows_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.LIST_WORKFLOWS_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.LIST_WORKFLOWS_SERVERS[ operations.LIST_WORKFLOWS_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.ListWorkflowsRequest) @@ -929,6 +920,8 @@ def run_workflow( ) -> operations.RunWorkflowResponse: r"""Run Workflow + Run a workflow by triggering a new job if none is currently active. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -940,15 +933,12 @@ def run_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.RUN_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.RUN_WORKFLOW_SERVERS[ operations.RUN_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.RunWorkflowRequest) @@ -1042,6 +1032,8 @@ async def run_workflow_async( ) -> operations.RunWorkflowResponse: r"""Run Workflow + Run a workflow by triggering a new job if none is currently active. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -1053,15 +1045,12 @@ async def run_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.RUN_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.RUN_WORKFLOW_SERVERS[ operations.RUN_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.RunWorkflowRequest) @@ -1155,6 +1144,8 @@ def update_workflow( ) -> operations.UpdateWorkflowResponse: r"""Update Workflow + Update an existing workflow's name, connectors, schedule, or workflow type. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -1166,15 +1157,12 @@ def update_workflow( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.UPDATE_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.UPDATE_WORKFLOW_SERVERS[ operations.UPDATE_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateWorkflowRequest) @@ -1271,6 +1259,8 @@ async def update_workflow_async( ) -> operations.UpdateWorkflowResponse: r"""Update Workflow + Update an existing workflow's name, connectors, schedule, or workflow type. + :param request: The request object to send. :param retries: Override the default retry configuration for this method :param server_url: Override the default server URL for this method @@ -1282,15 +1272,12 @@ async def update_workflow_async( if timeout_ms is None: timeout_ms = self.sdk_configuration.timeout_ms - client_url, *_ = self.sdk_configuration.get_server_details() - - base_url = choose_server_url( - endpoint_url=server_url, - client_url=client_url, - default_endpoint_url=operations.UPDATE_WORKFLOW_SERVERS[ + if server_url is not None: + base_url = server_url + else: + base_url = operations.UPDATE_WORKFLOW_SERVERS[ operations.UPDATE_WORKFLOW_SERVER_PLATFORM_API ] - ) if not isinstance(request, BaseModel): request = utils.unmarshal(request, operations.UpdateWorkflowRequest) From 8625982ea037691b54dd913c6e9181148aefcf6f Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Wed, 19 Feb 2025 16:25:59 -0500 Subject: [PATCH 09/12] Refactor server url helper functions --- .../_hooks/custom/clean_server_url_hook.py | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py index eb8f6580..225bcb5a 100644 --- a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py +++ b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py @@ -12,46 +12,44 @@ def clean_server_url(base_url: str) -> str: if not base_url: return "" - # -- add a url scheme if not present (urllib.parse does not work reliably without it) + + # add a url scheme if not present (urllib.parse does not work reliably without it) if "http" not in base_url: base_url = "http://" + base_url parsed_url: ParseResult = urlparse(base_url) - if "api.unstructuredapp.io" in base_url: + unstructured_services = [ + "api.unstructuredapp.io", + "api.unstructured.io", + "platform.unstructuredapp.io", + ] + if parsed_url.netloc in unstructured_services: if parsed_url.scheme != "https": parsed_url = parsed_url._replace(scheme="https") - # -- path should always be empty - return urlunparse(parsed_url._replace(path="")) + # We only want the base url + return urlunparse(parsed_url._replace(path="", params="", query="", fragment="")) -def choose_server_url(endpoint_url, client_url, default_endpoint_url) -> str: +def choose_server_url(endpoint_url: str, client_url: str, default_endpoint_url: str) -> str: """ Helper function to fix a breaking change in the SDK past 0.30.0. When we merged the partition and platform specs, the client server_url stopped working, and users need to pass it in the endpoint function. For now, call this helper in the generated code to set the correct url. - """ - # First, see if the endpoint has a url: - # s.general.partition(server_url=...) - if endpoint_url is not None: - url = endpoint_url + Order of choices: + Endpoint server_url -> s.general.partition(server_url=...) + (Passed in as None if not set) - # Next, try the base client url: - # s = UnstructuredClient(server_url=...) - # (If not set it's an empty string) - elif client_url != "": - url = client_url + Base client server_url -> s = UnstructuredClient(server_url=...) + (Passed as empty string if not set) - # Finally, take the url defined in the spec: - # operations.PARTITION_SERVERS[...] - else: - url = default_endpoint_url + Default endpoint URL as defined in the spec + """ - # Make sure we drop the path if it's provided anywhere - # (The endpoint url will be set after we've done the init hooks) + url = endpoint_url if endpoint_url is not None else (client_url or default_endpoint_url) return clean_server_url(url) From 77f982c8a796ed6079b208c5243d4ab06a1b4107 Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Wed, 19 Feb 2025 16:29:46 -0500 Subject: [PATCH 10/12] Fix typing error --- src/unstructured_client/_hooks/custom/clean_server_url_hook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py index 225bcb5a..c05b96a3 100644 --- a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py +++ b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py @@ -32,7 +32,7 @@ def clean_server_url(base_url: str) -> str: return urlunparse(parsed_url._replace(path="", params="", query="", fragment="")) -def choose_server_url(endpoint_url: str, client_url: str, default_endpoint_url: str) -> str: +def choose_server_url(endpoint_url: str | None, client_url: str, default_endpoint_url: str) -> str: """ Helper function to fix a breaking change in the SDK past 0.30.0. When we merged the partition and platform specs, the client server_url stopped working, From 8f0b655a06928bff59760ab4af6ef7e2b5677760 Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Wed, 19 Feb 2025 16:47:34 -0500 Subject: [PATCH 11/12] Fix unit tests --- .../_hooks/custom/clean_server_url_hook.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py index c05b96a3..13906bd3 100644 --- a/src/unstructured_client/_hooks/custom/clean_server_url_hook.py +++ b/src/unstructured_client/_hooks/custom/clean_server_url_hook.py @@ -18,13 +18,8 @@ def clean_server_url(base_url: str) -> str: base_url = "http://" + base_url parsed_url: ParseResult = urlparse(base_url) - - unstructured_services = [ - "api.unstructuredapp.io", - "api.unstructured.io", - "platform.unstructuredapp.io", - ] - if parsed_url.netloc in unstructured_services: + + if "api.unstructuredapp.io" in parsed_url.netloc: if parsed_url.scheme != "https": parsed_url = parsed_url._replace(scheme="https") From 592655083ffc826d1660999d98fbfa9ce3e24a46 Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Thu, 20 Feb 2025 11:52:14 -0500 Subject: [PATCH 12/12] Don't run this one test for now --- _test_unstructured_client/unit/test_custom_hooks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/_test_unstructured_client/unit/test_custom_hooks.py b/_test_unstructured_client/unit/test_custom_hooks.py index 206c685a..724a2d5f 100644 --- a/_test_unstructured_client/unit/test_custom_hooks.py +++ b/_test_unstructured_client/unit/test_custom_hooks.py @@ -17,6 +17,7 @@ FAKE_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +@pytest.mark.xfail(run=False, reason="This test is causing a hang") def test_unit_retry_with_backoff_does_retry(caplog): caplog.set_level(logging.INFO) filename = "README.md"