269 lines
11 KiB
Python
269 lines
11 KiB
Python
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
|
|
from .basesdk import BaseSDK
|
|
from enum import Enum
|
|
from typing import Any, Dict, List, Mapping, Optional, Union, cast
|
|
from unstructured_client import utils
|
|
from unstructured_client._hooks import HookContext
|
|
from unstructured_client.models import errors, operations, shared
|
|
from unstructured_client.types import BaseModel, OptionalNullable, UNSET
|
|
from unstructured_client._hooks.custom.clean_server_url_hook import clean_server_url
|
|
from unstructured_client.utils.unmarshal_json_response import unmarshal_json_response
|
|
|
|
|
|
class PartitionAcceptEnum(str, Enum):
|
|
APPLICATION_JSON = "application/json"
|
|
TEXT_CSV = "text/csv"
|
|
|
|
|
|
class General(BaseSDK):
|
|
def partition(
|
|
self,
|
|
*,
|
|
request: Union[
|
|
operations.PartitionRequest, operations.PartitionRequestTypedDict
|
|
],
|
|
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
server_url: Optional[str] = None,
|
|
timeout_ms: Optional[int] = None,
|
|
accept_header_override: Optional[PartitionAcceptEnum] = None,
|
|
http_headers: Optional[Mapping[str, str]] = None,
|
|
) -> operations.PartitionResponse:
|
|
r"""Summary
|
|
|
|
Description
|
|
|
|
: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
|
|
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
:param accept_header_override: Override the default accept header for this method
|
|
:param http_headers: Additional headers to set or replace on requests.
|
|
"""
|
|
base_url = None
|
|
url_variables = None
|
|
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 = self._get_url(base_url, url_variables)
|
|
|
|
# Note(austin): Add a custom check to handle the default server URL
|
|
# The SDK globally defaults to the platform URL.
|
|
# If that hasn't changed, we need to switch to the partition url here.
|
|
base_url = clean_server_url(base_url)
|
|
if base_url == "https://platform.unstructuredapp.io":
|
|
base_url = "https://api.unstructuredapp.io"
|
|
|
|
if not isinstance(request, BaseModel):
|
|
request = utils.unmarshal(request, operations.PartitionRequest)
|
|
request = cast(operations.PartitionRequest, request)
|
|
|
|
req = self._build_request(
|
|
method="POST",
|
|
path="/general/v0/general",
|
|
base_url=base_url,
|
|
url_variables=url_variables,
|
|
request=request,
|
|
request_body_required=True,
|
|
request_has_path_params=False,
|
|
request_has_query_params=True,
|
|
user_agent_header="user-agent",
|
|
accept_header_value=accept_header_override.value
|
|
if accept_header_override is not None
|
|
else "application/json;q=1, text/csv;q=0",
|
|
http_headers=http_headers,
|
|
security=self.sdk_configuration.security,
|
|
get_serialized_body=lambda: utils.serialize_request_body(
|
|
request.partition_parameters,
|
|
False,
|
|
False,
|
|
"multipart",
|
|
shared.PartitionParameters,
|
|
),
|
|
timeout_ms=timeout_ms,
|
|
)
|
|
|
|
if retries == UNSET:
|
|
if self.sdk_configuration.retry_config is not UNSET:
|
|
retries = self.sdk_configuration.retry_config
|
|
else:
|
|
retries = utils.RetryConfig(
|
|
"backoff", utils.BackoffStrategy(3000, 720000, 1.88, 1800000), True
|
|
)
|
|
|
|
retry_config = None
|
|
if isinstance(retries, utils.RetryConfig):
|
|
retry_config = (retries, ["5xx"])
|
|
|
|
http_res = self.do_request(
|
|
hook_ctx=HookContext(
|
|
config=self.sdk_configuration,
|
|
base_url=base_url or "",
|
|
operation_id="partition",
|
|
oauth2_scopes=[],
|
|
security_source=self.sdk_configuration.security,
|
|
),
|
|
request=req,
|
|
error_status_codes=["422", "4XX", "5XX"],
|
|
retry_config=retry_config,
|
|
)
|
|
|
|
response_data: Any = None
|
|
if utils.match_response(http_res, "200", "application/json"):
|
|
return operations.PartitionResponse(
|
|
elements=unmarshal_json_response(
|
|
Optional[List[Dict[str, Any]]], http_res
|
|
),
|
|
status_code=http_res.status_code,
|
|
content_type=http_res.headers.get("Content-Type") or "",
|
|
raw_response=http_res,
|
|
)
|
|
if utils.match_response(http_res, "200", "text/csv"):
|
|
return operations.PartitionResponse(
|
|
csv_elements=http_res.text,
|
|
status_code=http_res.status_code,
|
|
content_type=http_res.headers.get("Content-Type") or "",
|
|
raw_response=http_res,
|
|
)
|
|
if utils.match_response(http_res, "422", "application/json"):
|
|
response_data = unmarshal_json_response(
|
|
errors.HTTPValidationErrorData, http_res
|
|
)
|
|
raise errors.HTTPValidationError(response_data, http_res)
|
|
if utils.match_response(http_res, "4XX", "*"):
|
|
http_res_text = utils.stream_to_text(http_res)
|
|
raise errors.SDKError("API error occurred", http_res, http_res_text)
|
|
if utils.match_response(http_res, "5XX", "application/json"):
|
|
response_data = unmarshal_json_response(errors.ServerErrorData, http_res)
|
|
raise errors.ServerError(response_data, http_res)
|
|
|
|
raise errors.SDKError("Unexpected response received", http_res)
|
|
|
|
async def partition_async(
|
|
self,
|
|
*,
|
|
request: Union[
|
|
operations.PartitionRequest, operations.PartitionRequestTypedDict
|
|
],
|
|
retries: OptionalNullable[utils.RetryConfig] = UNSET,
|
|
server_url: Optional[str] = None,
|
|
timeout_ms: Optional[int] = None,
|
|
accept_header_override: Optional[PartitionAcceptEnum] = None,
|
|
http_headers: Optional[Mapping[str, str]] = None,
|
|
) -> operations.PartitionResponse:
|
|
r"""Summary
|
|
|
|
Description
|
|
|
|
: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
|
|
:param timeout_ms: Override the default request timeout configuration for this method in milliseconds
|
|
:param accept_header_override: Override the default accept header for this method
|
|
:param http_headers: Additional headers to set or replace on requests.
|
|
"""
|
|
base_url = None
|
|
url_variables = None
|
|
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 = self._get_url(base_url, url_variables)
|
|
|
|
# Note(austin): Add a custom check to handle the default server URL
|
|
# The SDK globally defaults to the platform URL.
|
|
# If that hasn't changed, we need to switch to the partition url here.
|
|
base_url = clean_server_url(base_url)
|
|
if base_url == "https://platform.unstructuredapp.io":
|
|
base_url = "https://api.unstructuredapp.io"
|
|
|
|
if not isinstance(request, BaseModel):
|
|
request = utils.unmarshal(request, operations.PartitionRequest)
|
|
request = cast(operations.PartitionRequest, request)
|
|
|
|
req = self._build_request_async(
|
|
method="POST",
|
|
path="/general/v0/general",
|
|
base_url=base_url,
|
|
url_variables=url_variables,
|
|
request=request,
|
|
request_body_required=True,
|
|
request_has_path_params=False,
|
|
request_has_query_params=True,
|
|
user_agent_header="user-agent",
|
|
accept_header_value=accept_header_override.value
|
|
if accept_header_override is not None
|
|
else "application/json;q=1, text/csv;q=0",
|
|
http_headers=http_headers,
|
|
security=self.sdk_configuration.security,
|
|
get_serialized_body=lambda: utils.serialize_request_body(
|
|
request.partition_parameters,
|
|
False,
|
|
False,
|
|
"multipart",
|
|
shared.PartitionParameters,
|
|
),
|
|
timeout_ms=timeout_ms,
|
|
)
|
|
|
|
if retries == UNSET:
|
|
if self.sdk_configuration.retry_config is not UNSET:
|
|
retries = self.sdk_configuration.retry_config
|
|
else:
|
|
retries = utils.RetryConfig(
|
|
"backoff", utils.BackoffStrategy(3000, 720000, 1.88, 1800000), True
|
|
)
|
|
|
|
retry_config = None
|
|
if isinstance(retries, utils.RetryConfig):
|
|
retry_config = (retries, ["5xx"])
|
|
|
|
http_res = await self.do_request_async(
|
|
hook_ctx=HookContext(
|
|
config=self.sdk_configuration,
|
|
base_url=base_url or "",
|
|
operation_id="partition",
|
|
oauth2_scopes=[],
|
|
security_source=self.sdk_configuration.security,
|
|
),
|
|
request=req,
|
|
error_status_codes=["422", "4XX", "5XX"],
|
|
retry_config=retry_config,
|
|
)
|
|
|
|
response_data: Any = None
|
|
if utils.match_response(http_res, "200", "application/json"):
|
|
return operations.PartitionResponse(
|
|
elements=unmarshal_json_response(
|
|
Optional[List[Dict[str, Any]]], http_res
|
|
),
|
|
status_code=http_res.status_code,
|
|
content_type=http_res.headers.get("Content-Type") or "",
|
|
raw_response=http_res,
|
|
)
|
|
if utils.match_response(http_res, "200", "text/csv"):
|
|
return operations.PartitionResponse(
|
|
csv_elements=http_res.text,
|
|
status_code=http_res.status_code,
|
|
content_type=http_res.headers.get("Content-Type") or "",
|
|
raw_response=http_res,
|
|
)
|
|
if utils.match_response(http_res, "422", "application/json"):
|
|
response_data = unmarshal_json_response(
|
|
errors.HTTPValidationErrorData, http_res
|
|
)
|
|
raise errors.HTTPValidationError(response_data, http_res)
|
|
if utils.match_response(http_res, "4XX", "*"):
|
|
http_res_text = await utils.stream_to_text_async(http_res)
|
|
raise errors.SDKError("API error occurred", http_res, http_res_text)
|
|
if utils.match_response(http_res, "5XX", "application/json"):
|
|
response_data = unmarshal_json_response(errors.ServerErrorData, http_res)
|
|
raise errors.ServerError(response_data, http_res)
|
|
|
|
raise errors.SDKError("Unexpected response received", http_res)
|