forked from Unstructured-IO/unstructured-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk.py
More file actions
184 lines (160 loc) · 6.87 KB
/
sdk.py
File metadata and controls
184 lines (160 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
from .basesdk import BaseSDK
from .httpclient import AsyncHttpClient, ClientOwner, HttpClient, close_clients
from .sdkconfiguration import SDKConfiguration
from .utils.logger import Logger, get_default_logger
from .utils.retries import RetryConfig
import httpx
import importlib
from typing import Any, Callable, Dict, Optional, TYPE_CHECKING, Union, cast
from unstructured_client import utils
from unstructured_client._hooks import SDKHooks
from unstructured_client.models import shared
from unstructured_client.types import OptionalNullable, UNSET
import weakref
if TYPE_CHECKING:
from unstructured_client.destinations import Destinations
from unstructured_client.general import General
from unstructured_client.jobs import Jobs
from unstructured_client.sources import Sources
from unstructured_client.templates import Templates
from unstructured_client.workflows import Workflows
class UnstructuredClient(BaseSDK):
destinations: "Destinations"
jobs: "Jobs"
sources: "Sources"
templates: "Templates"
workflows: "Workflows"
general: "General"
_sub_sdk_map = {
"destinations": ("unstructured_client.destinations", "Destinations"),
"jobs": ("unstructured_client.jobs", "Jobs"),
"sources": ("unstructured_client.sources", "Sources"),
"templates": ("unstructured_client.templates", "Templates"),
"workflows": ("unstructured_client.workflows", "Workflows"),
"general": ("unstructured_client.general", "General"),
}
def __init__(
self,
api_key_auth: Optional[
Union[Optional[str], Callable[[], Optional[str]]]
] = None,
server: Optional[str] = None,
server_url: Optional[str] = None,
url_params: Optional[Dict[str, str]] = None,
client: Optional[HttpClient] = None,
async_client: Optional[AsyncHttpClient] = None,
retry_config: OptionalNullable[RetryConfig] = UNSET,
timeout_ms: Optional[int] = None,
debug_logger: Optional[Logger] = None,
) -> None:
r"""Instantiates the SDK configuring it with the provided parameters.
:param api_key_auth: The api_key_auth required for authentication
:param server: The server by name to use for all methods
:param server_url: The server URL to use for all methods
:param url_params: Parameters to optionally template the server URL with
:param client: The HTTP client to use for all synchronous methods
:param async_client: The Async HTTP client to use for all asynchronous methods
:param retry_config: The retry configuration to use for all supported methods
:param timeout_ms: Optional request timeout applied to each operation in milliseconds
"""
client_supplied = True
if client is None:
client = httpx.Client()
client_supplied = False
assert issubclass(
type(client), HttpClient
), "The provided client must implement the HttpClient protocol."
async_client_supplied = True
if async_client is None:
async_client = httpx.AsyncClient()
async_client_supplied = False
if debug_logger is None:
debug_logger = get_default_logger()
assert issubclass(
type(async_client), AsyncHttpClient
), "The provided async_client must implement the AsyncHttpClient protocol."
security: Any = None
if callable(api_key_auth):
# pylint: disable=unnecessary-lambda-assignment
security = lambda: shared.Security(api_key_auth=api_key_auth())
else:
security = shared.Security(api_key_auth=api_key_auth)
if server_url is not None:
if url_params is not None:
server_url = utils.template_url(server_url, url_params)
BaseSDK.__init__(
self,
SDKConfiguration(
client=client,
client_supplied=client_supplied,
async_client=async_client,
async_client_supplied=async_client_supplied,
security=security,
server_url=server_url,
server=server,
retry_config=retry_config,
timeout_ms=timeout_ms,
debug_logger=debug_logger,
),
)
hooks = SDKHooks()
# pylint: disable=protected-access
self.sdk_configuration.__dict__["_hooks"] = hooks
current_server_url, *_ = self.sdk_configuration.get_server_details()
server_url, self.sdk_configuration.client = hooks.sdk_init(
current_server_url, client
)
if current_server_url != server_url:
self.sdk_configuration.server_url = server_url
weakref.finalize(
self,
close_clients,
cast(ClientOwner, self.sdk_configuration),
self.sdk_configuration.client,
self.sdk_configuration.client_supplied,
self.sdk_configuration.async_client,
self.sdk_configuration.async_client_supplied,
)
def __getattr__(self, name: str):
if name in self._sub_sdk_map:
module_path, class_name = self._sub_sdk_map[name]
try:
module = importlib.import_module(module_path)
klass = getattr(module, class_name)
instance = klass(self.sdk_configuration)
setattr(self, name, instance)
return instance
except ImportError as e:
raise AttributeError(
f"Failed to import module {module_path} for attribute {name}: {e}"
) from e
except AttributeError as e:
raise AttributeError(
f"Failed to find class {class_name} in module {module_path} for attribute {name}: {e}"
) from e
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'"
)
def __dir__(self):
default_attrs = list(super().__dir__())
lazy_attrs = list(self._sub_sdk_map.keys())
return sorted(list(set(default_attrs + lazy_attrs)))
def __enter__(self):
return self
async def __aenter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if (
self.sdk_configuration.client is not None
and not self.sdk_configuration.client_supplied
):
self.sdk_configuration.client.close()
self.sdk_configuration.client = None
async def __aexit__(self, exc_type, exc_val, exc_tb):
if (
self.sdk_configuration.async_client is not None
and not self.sdk_configuration.async_client_supplied
):
await self.sdk_configuration.async_client.aclose()
self.sdk_configuration.async_client = None