c63a5cfe-b235-4fbe-8bbb-82a9e02a482a-python 0.1.0a3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/METADATA +396 -0
  2. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/RECORD +190 -0
  3. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/WHEEL +4 -0
  4. c63a5cfe_b235_4fbe_8bbb_82a9e02a482a_python-0.1.0a3.dist-info/licenses/LICENSE +201 -0
  5. digitalocean_genai_sdk/__init__.py +99 -0
  6. digitalocean_genai_sdk/_base_client.py +1949 -0
  7. digitalocean_genai_sdk/_client.py +795 -0
  8. digitalocean_genai_sdk/_compat.py +219 -0
  9. digitalocean_genai_sdk/_constants.py +14 -0
  10. digitalocean_genai_sdk/_exceptions.py +108 -0
  11. digitalocean_genai_sdk/_files.py +123 -0
  12. digitalocean_genai_sdk/_models.py +805 -0
  13. digitalocean_genai_sdk/_qs.py +150 -0
  14. digitalocean_genai_sdk/_resource.py +43 -0
  15. digitalocean_genai_sdk/_response.py +832 -0
  16. digitalocean_genai_sdk/_streaming.py +333 -0
  17. digitalocean_genai_sdk/_types.py +219 -0
  18. digitalocean_genai_sdk/_utils/__init__.py +57 -0
  19. digitalocean_genai_sdk/_utils/_logs.py +25 -0
  20. digitalocean_genai_sdk/_utils/_proxy.py +65 -0
  21. digitalocean_genai_sdk/_utils/_reflection.py +42 -0
  22. digitalocean_genai_sdk/_utils/_resources_proxy.py +24 -0
  23. digitalocean_genai_sdk/_utils/_streams.py +12 -0
  24. digitalocean_genai_sdk/_utils/_sync.py +86 -0
  25. digitalocean_genai_sdk/_utils/_transform.py +447 -0
  26. digitalocean_genai_sdk/_utils/_typing.py +151 -0
  27. digitalocean_genai_sdk/_utils/_utils.py +422 -0
  28. digitalocean_genai_sdk/_version.py +4 -0
  29. digitalocean_genai_sdk/lib/.keep +4 -0
  30. digitalocean_genai_sdk/py.typed +0 -0
  31. digitalocean_genai_sdk/resources/__init__.py +145 -0
  32. digitalocean_genai_sdk/resources/agents/__init__.py +89 -0
  33. digitalocean_genai_sdk/resources/agents/agents.py +965 -0
  34. digitalocean_genai_sdk/resources/agents/api_keys.py +581 -0
  35. digitalocean_genai_sdk/resources/agents/child_agents.py +508 -0
  36. digitalocean_genai_sdk/resources/agents/functions.py +421 -0
  37. digitalocean_genai_sdk/resources/agents/knowledge_bases.py +346 -0
  38. digitalocean_genai_sdk/resources/agents/versions.py +298 -0
  39. digitalocean_genai_sdk/resources/api_keys/__init__.py +19 -0
  40. digitalocean_genai_sdk/resources/api_keys/api_keys.py +275 -0
  41. digitalocean_genai_sdk/resources/api_keys/api_keys_.py +529 -0
  42. digitalocean_genai_sdk/resources/auth/__init__.py +33 -0
  43. digitalocean_genai_sdk/resources/auth/agents/__init__.py +33 -0
  44. digitalocean_genai_sdk/resources/auth/agents/agents.py +102 -0
  45. digitalocean_genai_sdk/resources/auth/agents/token.py +173 -0
  46. digitalocean_genai_sdk/resources/auth/auth.py +102 -0
  47. digitalocean_genai_sdk/resources/chat.py +381 -0
  48. digitalocean_genai_sdk/resources/embeddings.py +201 -0
  49. digitalocean_genai_sdk/resources/indexing_jobs.py +543 -0
  50. digitalocean_genai_sdk/resources/knowledge_bases/__init__.py +33 -0
  51. digitalocean_genai_sdk/resources/knowledge_bases/data_sources.py +410 -0
  52. digitalocean_genai_sdk/resources/knowledge_bases/knowledge_bases.py +667 -0
  53. digitalocean_genai_sdk/resources/models.py +222 -0
  54. digitalocean_genai_sdk/resources/providers/__init__.py +47 -0
  55. digitalocean_genai_sdk/resources/providers/anthropic/__init__.py +33 -0
  56. digitalocean_genai_sdk/resources/providers/anthropic/anthropic.py +102 -0
  57. digitalocean_genai_sdk/resources/providers/anthropic/keys.py +662 -0
  58. digitalocean_genai_sdk/resources/providers/openai/__init__.py +33 -0
  59. digitalocean_genai_sdk/resources/providers/openai/keys.py +658 -0
  60. digitalocean_genai_sdk/resources/providers/openai/openai.py +102 -0
  61. digitalocean_genai_sdk/resources/providers/providers.py +134 -0
  62. digitalocean_genai_sdk/resources/regions.py +191 -0
  63. digitalocean_genai_sdk/types/__init__.py +57 -0
  64. digitalocean_genai_sdk/types/agent_create_params.py +39 -0
  65. digitalocean_genai_sdk/types/agent_create_response.py +16 -0
  66. digitalocean_genai_sdk/types/agent_delete_response.py +16 -0
  67. digitalocean_genai_sdk/types/agent_list_params.py +18 -0
  68. digitalocean_genai_sdk/types/agent_list_response.py +198 -0
  69. digitalocean_genai_sdk/types/agent_retrieve_response.py +16 -0
  70. digitalocean_genai_sdk/types/agent_update_params.py +65 -0
  71. digitalocean_genai_sdk/types/agent_update_response.py +16 -0
  72. digitalocean_genai_sdk/types/agent_update_status_params.py +16 -0
  73. digitalocean_genai_sdk/types/agent_update_status_response.py +16 -0
  74. digitalocean_genai_sdk/types/agents/__init__.py +31 -0
  75. digitalocean_genai_sdk/types/agents/api_key_create_params.py +15 -0
  76. digitalocean_genai_sdk/types/agents/api_key_create_response.py +12 -0
  77. digitalocean_genai_sdk/types/agents/api_key_delete_response.py +12 -0
  78. digitalocean_genai_sdk/types/agents/api_key_list_params.py +15 -0
  79. digitalocean_genai_sdk/types/agents/api_key_list_response.py +18 -0
  80. digitalocean_genai_sdk/types/agents/api_key_regenerate_response.py +12 -0
  81. digitalocean_genai_sdk/types/agents/api_key_update_params.py +19 -0
  82. digitalocean_genai_sdk/types/agents/api_key_update_response.py +12 -0
  83. digitalocean_genai_sdk/types/agents/api_link_knowledge_base_output.py +16 -0
  84. digitalocean_genai_sdk/types/agents/api_links.py +21 -0
  85. digitalocean_genai_sdk/types/agents/api_meta.py +15 -0
  86. digitalocean_genai_sdk/types/agents/child_agent_add_params.py +22 -0
  87. digitalocean_genai_sdk/types/agents/child_agent_add_response.py +14 -0
  88. digitalocean_genai_sdk/types/agents/child_agent_delete_response.py +13 -0
  89. digitalocean_genai_sdk/types/agents/child_agent_update_params.py +24 -0
  90. digitalocean_genai_sdk/types/agents/child_agent_update_response.py +18 -0
  91. digitalocean_genai_sdk/types/agents/child_agent_view_response.py +16 -0
  92. digitalocean_genai_sdk/types/agents/function_create_params.py +25 -0
  93. digitalocean_genai_sdk/types/agents/function_create_response.py +16 -0
  94. digitalocean_genai_sdk/types/agents/function_delete_response.py +16 -0
  95. digitalocean_genai_sdk/types/agents/function_update_params.py +29 -0
  96. digitalocean_genai_sdk/types/agents/function_update_response.py +16 -0
  97. digitalocean_genai_sdk/types/agents/knowledge_base_detach_response.py +16 -0
  98. digitalocean_genai_sdk/types/agents/version_list_params.py +15 -0
  99. digitalocean_genai_sdk/types/agents/version_list_response.py +118 -0
  100. digitalocean_genai_sdk/types/agents/version_update_params.py +15 -0
  101. digitalocean_genai_sdk/types/agents/version_update_response.py +30 -0
  102. digitalocean_genai_sdk/types/api_agent.py +263 -0
  103. digitalocean_genai_sdk/types/api_agent_api_key_info.py +22 -0
  104. digitalocean_genai_sdk/types/api_agreement.py +17 -0
  105. digitalocean_genai_sdk/types/api_anthropic_api_key_info.py +22 -0
  106. digitalocean_genai_sdk/types/api_deployment_visibility.py +9 -0
  107. digitalocean_genai_sdk/types/api_indexing_job.py +43 -0
  108. digitalocean_genai_sdk/types/api_key_list_params.py +42 -0
  109. digitalocean_genai_sdk/types/api_key_list_response.py +42 -0
  110. digitalocean_genai_sdk/types/api_keys/__init__.py +13 -0
  111. digitalocean_genai_sdk/types/api_keys/api_key_create_params.py +11 -0
  112. digitalocean_genai_sdk/types/api_keys/api_key_create_response.py +12 -0
  113. digitalocean_genai_sdk/types/api_keys/api_key_delete_response.py +12 -0
  114. digitalocean_genai_sdk/types/api_keys/api_key_list_params.py +15 -0
  115. digitalocean_genai_sdk/types/api_keys/api_key_list_response.py +18 -0
  116. digitalocean_genai_sdk/types/api_keys/api_key_update_params.py +15 -0
  117. digitalocean_genai_sdk/types/api_keys/api_key_update_regenerate_response.py +12 -0
  118. digitalocean_genai_sdk/types/api_keys/api_key_update_response.py +12 -0
  119. digitalocean_genai_sdk/types/api_keys/api_model_api_key_info.py +22 -0
  120. digitalocean_genai_sdk/types/api_knowledge_base.py +37 -0
  121. digitalocean_genai_sdk/types/api_model.py +57 -0
  122. digitalocean_genai_sdk/types/api_model_version.py +15 -0
  123. digitalocean_genai_sdk/types/api_openai_api_key_info.py +25 -0
  124. digitalocean_genai_sdk/types/api_retrieval_method.py +13 -0
  125. digitalocean_genai_sdk/types/auth/__init__.py +3 -0
  126. digitalocean_genai_sdk/types/auth/agents/__init__.py +6 -0
  127. digitalocean_genai_sdk/types/auth/agents/token_create_params.py +13 -0
  128. digitalocean_genai_sdk/types/auth/agents/token_create_response.py +13 -0
  129. digitalocean_genai_sdk/types/chat_completion_request_message_content_part_text_param.py +15 -0
  130. digitalocean_genai_sdk/types/chat_completion_token_logprob.py +57 -0
  131. digitalocean_genai_sdk/types/chat_create_completion_params.py +208 -0
  132. digitalocean_genai_sdk/types/chat_create_completion_response.py +81 -0
  133. digitalocean_genai_sdk/types/embedding_create_params.py +28 -0
  134. digitalocean_genai_sdk/types/embedding_create_response.py +41 -0
  135. digitalocean_genai_sdk/types/indexing_job_create_params.py +14 -0
  136. digitalocean_genai_sdk/types/indexing_job_create_response.py +12 -0
  137. digitalocean_genai_sdk/types/indexing_job_list_params.py +15 -0
  138. digitalocean_genai_sdk/types/indexing_job_list_response.py +18 -0
  139. digitalocean_genai_sdk/types/indexing_job_retrieve_data_sources_response.py +52 -0
  140. digitalocean_genai_sdk/types/indexing_job_retrieve_response.py +12 -0
  141. digitalocean_genai_sdk/types/indexing_job_update_cancel_params.py +14 -0
  142. digitalocean_genai_sdk/types/indexing_job_update_cancel_response.py +12 -0
  143. digitalocean_genai_sdk/types/knowledge_base_create_params.py +64 -0
  144. digitalocean_genai_sdk/types/knowledge_base_create_response.py +12 -0
  145. digitalocean_genai_sdk/types/knowledge_base_delete_response.py +11 -0
  146. digitalocean_genai_sdk/types/knowledge_base_list_params.py +15 -0
  147. digitalocean_genai_sdk/types/knowledge_base_list_response.py +18 -0
  148. digitalocean_genai_sdk/types/knowledge_base_retrieve_response.py +30 -0
  149. digitalocean_genai_sdk/types/knowledge_base_update_params.py +27 -0
  150. digitalocean_genai_sdk/types/knowledge_base_update_response.py +12 -0
  151. digitalocean_genai_sdk/types/knowledge_bases/__init__.py +16 -0
  152. digitalocean_genai_sdk/types/knowledge_bases/api_file_upload_data_source.py +15 -0
  153. digitalocean_genai_sdk/types/knowledge_bases/api_file_upload_data_source_param.py +15 -0
  154. digitalocean_genai_sdk/types/knowledge_bases/api_knowledge_base_data_source.py +35 -0
  155. digitalocean_genai_sdk/types/knowledge_bases/api_spaces_data_source.py +15 -0
  156. digitalocean_genai_sdk/types/knowledge_bases/api_spaces_data_source_param.py +15 -0
  157. digitalocean_genai_sdk/types/knowledge_bases/api_web_crawler_data_source.py +26 -0
  158. digitalocean_genai_sdk/types/knowledge_bases/api_web_crawler_data_source_param.py +25 -0
  159. digitalocean_genai_sdk/types/knowledge_bases/data_source_create_params.py +33 -0
  160. digitalocean_genai_sdk/types/knowledge_bases/data_source_create_response.py +12 -0
  161. digitalocean_genai_sdk/types/knowledge_bases/data_source_delete_response.py +13 -0
  162. digitalocean_genai_sdk/types/knowledge_bases/data_source_list_params.py +15 -0
  163. digitalocean_genai_sdk/types/knowledge_bases/data_source_list_response.py +18 -0
  164. digitalocean_genai_sdk/types/model.py +21 -0
  165. digitalocean_genai_sdk/types/model_list_response.py +15 -0
  166. digitalocean_genai_sdk/types/providers/__init__.py +3 -0
  167. digitalocean_genai_sdk/types/providers/anthropic/__init__.py +14 -0
  168. digitalocean_genai_sdk/types/providers/anthropic/key_create_params.py +13 -0
  169. digitalocean_genai_sdk/types/providers/anthropic/key_create_response.py +12 -0
  170. digitalocean_genai_sdk/types/providers/anthropic/key_delete_response.py +12 -0
  171. digitalocean_genai_sdk/types/providers/anthropic/key_list_agents_params.py +15 -0
  172. digitalocean_genai_sdk/types/providers/anthropic/key_list_agents_response.py +22 -0
  173. digitalocean_genai_sdk/types/providers/anthropic/key_list_params.py +15 -0
  174. digitalocean_genai_sdk/types/providers/anthropic/key_list_response.py +18 -0
  175. digitalocean_genai_sdk/types/providers/anthropic/key_retrieve_response.py +12 -0
  176. digitalocean_genai_sdk/types/providers/anthropic/key_update_params.py +17 -0
  177. digitalocean_genai_sdk/types/providers/anthropic/key_update_response.py +12 -0
  178. digitalocean_genai_sdk/types/providers/openai/__init__.py +14 -0
  179. digitalocean_genai_sdk/types/providers/openai/key_create_params.py +13 -0
  180. digitalocean_genai_sdk/types/providers/openai/key_create_response.py +12 -0
  181. digitalocean_genai_sdk/types/providers/openai/key_delete_response.py +12 -0
  182. digitalocean_genai_sdk/types/providers/openai/key_list_params.py +15 -0
  183. digitalocean_genai_sdk/types/providers/openai/key_list_response.py +18 -0
  184. digitalocean_genai_sdk/types/providers/openai/key_retrieve_agents_params.py +15 -0
  185. digitalocean_genai_sdk/types/providers/openai/key_retrieve_agents_response.py +22 -0
  186. digitalocean_genai_sdk/types/providers/openai/key_retrieve_response.py +12 -0
  187. digitalocean_genai_sdk/types/providers/openai/key_update_params.py +17 -0
  188. digitalocean_genai_sdk/types/providers/openai/key_update_response.py +12 -0
  189. digitalocean_genai_sdk/types/region_list_params.py +15 -0
  190. digitalocean_genai_sdk/types/region_list_response.py +23 -0
@@ -0,0 +1,1949 @@
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+ import json
5
+ import time
6
+ import uuid
7
+ import email
8
+ import asyncio
9
+ import inspect
10
+ import logging
11
+ import platform
12
+ import email.utils
13
+ from types import TracebackType
14
+ from random import random
15
+ from typing import (
16
+ TYPE_CHECKING,
17
+ Any,
18
+ Dict,
19
+ Type,
20
+ Union,
21
+ Generic,
22
+ Mapping,
23
+ TypeVar,
24
+ Iterable,
25
+ Iterator,
26
+ Optional,
27
+ Generator,
28
+ AsyncIterator,
29
+ cast,
30
+ overload,
31
+ )
32
+ from typing_extensions import Literal, override, get_origin
33
+
34
+ import anyio
35
+ import httpx
36
+ import distro
37
+ import pydantic
38
+ from httpx import URL
39
+ from pydantic import PrivateAttr
40
+
41
+ from . import _exceptions
42
+ from ._qs import Querystring
43
+ from ._files import to_httpx_files, async_to_httpx_files
44
+ from ._types import (
45
+ NOT_GIVEN,
46
+ Body,
47
+ Omit,
48
+ Query,
49
+ Headers,
50
+ Timeout,
51
+ NotGiven,
52
+ ResponseT,
53
+ AnyMapping,
54
+ PostParser,
55
+ RequestFiles,
56
+ HttpxSendArgs,
57
+ RequestOptions,
58
+ HttpxRequestFiles,
59
+ ModelBuilderProtocol,
60
+ )
61
+ from ._utils import is_dict, is_list, asyncify, is_given, lru_cache, is_mapping
62
+ from ._compat import PYDANTIC_V2, model_copy, model_dump
63
+ from ._models import GenericModel, FinalRequestOptions, validate_type, construct_type
64
+ from ._response import (
65
+ APIResponse,
66
+ BaseAPIResponse,
67
+ AsyncAPIResponse,
68
+ extract_response_type,
69
+ )
70
+ from ._constants import (
71
+ DEFAULT_TIMEOUT,
72
+ MAX_RETRY_DELAY,
73
+ DEFAULT_MAX_RETRIES,
74
+ INITIAL_RETRY_DELAY,
75
+ RAW_RESPONSE_HEADER,
76
+ OVERRIDE_CAST_TO_HEADER,
77
+ DEFAULT_CONNECTION_LIMITS,
78
+ )
79
+ from ._streaming import Stream, SSEDecoder, AsyncStream, SSEBytesDecoder
80
+ from ._exceptions import (
81
+ APIStatusError,
82
+ APITimeoutError,
83
+ APIConnectionError,
84
+ APIResponseValidationError,
85
+ )
86
+
87
+ log: logging.Logger = logging.getLogger(__name__)
88
+
89
+ # TODO: make base page type vars covariant
90
+ SyncPageT = TypeVar("SyncPageT", bound="BaseSyncPage[Any]")
91
+ AsyncPageT = TypeVar("AsyncPageT", bound="BaseAsyncPage[Any]")
92
+
93
+
94
+ _T = TypeVar("_T")
95
+ _T_co = TypeVar("_T_co", covariant=True)
96
+
97
+ _StreamT = TypeVar("_StreamT", bound=Stream[Any])
98
+ _AsyncStreamT = TypeVar("_AsyncStreamT", bound=AsyncStream[Any])
99
+
100
+ if TYPE_CHECKING:
101
+ from httpx._config import (
102
+ DEFAULT_TIMEOUT_CONFIG, # pyright: ignore[reportPrivateImportUsage]
103
+ )
104
+
105
+ HTTPX_DEFAULT_TIMEOUT = DEFAULT_TIMEOUT_CONFIG
106
+ else:
107
+ try:
108
+ from httpx._config import DEFAULT_TIMEOUT_CONFIG as HTTPX_DEFAULT_TIMEOUT
109
+ except ImportError:
110
+ # taken from https://github.com/encode/httpx/blob/3ba5fe0d7ac70222590e759c31442b1cab263791/httpx/_config.py#L366
111
+ HTTPX_DEFAULT_TIMEOUT = Timeout(5.0)
112
+
113
+
114
+ class PageInfo:
115
+ """Stores the necessary information to build the request to retrieve the next page.
116
+
117
+ Either `url` or `params` must be set.
118
+ """
119
+
120
+ url: URL | NotGiven
121
+ params: Query | NotGiven
122
+ json: Body | NotGiven
123
+
124
+ @overload
125
+ def __init__(
126
+ self,
127
+ *,
128
+ url: URL,
129
+ ) -> None: ...
130
+
131
+ @overload
132
+ def __init__(
133
+ self,
134
+ *,
135
+ params: Query,
136
+ ) -> None: ...
137
+
138
+ @overload
139
+ def __init__(
140
+ self,
141
+ *,
142
+ json: Body,
143
+ ) -> None: ...
144
+
145
+ def __init__(
146
+ self,
147
+ *,
148
+ url: URL | NotGiven = NOT_GIVEN,
149
+ json: Body | NotGiven = NOT_GIVEN,
150
+ params: Query | NotGiven = NOT_GIVEN,
151
+ ) -> None:
152
+ self.url = url
153
+ self.json = json
154
+ self.params = params
155
+
156
+ @override
157
+ def __repr__(self) -> str:
158
+ if self.url:
159
+ return f"{self.__class__.__name__}(url={self.url})"
160
+ if self.json:
161
+ return f"{self.__class__.__name__}(json={self.json})"
162
+ return f"{self.__class__.__name__}(params={self.params})"
163
+
164
+
165
+ class BasePage(GenericModel, Generic[_T]):
166
+ """
167
+ Defines the core interface for pagination.
168
+
169
+ Type Args:
170
+ ModelT: The pydantic model that represents an item in the response.
171
+
172
+ Methods:
173
+ has_next_page(): Check if there is another page available
174
+ next_page_info(): Get the necessary information to make a request for the next page
175
+ """
176
+
177
+ _options: FinalRequestOptions = PrivateAttr()
178
+ _model: Type[_T] = PrivateAttr()
179
+
180
+ def has_next_page(self) -> bool:
181
+ items = self._get_page_items()
182
+ if not items:
183
+ return False
184
+ return self.next_page_info() is not None
185
+
186
+ def next_page_info(self) -> Optional[PageInfo]: ...
187
+
188
+ def _get_page_items(self) -> Iterable[_T]: # type: ignore[empty-body]
189
+ ...
190
+
191
+ def _params_from_url(self, url: URL) -> httpx.QueryParams:
192
+ # TODO: do we have to preprocess params here?
193
+ return httpx.QueryParams(cast(Any, self._options.params)).merge(url.params)
194
+
195
+ def _info_to_options(self, info: PageInfo) -> FinalRequestOptions:
196
+ options = model_copy(self._options)
197
+ options._strip_raw_response_header()
198
+
199
+ if not isinstance(info.params, NotGiven):
200
+ options.params = {**options.params, **info.params}
201
+ return options
202
+
203
+ if not isinstance(info.url, NotGiven):
204
+ params = self._params_from_url(info.url)
205
+ url = info.url.copy_with(params=params)
206
+ options.params = dict(url.params)
207
+ options.url = str(url)
208
+ return options
209
+
210
+ if not isinstance(info.json, NotGiven):
211
+ if not is_mapping(info.json):
212
+ raise TypeError("Pagination is only supported with mappings")
213
+
214
+ if not options.json_data:
215
+ options.json_data = {**info.json}
216
+ else:
217
+ if not is_mapping(options.json_data):
218
+ raise TypeError("Pagination is only supported with mappings")
219
+
220
+ options.json_data = {**options.json_data, **info.json}
221
+ return options
222
+
223
+ raise ValueError("Unexpected PageInfo state")
224
+
225
+
226
+ class BaseSyncPage(BasePage[_T], Generic[_T]):
227
+ _client: SyncAPIClient = pydantic.PrivateAttr()
228
+
229
+ def _set_private_attributes(
230
+ self,
231
+ client: SyncAPIClient,
232
+ model: Type[_T],
233
+ options: FinalRequestOptions,
234
+ ) -> None:
235
+ if PYDANTIC_V2 and getattr(self, "__pydantic_private__", None) is None:
236
+ self.__pydantic_private__ = {}
237
+
238
+ self._model = model
239
+ self._client = client
240
+ self._options = options
241
+
242
+ # Pydantic uses a custom `__iter__` method to support casting BaseModels
243
+ # to dictionaries. e.g. dict(model).
244
+ # As we want to support `for item in page`, this is inherently incompatible
245
+ # with the default pydantic behaviour. It is not possible to support both
246
+ # use cases at once. Fortunately, this is not a big deal as all other pydantic
247
+ # methods should continue to work as expected as there is an alternative method
248
+ # to cast a model to a dictionary, model.dict(), which is used internally
249
+ # by pydantic.
250
+ def __iter__(self) -> Iterator[_T]: # type: ignore
251
+ for page in self.iter_pages():
252
+ for item in page._get_page_items():
253
+ yield item
254
+
255
+ def iter_pages(self: SyncPageT) -> Iterator[SyncPageT]:
256
+ page = self
257
+ while True:
258
+ yield page
259
+ if page.has_next_page():
260
+ page = page.get_next_page()
261
+ else:
262
+ return
263
+
264
+ def get_next_page(self: SyncPageT) -> SyncPageT:
265
+ info = self.next_page_info()
266
+ if not info:
267
+ raise RuntimeError(
268
+ "No next page expected; please check `.has_next_page()` before calling `.get_next_page()`."
269
+ )
270
+
271
+ options = self._info_to_options(info)
272
+ return self._client._request_api_list(self._model, page=self.__class__, options=options)
273
+
274
+
275
+ class AsyncPaginator(Generic[_T, AsyncPageT]):
276
+ def __init__(
277
+ self,
278
+ client: AsyncAPIClient,
279
+ options: FinalRequestOptions,
280
+ page_cls: Type[AsyncPageT],
281
+ model: Type[_T],
282
+ ) -> None:
283
+ self._model = model
284
+ self._client = client
285
+ self._options = options
286
+ self._page_cls = page_cls
287
+
288
+ def __await__(self) -> Generator[Any, None, AsyncPageT]:
289
+ return self._get_page().__await__()
290
+
291
+ async def _get_page(self) -> AsyncPageT:
292
+ def _parser(resp: AsyncPageT) -> AsyncPageT:
293
+ resp._set_private_attributes(
294
+ model=self._model,
295
+ options=self._options,
296
+ client=self._client,
297
+ )
298
+ return resp
299
+
300
+ self._options.post_parser = _parser
301
+
302
+ return await self._client.request(self._page_cls, self._options)
303
+
304
+ async def __aiter__(self) -> AsyncIterator[_T]:
305
+ # https://github.com/microsoft/pyright/issues/3464
306
+ page = cast(
307
+ AsyncPageT,
308
+ await self, # type: ignore
309
+ )
310
+ async for item in page:
311
+ yield item
312
+
313
+
314
+ class BaseAsyncPage(BasePage[_T], Generic[_T]):
315
+ _client: AsyncAPIClient = pydantic.PrivateAttr()
316
+
317
+ def _set_private_attributes(
318
+ self,
319
+ model: Type[_T],
320
+ client: AsyncAPIClient,
321
+ options: FinalRequestOptions,
322
+ ) -> None:
323
+ if PYDANTIC_V2 and getattr(self, "__pydantic_private__", None) is None:
324
+ self.__pydantic_private__ = {}
325
+
326
+ self._model = model
327
+ self._client = client
328
+ self._options = options
329
+
330
+ async def __aiter__(self) -> AsyncIterator[_T]:
331
+ async for page in self.iter_pages():
332
+ for item in page._get_page_items():
333
+ yield item
334
+
335
+ async def iter_pages(self: AsyncPageT) -> AsyncIterator[AsyncPageT]:
336
+ page = self
337
+ while True:
338
+ yield page
339
+ if page.has_next_page():
340
+ page = await page.get_next_page()
341
+ else:
342
+ return
343
+
344
+ async def get_next_page(self: AsyncPageT) -> AsyncPageT:
345
+ info = self.next_page_info()
346
+ if not info:
347
+ raise RuntimeError(
348
+ "No next page expected; please check `.has_next_page()` before calling `.get_next_page()`."
349
+ )
350
+
351
+ options = self._info_to_options(info)
352
+ return await self._client._request_api_list(self._model, page=self.__class__, options=options)
353
+
354
+
355
+ _HttpxClientT = TypeVar("_HttpxClientT", bound=Union[httpx.Client, httpx.AsyncClient])
356
+ _DefaultStreamT = TypeVar("_DefaultStreamT", bound=Union[Stream[Any], AsyncStream[Any]])
357
+
358
+
359
+ class BaseClient(Generic[_HttpxClientT, _DefaultStreamT]):
360
+ _client: _HttpxClientT
361
+ _version: str
362
+ _base_url: URL
363
+ max_retries: int
364
+ timeout: Union[float, Timeout, None]
365
+ _strict_response_validation: bool
366
+ _idempotency_header: str | None
367
+ _default_stream_cls: type[_DefaultStreamT] | None = None
368
+
369
+ def __init__(
370
+ self,
371
+ *,
372
+ version: str,
373
+ base_url: str | URL,
374
+ _strict_response_validation: bool,
375
+ max_retries: int = DEFAULT_MAX_RETRIES,
376
+ timeout: float | Timeout | None = DEFAULT_TIMEOUT,
377
+ custom_headers: Mapping[str, str] | None = None,
378
+ custom_query: Mapping[str, object] | None = None,
379
+ ) -> None:
380
+ self._version = version
381
+ self._base_url = self._enforce_trailing_slash(URL(base_url))
382
+ self.max_retries = max_retries
383
+ self.timeout = timeout
384
+ self._custom_headers = custom_headers or {}
385
+ self._custom_query = custom_query or {}
386
+ self._strict_response_validation = _strict_response_validation
387
+ self._idempotency_header = None
388
+ self._platform: Platform | None = None
389
+
390
+ if max_retries is None: # pyright: ignore[reportUnnecessaryComparison]
391
+ raise TypeError(
392
+ "max_retries cannot be None. If you want to disable retries, pass `0`; if you want unlimited retries, pass `math.inf` or a very high number; if you want the default behavior, pass `digitalocean_genai_sdk.DEFAULT_MAX_RETRIES`"
393
+ )
394
+
395
+ def _enforce_trailing_slash(self, url: URL) -> URL:
396
+ if url.raw_path.endswith(b"/"):
397
+ return url
398
+ return url.copy_with(raw_path=url.raw_path + b"/")
399
+
400
+ def _make_status_error_from_response(
401
+ self,
402
+ response: httpx.Response,
403
+ ) -> APIStatusError:
404
+ if response.is_closed and not response.is_stream_consumed:
405
+ # We can't read the response body as it has been closed
406
+ # before it was read. This can happen if an event hook
407
+ # raises a status error.
408
+ body = None
409
+ err_msg = f"Error code: {response.status_code}"
410
+ else:
411
+ err_text = response.text.strip()
412
+ body = err_text
413
+
414
+ try:
415
+ body = json.loads(err_text)
416
+ err_msg = f"Error code: {response.status_code} - {body}"
417
+ except Exception:
418
+ err_msg = err_text or f"Error code: {response.status_code}"
419
+
420
+ return self._make_status_error(err_msg, body=body, response=response)
421
+
422
+ def _make_status_error(
423
+ self,
424
+ err_msg: str,
425
+ *,
426
+ body: object,
427
+ response: httpx.Response,
428
+ ) -> _exceptions.APIStatusError:
429
+ raise NotImplementedError()
430
+
431
+ def _build_headers(self, options: FinalRequestOptions, *, retries_taken: int = 0) -> httpx.Headers:
432
+ custom_headers = options.headers or {}
433
+ headers_dict = _merge_mappings(self.default_headers, custom_headers)
434
+ self._validate_headers(headers_dict, custom_headers)
435
+
436
+ # headers are case-insensitive while dictionaries are not.
437
+ headers = httpx.Headers(headers_dict)
438
+
439
+ idempotency_header = self._idempotency_header
440
+ if idempotency_header and options.idempotency_key and idempotency_header not in headers:
441
+ headers[idempotency_header] = options.idempotency_key
442
+
443
+ # Don't set these headers if they were already set or removed by the caller. We check
444
+ # `custom_headers`, which can contain `Omit()`, instead of `headers` to account for the removal case.
445
+ lower_custom_headers = [header.lower() for header in custom_headers]
446
+ if "x-stainless-retry-count" not in lower_custom_headers:
447
+ headers["x-stainless-retry-count"] = str(retries_taken)
448
+ if "x-stainless-read-timeout" not in lower_custom_headers:
449
+ timeout = self.timeout if isinstance(options.timeout, NotGiven) else options.timeout
450
+ if isinstance(timeout, Timeout):
451
+ timeout = timeout.read
452
+ if timeout is not None:
453
+ headers["x-stainless-read-timeout"] = str(timeout)
454
+
455
+ return headers
456
+
457
+ def _prepare_url(self, url: str) -> URL:
458
+ """
459
+ Merge a URL argument together with any 'base_url' on the client,
460
+ to create the URL used for the outgoing request.
461
+ """
462
+ # Copied from httpx's `_merge_url` method.
463
+ merge_url = URL(url)
464
+ if merge_url.is_relative_url:
465
+ merge_raw_path = self.base_url.raw_path + merge_url.raw_path.lstrip(b"/")
466
+ return self.base_url.copy_with(raw_path=merge_raw_path)
467
+
468
+ return merge_url
469
+
470
+ def _make_sse_decoder(self) -> SSEDecoder | SSEBytesDecoder:
471
+ return SSEDecoder()
472
+
473
+ def _build_request(
474
+ self,
475
+ options: FinalRequestOptions,
476
+ *,
477
+ retries_taken: int = 0,
478
+ ) -> httpx.Request:
479
+ if log.isEnabledFor(logging.DEBUG):
480
+ log.debug("Request options: %s", model_dump(options, exclude_unset=True))
481
+
482
+ kwargs: dict[str, Any] = {}
483
+
484
+ json_data = options.json_data
485
+ if options.extra_json is not None:
486
+ if json_data is None:
487
+ json_data = cast(Body, options.extra_json)
488
+ elif is_mapping(json_data):
489
+ json_data = _merge_mappings(json_data, options.extra_json)
490
+ else:
491
+ raise RuntimeError(f"Unexpected JSON data type, {type(json_data)}, cannot merge with `extra_body`")
492
+
493
+ headers = self._build_headers(options, retries_taken=retries_taken)
494
+ params = _merge_mappings(self.default_query, options.params)
495
+ content_type = headers.get("Content-Type")
496
+ files = options.files
497
+
498
+ # If the given Content-Type header is multipart/form-data then it
499
+ # has to be removed so that httpx can generate the header with
500
+ # additional information for us as it has to be in this form
501
+ # for the server to be able to correctly parse the request:
502
+ # multipart/form-data; boundary=---abc--
503
+ if content_type is not None and content_type.startswith("multipart/form-data"):
504
+ if "boundary" not in content_type:
505
+ # only remove the header if the boundary hasn't been explicitly set
506
+ # as the caller doesn't want httpx to come up with their own boundary
507
+ headers.pop("Content-Type")
508
+
509
+ # As we are now sending multipart/form-data instead of application/json
510
+ # we need to tell httpx to use it, https://www.python-httpx.org/advanced/clients/#multipart-file-encoding
511
+ if json_data:
512
+ if not is_dict(json_data):
513
+ raise TypeError(
514
+ f"Expected query input to be a dictionary for multipart requests but got {type(json_data)} instead."
515
+ )
516
+ kwargs["data"] = self._serialize_multipartform(json_data)
517
+
518
+ # httpx determines whether or not to send a "multipart/form-data"
519
+ # request based on the truthiness of the "files" argument.
520
+ # This gets around that issue by generating a dict value that
521
+ # evaluates to true.
522
+ #
523
+ # https://github.com/encode/httpx/discussions/2399#discussioncomment-3814186
524
+ if not files:
525
+ files = cast(HttpxRequestFiles, ForceMultipartDict())
526
+
527
+ prepared_url = self._prepare_url(options.url)
528
+ if "_" in prepared_url.host:
529
+ # work around https://github.com/encode/httpx/discussions/2880
530
+ kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")}
531
+
532
+ # TODO: report this error to httpx
533
+ return self._client.build_request( # pyright: ignore[reportUnknownMemberType]
534
+ headers=headers,
535
+ timeout=self.timeout if isinstance(options.timeout, NotGiven) else options.timeout,
536
+ method=options.method,
537
+ url=prepared_url,
538
+ # the `Query` type that we use is incompatible with qs'
539
+ # `Params` type as it needs to be typed as `Mapping[str, object]`
540
+ # so that passing a `TypedDict` doesn't cause an error.
541
+ # https://github.com/microsoft/pyright/issues/3526#event-6715453066
542
+ params=self.qs.stringify(cast(Mapping[str, Any], params)) if params else None,
543
+ json=json_data if is_given(json_data) else None,
544
+ files=files,
545
+ **kwargs,
546
+ )
547
+
548
+ def _serialize_multipartform(self, data: Mapping[object, object]) -> dict[str, object]:
549
+ items = self.qs.stringify_items(
550
+ # TODO: type ignore is required as stringify_items is well typed but we can't be
551
+ # well typed without heavy validation.
552
+ data, # type: ignore
553
+ array_format="brackets",
554
+ )
555
+ serialized: dict[str, object] = {}
556
+ for key, value in items:
557
+ existing = serialized.get(key)
558
+
559
+ if not existing:
560
+ serialized[key] = value
561
+ continue
562
+
563
+ # If a value has already been set for this key then that
564
+ # means we're sending data like `array[]=[1, 2, 3]` and we
565
+ # need to tell httpx that we want to send multiple values with
566
+ # the same key which is done by using a list or a tuple.
567
+ #
568
+ # Note: 2d arrays should never result in the same key at both
569
+ # levels so it's safe to assume that if the value is a list,
570
+ # it was because we changed it to be a list.
571
+ if is_list(existing):
572
+ existing.append(value)
573
+ else:
574
+ serialized[key] = [existing, value]
575
+
576
+ return serialized
577
+
578
+ def _maybe_override_cast_to(self, cast_to: type[ResponseT], options: FinalRequestOptions) -> type[ResponseT]:
579
+ if not is_given(options.headers):
580
+ return cast_to
581
+
582
+ # make a copy of the headers so we don't mutate user-input
583
+ headers = dict(options.headers)
584
+
585
+ # we internally support defining a temporary header to override the
586
+ # default `cast_to` type for use with `.with_raw_response` and `.with_streaming_response`
587
+ # see _response.py for implementation details
588
+ override_cast_to = headers.pop(OVERRIDE_CAST_TO_HEADER, NOT_GIVEN)
589
+ if is_given(override_cast_to):
590
+ options.headers = headers
591
+ return cast(Type[ResponseT], override_cast_to)
592
+
593
+ return cast_to
594
+
595
+ def _should_stream_response_body(self, request: httpx.Request) -> bool:
596
+ return request.headers.get(RAW_RESPONSE_HEADER) == "stream" # type: ignore[no-any-return]
597
+
598
+ def _process_response_data(
599
+ self,
600
+ *,
601
+ data: object,
602
+ cast_to: type[ResponseT],
603
+ response: httpx.Response,
604
+ ) -> ResponseT:
605
+ if data is None:
606
+ return cast(ResponseT, None)
607
+
608
+ if cast_to is object:
609
+ return cast(ResponseT, data)
610
+
611
+ try:
612
+ if inspect.isclass(cast_to) and issubclass(cast_to, ModelBuilderProtocol):
613
+ return cast(ResponseT, cast_to.build(response=response, data=data))
614
+
615
+ if self._strict_response_validation:
616
+ return cast(ResponseT, validate_type(type_=cast_to, value=data))
617
+
618
+ return cast(ResponseT, construct_type(type_=cast_to, value=data))
619
+ except pydantic.ValidationError as err:
620
+ raise APIResponseValidationError(response=response, body=data) from err
621
+
622
+ @property
623
+ def qs(self) -> Querystring:
624
+ return Querystring()
625
+
626
+ @property
627
+ def custom_auth(self) -> httpx.Auth | None:
628
+ return None
629
+
630
+ @property
631
+ def auth_headers(self) -> dict[str, str]:
632
+ return {}
633
+
634
+ @property
635
+ def default_headers(self) -> dict[str, str | Omit]:
636
+ return {
637
+ "Accept": "application/json",
638
+ "Content-Type": "application/json",
639
+ "User-Agent": self.user_agent,
640
+ **self.platform_headers(),
641
+ **self.auth_headers,
642
+ **self._custom_headers,
643
+ }
644
+
645
+ @property
646
+ def default_query(self) -> dict[str, object]:
647
+ return {
648
+ **self._custom_query,
649
+ }
650
+
651
+ def _validate_headers(
652
+ self,
653
+ headers: Headers, # noqa: ARG002
654
+ custom_headers: Headers, # noqa: ARG002
655
+ ) -> None:
656
+ """Validate the given default headers and custom headers.
657
+
658
+ Does nothing by default.
659
+ """
660
+ return
661
+
662
+ @property
663
+ def user_agent(self) -> str:
664
+ return f"{self.__class__.__name__}/Python {self._version}"
665
+
666
+ @property
667
+ def base_url(self) -> URL:
668
+ return self._base_url
669
+
670
+ @base_url.setter
671
+ def base_url(self, url: URL | str) -> None:
672
+ self._base_url = self._enforce_trailing_slash(url if isinstance(url, URL) else URL(url))
673
+
674
+ def platform_headers(self) -> Dict[str, str]:
675
+ # the actual implementation is in a separate `lru_cache` decorated
676
+ # function because adding `lru_cache` to methods will leak memory
677
+ # https://github.com/python/cpython/issues/88476
678
+ return platform_headers(self._version, platform=self._platform)
679
+
680
+ def _parse_retry_after_header(self, response_headers: Optional[httpx.Headers] = None) -> float | None:
681
+ """Returns a float of the number of seconds (not milliseconds) to wait after retrying, or None if unspecified.
682
+
683
+ About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
684
+ See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After#syntax
685
+ """
686
+ if response_headers is None:
687
+ return None
688
+
689
+ # First, try the non-standard `retry-after-ms` header for milliseconds,
690
+ # which is more precise than integer-seconds `retry-after`
691
+ try:
692
+ retry_ms_header = response_headers.get("retry-after-ms", None)
693
+ return float(retry_ms_header) / 1000
694
+ except (TypeError, ValueError):
695
+ pass
696
+
697
+ # Next, try parsing `retry-after` header as seconds (allowing nonstandard floats).
698
+ retry_header = response_headers.get("retry-after")
699
+ try:
700
+ # note: the spec indicates that this should only ever be an integer
701
+ # but if someone sends a float there's no reason for us to not respect it
702
+ return float(retry_header)
703
+ except (TypeError, ValueError):
704
+ pass
705
+
706
+ # Last, try parsing `retry-after` as a date.
707
+ retry_date_tuple = email.utils.parsedate_tz(retry_header)
708
+ if retry_date_tuple is None:
709
+ return None
710
+
711
+ retry_date = email.utils.mktime_tz(retry_date_tuple)
712
+ return float(retry_date - time.time())
713
+
714
+ def _calculate_retry_timeout(
715
+ self,
716
+ remaining_retries: int,
717
+ options: FinalRequestOptions,
718
+ response_headers: Optional[httpx.Headers] = None,
719
+ ) -> float:
720
+ max_retries = options.get_max_retries(self.max_retries)
721
+
722
+ # If the API asks us to wait a certain amount of time (and it's a reasonable amount), just do what it says.
723
+ retry_after = self._parse_retry_after_header(response_headers)
724
+ if retry_after is not None and 0 < retry_after <= 60:
725
+ return retry_after
726
+
727
+ # Also cap retry count to 1000 to avoid any potential overflows with `pow`
728
+ nb_retries = min(max_retries - remaining_retries, 1000)
729
+
730
+ # Apply exponential backoff, but not more than the max.
731
+ sleep_seconds = min(INITIAL_RETRY_DELAY * pow(2.0, nb_retries), MAX_RETRY_DELAY)
732
+
733
+ # Apply some jitter, plus-or-minus half a second.
734
+ jitter = 1 - 0.25 * random()
735
+ timeout = sleep_seconds * jitter
736
+ return timeout if timeout >= 0 else 0
737
+
738
+ def _should_retry(self, response: httpx.Response) -> bool:
739
+ # Note: this is not a standard header
740
+ should_retry_header = response.headers.get("x-should-retry")
741
+
742
+ # If the server explicitly says whether or not to retry, obey.
743
+ if should_retry_header == "true":
744
+ log.debug("Retrying as header `x-should-retry` is set to `true`")
745
+ return True
746
+ if should_retry_header == "false":
747
+ log.debug("Not retrying as header `x-should-retry` is set to `false`")
748
+ return False
749
+
750
+ # Retry on request timeouts.
751
+ if response.status_code == 408:
752
+ log.debug("Retrying due to status code %i", response.status_code)
753
+ return True
754
+
755
+ # Retry on lock timeouts.
756
+ if response.status_code == 409:
757
+ log.debug("Retrying due to status code %i", response.status_code)
758
+ return True
759
+
760
+ # Retry on rate limits.
761
+ if response.status_code == 429:
762
+ log.debug("Retrying due to status code %i", response.status_code)
763
+ return True
764
+
765
+ # Retry internal errors.
766
+ if response.status_code >= 500:
767
+ log.debug("Retrying due to status code %i", response.status_code)
768
+ return True
769
+
770
+ log.debug("Not retrying")
771
+ return False
772
+
773
+ def _idempotency_key(self) -> str:
774
+ return f"stainless-python-retry-{uuid.uuid4()}"
775
+
776
+
777
+ class _DefaultHttpxClient(httpx.Client):
778
+ def __init__(self, **kwargs: Any) -> None:
779
+ kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
780
+ kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS)
781
+ kwargs.setdefault("follow_redirects", True)
782
+ super().__init__(**kwargs)
783
+
784
+
785
+ if TYPE_CHECKING:
786
+ DefaultHttpxClient = httpx.Client
787
+ """An alias to `httpx.Client` that provides the same defaults that this SDK
788
+ uses internally.
789
+
790
+ This is useful because overriding the `http_client` with your own instance of
791
+ `httpx.Client` will result in httpx's defaults being used, not ours.
792
+ """
793
+ else:
794
+ DefaultHttpxClient = _DefaultHttpxClient
795
+
796
+
797
+ class SyncHttpxClientWrapper(DefaultHttpxClient):
798
+ def __del__(self) -> None:
799
+ if self.is_closed:
800
+ return
801
+
802
+ try:
803
+ self.close()
804
+ except Exception:
805
+ pass
806
+
807
+
808
+ class SyncAPIClient(BaseClient[httpx.Client, Stream[Any]]):
809
+ _client: httpx.Client
810
+ _default_stream_cls: type[Stream[Any]] | None = None
811
+
812
+ def __init__(
813
+ self,
814
+ *,
815
+ version: str,
816
+ base_url: str | URL,
817
+ max_retries: int = DEFAULT_MAX_RETRIES,
818
+ timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
819
+ http_client: httpx.Client | None = None,
820
+ custom_headers: Mapping[str, str] | None = None,
821
+ custom_query: Mapping[str, object] | None = None,
822
+ _strict_response_validation: bool,
823
+ ) -> None:
824
+ if not is_given(timeout):
825
+ # if the user passed in a custom http client with a non-default
826
+ # timeout set then we use that timeout.
827
+ #
828
+ # note: there is an edge case here where the user passes in a client
829
+ # where they've explicitly set the timeout to match the default timeout
830
+ # as this check is structural, meaning that we'll think they didn't
831
+ # pass in a timeout and will ignore it
832
+ if http_client and http_client.timeout != HTTPX_DEFAULT_TIMEOUT:
833
+ timeout = http_client.timeout
834
+ else:
835
+ timeout = DEFAULT_TIMEOUT
836
+
837
+ if http_client is not None and not isinstance(http_client, httpx.Client): # pyright: ignore[reportUnnecessaryIsInstance]
838
+ raise TypeError(
839
+ f"Invalid `http_client` argument; Expected an instance of `httpx.Client` but got {type(http_client)}"
840
+ )
841
+
842
+ super().__init__(
843
+ version=version,
844
+ # cast to a valid type because mypy doesn't understand our type narrowing
845
+ timeout=cast(Timeout, timeout),
846
+ base_url=base_url,
847
+ max_retries=max_retries,
848
+ custom_query=custom_query,
849
+ custom_headers=custom_headers,
850
+ _strict_response_validation=_strict_response_validation,
851
+ )
852
+ self._client = http_client or SyncHttpxClientWrapper(
853
+ base_url=base_url,
854
+ # cast to a valid type because mypy doesn't understand our type narrowing
855
+ timeout=cast(Timeout, timeout),
856
+ )
857
+
858
+ def is_closed(self) -> bool:
859
+ return self._client.is_closed
860
+
861
+ def close(self) -> None:
862
+ """Close the underlying HTTPX client.
863
+
864
+ The client will *not* be usable after this.
865
+ """
866
+ # If an error is thrown while constructing a client, self._client
867
+ # may not be present
868
+ if hasattr(self, "_client"):
869
+ self._client.close()
870
+
871
+ def __enter__(self: _T) -> _T:
872
+ return self
873
+
874
+ def __exit__(
875
+ self,
876
+ exc_type: type[BaseException] | None,
877
+ exc: BaseException | None,
878
+ exc_tb: TracebackType | None,
879
+ ) -> None:
880
+ self.close()
881
+
882
+ def _prepare_options(
883
+ self,
884
+ options: FinalRequestOptions, # noqa: ARG002
885
+ ) -> FinalRequestOptions:
886
+ """Hook for mutating the given options"""
887
+ return options
888
+
889
+ def _prepare_request(
890
+ self,
891
+ request: httpx.Request, # noqa: ARG002
892
+ ) -> None:
893
+ """This method is used as a callback for mutating the `Request` object
894
+ after it has been constructed.
895
+ This is useful for cases where you want to add certain headers based off of
896
+ the request properties, e.g. `url`, `method` etc.
897
+ """
898
+ return None
899
+
900
+ @overload
901
+ def request(
902
+ self,
903
+ cast_to: Type[ResponseT],
904
+ options: FinalRequestOptions,
905
+ *,
906
+ stream: Literal[True],
907
+ stream_cls: Type[_StreamT],
908
+ ) -> _StreamT: ...
909
+
910
+ @overload
911
+ def request(
912
+ self,
913
+ cast_to: Type[ResponseT],
914
+ options: FinalRequestOptions,
915
+ *,
916
+ stream: Literal[False] = False,
917
+ ) -> ResponseT: ...
918
+
919
+ @overload
920
+ def request(
921
+ self,
922
+ cast_to: Type[ResponseT],
923
+ options: FinalRequestOptions,
924
+ *,
925
+ stream: bool = False,
926
+ stream_cls: Type[_StreamT] | None = None,
927
+ ) -> ResponseT | _StreamT: ...
928
+
929
+ def request(
930
+ self,
931
+ cast_to: Type[ResponseT],
932
+ options: FinalRequestOptions,
933
+ *,
934
+ stream: bool = False,
935
+ stream_cls: type[_StreamT] | None = None,
936
+ ) -> ResponseT | _StreamT:
937
+ cast_to = self._maybe_override_cast_to(cast_to, options)
938
+
939
+ # create a copy of the options we were given so that if the
940
+ # options are mutated later & we then retry, the retries are
941
+ # given the original options
942
+ input_options = model_copy(options)
943
+ if input_options.idempotency_key is None and input_options.method.lower() != "get":
944
+ # ensure the idempotency key is reused between requests
945
+ input_options.idempotency_key = self._idempotency_key()
946
+
947
+ response: httpx.Response | None = None
948
+ max_retries = input_options.get_max_retries(self.max_retries)
949
+
950
+ retries_taken = 0
951
+ for retries_taken in range(max_retries + 1):
952
+ options = model_copy(input_options)
953
+ options = self._prepare_options(options)
954
+
955
+ remaining_retries = max_retries - retries_taken
956
+ request = self._build_request(options, retries_taken=retries_taken)
957
+ self._prepare_request(request)
958
+
959
+ kwargs: HttpxSendArgs = {}
960
+ if self.custom_auth is not None:
961
+ kwargs["auth"] = self.custom_auth
962
+
963
+ if options.follow_redirects is not None:
964
+ kwargs["follow_redirects"] = options.follow_redirects
965
+
966
+ log.debug("Sending HTTP Request: %s %s", request.method, request.url)
967
+
968
+ response = None
969
+ try:
970
+ response = self._client.send(
971
+ request,
972
+ stream=stream or self._should_stream_response_body(request=request),
973
+ **kwargs,
974
+ )
975
+ except httpx.TimeoutException as err:
976
+ log.debug("Encountered httpx.TimeoutException", exc_info=True)
977
+
978
+ if remaining_retries > 0:
979
+ self._sleep_for_retry(
980
+ retries_taken=retries_taken,
981
+ max_retries=max_retries,
982
+ options=input_options,
983
+ response=None,
984
+ )
985
+ continue
986
+
987
+ log.debug("Raising timeout error")
988
+ raise APITimeoutError(request=request) from err
989
+ except Exception as err:
990
+ log.debug("Encountered Exception", exc_info=True)
991
+
992
+ if remaining_retries > 0:
993
+ self._sleep_for_retry(
994
+ retries_taken=retries_taken,
995
+ max_retries=max_retries,
996
+ options=input_options,
997
+ response=None,
998
+ )
999
+ continue
1000
+
1001
+ log.debug("Raising connection error")
1002
+ raise APIConnectionError(request=request) from err
1003
+
1004
+ log.debug(
1005
+ 'HTTP Response: %s %s "%i %s" %s',
1006
+ request.method,
1007
+ request.url,
1008
+ response.status_code,
1009
+ response.reason_phrase,
1010
+ response.headers,
1011
+ )
1012
+
1013
+ try:
1014
+ response.raise_for_status()
1015
+ except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
1016
+ log.debug("Encountered httpx.HTTPStatusError", exc_info=True)
1017
+
1018
+ if remaining_retries > 0 and self._should_retry(err.response):
1019
+ err.response.close()
1020
+ self._sleep_for_retry(
1021
+ retries_taken=retries_taken,
1022
+ max_retries=max_retries,
1023
+ options=input_options,
1024
+ response=response,
1025
+ )
1026
+ continue
1027
+
1028
+ # If the response is streamed then we need to explicitly read the response
1029
+ # to completion before attempting to access the response text.
1030
+ if not err.response.is_closed:
1031
+ err.response.read()
1032
+
1033
+ log.debug("Re-raising status error")
1034
+ raise self._make_status_error_from_response(err.response) from None
1035
+
1036
+ break
1037
+
1038
+ assert response is not None, "could not resolve response (should never happen)"
1039
+ return self._process_response(
1040
+ cast_to=cast_to,
1041
+ options=options,
1042
+ response=response,
1043
+ stream=stream,
1044
+ stream_cls=stream_cls,
1045
+ retries_taken=retries_taken,
1046
+ )
1047
+
1048
+ def _sleep_for_retry(
1049
+ self, *, retries_taken: int, max_retries: int, options: FinalRequestOptions, response: httpx.Response | None
1050
+ ) -> None:
1051
+ remaining_retries = max_retries - retries_taken
1052
+ if remaining_retries == 1:
1053
+ log.debug("1 retry left")
1054
+ else:
1055
+ log.debug("%i retries left", remaining_retries)
1056
+
1057
+ timeout = self._calculate_retry_timeout(remaining_retries, options, response.headers if response else None)
1058
+ log.info("Retrying request to %s in %f seconds", options.url, timeout)
1059
+
1060
+ time.sleep(timeout)
1061
+
1062
+ def _process_response(
1063
+ self,
1064
+ *,
1065
+ cast_to: Type[ResponseT],
1066
+ options: FinalRequestOptions,
1067
+ response: httpx.Response,
1068
+ stream: bool,
1069
+ stream_cls: type[Stream[Any]] | type[AsyncStream[Any]] | None,
1070
+ retries_taken: int = 0,
1071
+ ) -> ResponseT:
1072
+ origin = get_origin(cast_to) or cast_to
1073
+
1074
+ if inspect.isclass(origin) and issubclass(origin, BaseAPIResponse):
1075
+ if not issubclass(origin, APIResponse):
1076
+ raise TypeError(f"API Response types must subclass {APIResponse}; Received {origin}")
1077
+
1078
+ response_cls = cast("type[BaseAPIResponse[Any]]", cast_to)
1079
+ return cast(
1080
+ ResponseT,
1081
+ response_cls(
1082
+ raw=response,
1083
+ client=self,
1084
+ cast_to=extract_response_type(response_cls),
1085
+ stream=stream,
1086
+ stream_cls=stream_cls,
1087
+ options=options,
1088
+ retries_taken=retries_taken,
1089
+ ),
1090
+ )
1091
+
1092
+ if cast_to == httpx.Response:
1093
+ return cast(ResponseT, response)
1094
+
1095
+ api_response = APIResponse(
1096
+ raw=response,
1097
+ client=self,
1098
+ cast_to=cast("type[ResponseT]", cast_to), # pyright: ignore[reportUnnecessaryCast]
1099
+ stream=stream,
1100
+ stream_cls=stream_cls,
1101
+ options=options,
1102
+ retries_taken=retries_taken,
1103
+ )
1104
+ if bool(response.request.headers.get(RAW_RESPONSE_HEADER)):
1105
+ return cast(ResponseT, api_response)
1106
+
1107
+ return api_response.parse()
1108
+
1109
+ def _request_api_list(
1110
+ self,
1111
+ model: Type[object],
1112
+ page: Type[SyncPageT],
1113
+ options: FinalRequestOptions,
1114
+ ) -> SyncPageT:
1115
+ def _parser(resp: SyncPageT) -> SyncPageT:
1116
+ resp._set_private_attributes(
1117
+ client=self,
1118
+ model=model,
1119
+ options=options,
1120
+ )
1121
+ return resp
1122
+
1123
+ options.post_parser = _parser
1124
+
1125
+ return self.request(page, options, stream=False)
1126
+
1127
+ @overload
1128
+ def get(
1129
+ self,
1130
+ path: str,
1131
+ *,
1132
+ cast_to: Type[ResponseT],
1133
+ options: RequestOptions = {},
1134
+ stream: Literal[False] = False,
1135
+ ) -> ResponseT: ...
1136
+
1137
+ @overload
1138
+ def get(
1139
+ self,
1140
+ path: str,
1141
+ *,
1142
+ cast_to: Type[ResponseT],
1143
+ options: RequestOptions = {},
1144
+ stream: Literal[True],
1145
+ stream_cls: type[_StreamT],
1146
+ ) -> _StreamT: ...
1147
+
1148
+ @overload
1149
+ def get(
1150
+ self,
1151
+ path: str,
1152
+ *,
1153
+ cast_to: Type[ResponseT],
1154
+ options: RequestOptions = {},
1155
+ stream: bool,
1156
+ stream_cls: type[_StreamT] | None = None,
1157
+ ) -> ResponseT | _StreamT: ...
1158
+
1159
+ def get(
1160
+ self,
1161
+ path: str,
1162
+ *,
1163
+ cast_to: Type[ResponseT],
1164
+ options: RequestOptions = {},
1165
+ stream: bool = False,
1166
+ stream_cls: type[_StreamT] | None = None,
1167
+ ) -> ResponseT | _StreamT:
1168
+ opts = FinalRequestOptions.construct(method="get", url=path, **options)
1169
+ # cast is required because mypy complains about returning Any even though
1170
+ # it understands the type variables
1171
+ return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
1172
+
1173
+ @overload
1174
+ def post(
1175
+ self,
1176
+ path: str,
1177
+ *,
1178
+ cast_to: Type[ResponseT],
1179
+ body: Body | None = None,
1180
+ options: RequestOptions = {},
1181
+ files: RequestFiles | None = None,
1182
+ stream: Literal[False] = False,
1183
+ ) -> ResponseT: ...
1184
+
1185
+ @overload
1186
+ def post(
1187
+ self,
1188
+ path: str,
1189
+ *,
1190
+ cast_to: Type[ResponseT],
1191
+ body: Body | None = None,
1192
+ options: RequestOptions = {},
1193
+ files: RequestFiles | None = None,
1194
+ stream: Literal[True],
1195
+ stream_cls: type[_StreamT],
1196
+ ) -> _StreamT: ...
1197
+
1198
+ @overload
1199
+ def post(
1200
+ self,
1201
+ path: str,
1202
+ *,
1203
+ cast_to: Type[ResponseT],
1204
+ body: Body | None = None,
1205
+ options: RequestOptions = {},
1206
+ files: RequestFiles | None = None,
1207
+ stream: bool,
1208
+ stream_cls: type[_StreamT] | None = None,
1209
+ ) -> ResponseT | _StreamT: ...
1210
+
1211
+ def post(
1212
+ self,
1213
+ path: str,
1214
+ *,
1215
+ cast_to: Type[ResponseT],
1216
+ body: Body | None = None,
1217
+ options: RequestOptions = {},
1218
+ files: RequestFiles | None = None,
1219
+ stream: bool = False,
1220
+ stream_cls: type[_StreamT] | None = None,
1221
+ ) -> ResponseT | _StreamT:
1222
+ opts = FinalRequestOptions.construct(
1223
+ method="post", url=path, json_data=body, files=to_httpx_files(files), **options
1224
+ )
1225
+ return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
1226
+
1227
+ def patch(
1228
+ self,
1229
+ path: str,
1230
+ *,
1231
+ cast_to: Type[ResponseT],
1232
+ body: Body | None = None,
1233
+ options: RequestOptions = {},
1234
+ ) -> ResponseT:
1235
+ opts = FinalRequestOptions.construct(method="patch", url=path, json_data=body, **options)
1236
+ return self.request(cast_to, opts)
1237
+
1238
+ def put(
1239
+ self,
1240
+ path: str,
1241
+ *,
1242
+ cast_to: Type[ResponseT],
1243
+ body: Body | None = None,
1244
+ files: RequestFiles | None = None,
1245
+ options: RequestOptions = {},
1246
+ ) -> ResponseT:
1247
+ opts = FinalRequestOptions.construct(
1248
+ method="put", url=path, json_data=body, files=to_httpx_files(files), **options
1249
+ )
1250
+ return self.request(cast_to, opts)
1251
+
1252
+ def delete(
1253
+ self,
1254
+ path: str,
1255
+ *,
1256
+ cast_to: Type[ResponseT],
1257
+ body: Body | None = None,
1258
+ options: RequestOptions = {},
1259
+ ) -> ResponseT:
1260
+ opts = FinalRequestOptions.construct(method="delete", url=path, json_data=body, **options)
1261
+ return self.request(cast_to, opts)
1262
+
1263
+ def get_api_list(
1264
+ self,
1265
+ path: str,
1266
+ *,
1267
+ model: Type[object],
1268
+ page: Type[SyncPageT],
1269
+ body: Body | None = None,
1270
+ options: RequestOptions = {},
1271
+ method: str = "get",
1272
+ ) -> SyncPageT:
1273
+ opts = FinalRequestOptions.construct(method=method, url=path, json_data=body, **options)
1274
+ return self._request_api_list(model, page, opts)
1275
+
1276
+
1277
+ class _DefaultAsyncHttpxClient(httpx.AsyncClient):
1278
+ def __init__(self, **kwargs: Any) -> None:
1279
+ kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
1280
+ kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS)
1281
+ kwargs.setdefault("follow_redirects", True)
1282
+ super().__init__(**kwargs)
1283
+
1284
+
1285
+ if TYPE_CHECKING:
1286
+ DefaultAsyncHttpxClient = httpx.AsyncClient
1287
+ """An alias to `httpx.AsyncClient` that provides the same defaults that this SDK
1288
+ uses internally.
1289
+
1290
+ This is useful because overriding the `http_client` with your own instance of
1291
+ `httpx.AsyncClient` will result in httpx's defaults being used, not ours.
1292
+ """
1293
+ else:
1294
+ DefaultAsyncHttpxClient = _DefaultAsyncHttpxClient
1295
+
1296
+
1297
+ class AsyncHttpxClientWrapper(DefaultAsyncHttpxClient):
1298
+ def __del__(self) -> None:
1299
+ if self.is_closed:
1300
+ return
1301
+
1302
+ try:
1303
+ # TODO(someday): support non asyncio runtimes here
1304
+ asyncio.get_running_loop().create_task(self.aclose())
1305
+ except Exception:
1306
+ pass
1307
+
1308
+
1309
+ class AsyncAPIClient(BaseClient[httpx.AsyncClient, AsyncStream[Any]]):
1310
+ _client: httpx.AsyncClient
1311
+ _default_stream_cls: type[AsyncStream[Any]] | None = None
1312
+
1313
+ def __init__(
1314
+ self,
1315
+ *,
1316
+ version: str,
1317
+ base_url: str | URL,
1318
+ _strict_response_validation: bool,
1319
+ max_retries: int = DEFAULT_MAX_RETRIES,
1320
+ timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
1321
+ http_client: httpx.AsyncClient | None = None,
1322
+ custom_headers: Mapping[str, str] | None = None,
1323
+ custom_query: Mapping[str, object] | None = None,
1324
+ ) -> None:
1325
+ if not is_given(timeout):
1326
+ # if the user passed in a custom http client with a non-default
1327
+ # timeout set then we use that timeout.
1328
+ #
1329
+ # note: there is an edge case here where the user passes in a client
1330
+ # where they've explicitly set the timeout to match the default timeout
1331
+ # as this check is structural, meaning that we'll think they didn't
1332
+ # pass in a timeout and will ignore it
1333
+ if http_client and http_client.timeout != HTTPX_DEFAULT_TIMEOUT:
1334
+ timeout = http_client.timeout
1335
+ else:
1336
+ timeout = DEFAULT_TIMEOUT
1337
+
1338
+ if http_client is not None and not isinstance(http_client, httpx.AsyncClient): # pyright: ignore[reportUnnecessaryIsInstance]
1339
+ raise TypeError(
1340
+ f"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` but got {type(http_client)}"
1341
+ )
1342
+
1343
+ super().__init__(
1344
+ version=version,
1345
+ base_url=base_url,
1346
+ # cast to a valid type because mypy doesn't understand our type narrowing
1347
+ timeout=cast(Timeout, timeout),
1348
+ max_retries=max_retries,
1349
+ custom_query=custom_query,
1350
+ custom_headers=custom_headers,
1351
+ _strict_response_validation=_strict_response_validation,
1352
+ )
1353
+ self._client = http_client or AsyncHttpxClientWrapper(
1354
+ base_url=base_url,
1355
+ # cast to a valid type because mypy doesn't understand our type narrowing
1356
+ timeout=cast(Timeout, timeout),
1357
+ )
1358
+
1359
+ def is_closed(self) -> bool:
1360
+ return self._client.is_closed
1361
+
1362
+ async def close(self) -> None:
1363
+ """Close the underlying HTTPX client.
1364
+
1365
+ The client will *not* be usable after this.
1366
+ """
1367
+ await self._client.aclose()
1368
+
1369
+ async def __aenter__(self: _T) -> _T:
1370
+ return self
1371
+
1372
+ async def __aexit__(
1373
+ self,
1374
+ exc_type: type[BaseException] | None,
1375
+ exc: BaseException | None,
1376
+ exc_tb: TracebackType | None,
1377
+ ) -> None:
1378
+ await self.close()
1379
+
1380
+ async def _prepare_options(
1381
+ self,
1382
+ options: FinalRequestOptions, # noqa: ARG002
1383
+ ) -> FinalRequestOptions:
1384
+ """Hook for mutating the given options"""
1385
+ return options
1386
+
1387
+ async def _prepare_request(
1388
+ self,
1389
+ request: httpx.Request, # noqa: ARG002
1390
+ ) -> None:
1391
+ """This method is used as a callback for mutating the `Request` object
1392
+ after it has been constructed.
1393
+ This is useful for cases where you want to add certain headers based off of
1394
+ the request properties, e.g. `url`, `method` etc.
1395
+ """
1396
+ return None
1397
+
1398
+ @overload
1399
+ async def request(
1400
+ self,
1401
+ cast_to: Type[ResponseT],
1402
+ options: FinalRequestOptions,
1403
+ *,
1404
+ stream: Literal[False] = False,
1405
+ ) -> ResponseT: ...
1406
+
1407
+ @overload
1408
+ async def request(
1409
+ self,
1410
+ cast_to: Type[ResponseT],
1411
+ options: FinalRequestOptions,
1412
+ *,
1413
+ stream: Literal[True],
1414
+ stream_cls: type[_AsyncStreamT],
1415
+ ) -> _AsyncStreamT: ...
1416
+
1417
+ @overload
1418
+ async def request(
1419
+ self,
1420
+ cast_to: Type[ResponseT],
1421
+ options: FinalRequestOptions,
1422
+ *,
1423
+ stream: bool,
1424
+ stream_cls: type[_AsyncStreamT] | None = None,
1425
+ ) -> ResponseT | _AsyncStreamT: ...
1426
+
1427
+ async def request(
1428
+ self,
1429
+ cast_to: Type[ResponseT],
1430
+ options: FinalRequestOptions,
1431
+ *,
1432
+ stream: bool = False,
1433
+ stream_cls: type[_AsyncStreamT] | None = None,
1434
+ ) -> ResponseT | _AsyncStreamT:
1435
+ if self._platform is None:
1436
+ # `get_platform` can make blocking IO calls so we
1437
+ # execute it earlier while we are in an async context
1438
+ self._platform = await asyncify(get_platform)()
1439
+
1440
+ cast_to = self._maybe_override_cast_to(cast_to, options)
1441
+
1442
+ # create a copy of the options we were given so that if the
1443
+ # options are mutated later & we then retry, the retries are
1444
+ # given the original options
1445
+ input_options = model_copy(options)
1446
+ if input_options.idempotency_key is None and input_options.method.lower() != "get":
1447
+ # ensure the idempotency key is reused between requests
1448
+ input_options.idempotency_key = self._idempotency_key()
1449
+
1450
+ response: httpx.Response | None = None
1451
+ max_retries = input_options.get_max_retries(self.max_retries)
1452
+
1453
+ retries_taken = 0
1454
+ for retries_taken in range(max_retries + 1):
1455
+ options = model_copy(input_options)
1456
+ options = await self._prepare_options(options)
1457
+
1458
+ remaining_retries = max_retries - retries_taken
1459
+ request = self._build_request(options, retries_taken=retries_taken)
1460
+ await self._prepare_request(request)
1461
+
1462
+ kwargs: HttpxSendArgs = {}
1463
+ if self.custom_auth is not None:
1464
+ kwargs["auth"] = self.custom_auth
1465
+
1466
+ if options.follow_redirects is not None:
1467
+ kwargs["follow_redirects"] = options.follow_redirects
1468
+
1469
+ log.debug("Sending HTTP Request: %s %s", request.method, request.url)
1470
+
1471
+ response = None
1472
+ try:
1473
+ response = await self._client.send(
1474
+ request,
1475
+ stream=stream or self._should_stream_response_body(request=request),
1476
+ **kwargs,
1477
+ )
1478
+ except httpx.TimeoutException as err:
1479
+ log.debug("Encountered httpx.TimeoutException", exc_info=True)
1480
+
1481
+ if remaining_retries > 0:
1482
+ await self._sleep_for_retry(
1483
+ retries_taken=retries_taken,
1484
+ max_retries=max_retries,
1485
+ options=input_options,
1486
+ response=None,
1487
+ )
1488
+ continue
1489
+
1490
+ log.debug("Raising timeout error")
1491
+ raise APITimeoutError(request=request) from err
1492
+ except Exception as err:
1493
+ log.debug("Encountered Exception", exc_info=True)
1494
+
1495
+ if remaining_retries > 0:
1496
+ await self._sleep_for_retry(
1497
+ retries_taken=retries_taken,
1498
+ max_retries=max_retries,
1499
+ options=input_options,
1500
+ response=None,
1501
+ )
1502
+ continue
1503
+
1504
+ log.debug("Raising connection error")
1505
+ raise APIConnectionError(request=request) from err
1506
+
1507
+ log.debug(
1508
+ 'HTTP Response: %s %s "%i %s" %s',
1509
+ request.method,
1510
+ request.url,
1511
+ response.status_code,
1512
+ response.reason_phrase,
1513
+ response.headers,
1514
+ )
1515
+
1516
+ try:
1517
+ response.raise_for_status()
1518
+ except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
1519
+ log.debug("Encountered httpx.HTTPStatusError", exc_info=True)
1520
+
1521
+ if remaining_retries > 0 and self._should_retry(err.response):
1522
+ await err.response.aclose()
1523
+ await self._sleep_for_retry(
1524
+ retries_taken=retries_taken,
1525
+ max_retries=max_retries,
1526
+ options=input_options,
1527
+ response=response,
1528
+ )
1529
+ continue
1530
+
1531
+ # If the response is streamed then we need to explicitly read the response
1532
+ # to completion before attempting to access the response text.
1533
+ if not err.response.is_closed:
1534
+ await err.response.aread()
1535
+
1536
+ log.debug("Re-raising status error")
1537
+ raise self._make_status_error_from_response(err.response) from None
1538
+
1539
+ break
1540
+
1541
+ assert response is not None, "could not resolve response (should never happen)"
1542
+ return await self._process_response(
1543
+ cast_to=cast_to,
1544
+ options=options,
1545
+ response=response,
1546
+ stream=stream,
1547
+ stream_cls=stream_cls,
1548
+ retries_taken=retries_taken,
1549
+ )
1550
+
1551
+ async def _sleep_for_retry(
1552
+ self, *, retries_taken: int, max_retries: int, options: FinalRequestOptions, response: httpx.Response | None
1553
+ ) -> None:
1554
+ remaining_retries = max_retries - retries_taken
1555
+ if remaining_retries == 1:
1556
+ log.debug("1 retry left")
1557
+ else:
1558
+ log.debug("%i retries left", remaining_retries)
1559
+
1560
+ timeout = self._calculate_retry_timeout(remaining_retries, options, response.headers if response else None)
1561
+ log.info("Retrying request to %s in %f seconds", options.url, timeout)
1562
+
1563
+ await anyio.sleep(timeout)
1564
+
1565
+ async def _process_response(
1566
+ self,
1567
+ *,
1568
+ cast_to: Type[ResponseT],
1569
+ options: FinalRequestOptions,
1570
+ response: httpx.Response,
1571
+ stream: bool,
1572
+ stream_cls: type[Stream[Any]] | type[AsyncStream[Any]] | None,
1573
+ retries_taken: int = 0,
1574
+ ) -> ResponseT:
1575
+ origin = get_origin(cast_to) or cast_to
1576
+
1577
+ if inspect.isclass(origin) and issubclass(origin, BaseAPIResponse):
1578
+ if not issubclass(origin, AsyncAPIResponse):
1579
+ raise TypeError(f"API Response types must subclass {AsyncAPIResponse}; Received {origin}")
1580
+
1581
+ response_cls = cast("type[BaseAPIResponse[Any]]", cast_to)
1582
+ return cast(
1583
+ "ResponseT",
1584
+ response_cls(
1585
+ raw=response,
1586
+ client=self,
1587
+ cast_to=extract_response_type(response_cls),
1588
+ stream=stream,
1589
+ stream_cls=stream_cls,
1590
+ options=options,
1591
+ retries_taken=retries_taken,
1592
+ ),
1593
+ )
1594
+
1595
+ if cast_to == httpx.Response:
1596
+ return cast(ResponseT, response)
1597
+
1598
+ api_response = AsyncAPIResponse(
1599
+ raw=response,
1600
+ client=self,
1601
+ cast_to=cast("type[ResponseT]", cast_to), # pyright: ignore[reportUnnecessaryCast]
1602
+ stream=stream,
1603
+ stream_cls=stream_cls,
1604
+ options=options,
1605
+ retries_taken=retries_taken,
1606
+ )
1607
+ if bool(response.request.headers.get(RAW_RESPONSE_HEADER)):
1608
+ return cast(ResponseT, api_response)
1609
+
1610
+ return await api_response.parse()
1611
+
1612
+ def _request_api_list(
1613
+ self,
1614
+ model: Type[_T],
1615
+ page: Type[AsyncPageT],
1616
+ options: FinalRequestOptions,
1617
+ ) -> AsyncPaginator[_T, AsyncPageT]:
1618
+ return AsyncPaginator(client=self, options=options, page_cls=page, model=model)
1619
+
1620
+ @overload
1621
+ async def get(
1622
+ self,
1623
+ path: str,
1624
+ *,
1625
+ cast_to: Type[ResponseT],
1626
+ options: RequestOptions = {},
1627
+ stream: Literal[False] = False,
1628
+ ) -> ResponseT: ...
1629
+
1630
+ @overload
1631
+ async def get(
1632
+ self,
1633
+ path: str,
1634
+ *,
1635
+ cast_to: Type[ResponseT],
1636
+ options: RequestOptions = {},
1637
+ stream: Literal[True],
1638
+ stream_cls: type[_AsyncStreamT],
1639
+ ) -> _AsyncStreamT: ...
1640
+
1641
+ @overload
1642
+ async def get(
1643
+ self,
1644
+ path: str,
1645
+ *,
1646
+ cast_to: Type[ResponseT],
1647
+ options: RequestOptions = {},
1648
+ stream: bool,
1649
+ stream_cls: type[_AsyncStreamT] | None = None,
1650
+ ) -> ResponseT | _AsyncStreamT: ...
1651
+
1652
+ async def get(
1653
+ self,
1654
+ path: str,
1655
+ *,
1656
+ cast_to: Type[ResponseT],
1657
+ options: RequestOptions = {},
1658
+ stream: bool = False,
1659
+ stream_cls: type[_AsyncStreamT] | None = None,
1660
+ ) -> ResponseT | _AsyncStreamT:
1661
+ opts = FinalRequestOptions.construct(method="get", url=path, **options)
1662
+ return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
1663
+
1664
+ @overload
1665
+ async def post(
1666
+ self,
1667
+ path: str,
1668
+ *,
1669
+ cast_to: Type[ResponseT],
1670
+ body: Body | None = None,
1671
+ files: RequestFiles | None = None,
1672
+ options: RequestOptions = {},
1673
+ stream: Literal[False] = False,
1674
+ ) -> ResponseT: ...
1675
+
1676
+ @overload
1677
+ async def post(
1678
+ self,
1679
+ path: str,
1680
+ *,
1681
+ cast_to: Type[ResponseT],
1682
+ body: Body | None = None,
1683
+ files: RequestFiles | None = None,
1684
+ options: RequestOptions = {},
1685
+ stream: Literal[True],
1686
+ stream_cls: type[_AsyncStreamT],
1687
+ ) -> _AsyncStreamT: ...
1688
+
1689
+ @overload
1690
+ async def post(
1691
+ self,
1692
+ path: str,
1693
+ *,
1694
+ cast_to: Type[ResponseT],
1695
+ body: Body | None = None,
1696
+ files: RequestFiles | None = None,
1697
+ options: RequestOptions = {},
1698
+ stream: bool,
1699
+ stream_cls: type[_AsyncStreamT] | None = None,
1700
+ ) -> ResponseT | _AsyncStreamT: ...
1701
+
1702
+ async def post(
1703
+ self,
1704
+ path: str,
1705
+ *,
1706
+ cast_to: Type[ResponseT],
1707
+ body: Body | None = None,
1708
+ files: RequestFiles | None = None,
1709
+ options: RequestOptions = {},
1710
+ stream: bool = False,
1711
+ stream_cls: type[_AsyncStreamT] | None = None,
1712
+ ) -> ResponseT | _AsyncStreamT:
1713
+ opts = FinalRequestOptions.construct(
1714
+ method="post", url=path, json_data=body, files=await async_to_httpx_files(files), **options
1715
+ )
1716
+ return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
1717
+
1718
+ async def patch(
1719
+ self,
1720
+ path: str,
1721
+ *,
1722
+ cast_to: Type[ResponseT],
1723
+ body: Body | None = None,
1724
+ options: RequestOptions = {},
1725
+ ) -> ResponseT:
1726
+ opts = FinalRequestOptions.construct(method="patch", url=path, json_data=body, **options)
1727
+ return await self.request(cast_to, opts)
1728
+
1729
+ async def put(
1730
+ self,
1731
+ path: str,
1732
+ *,
1733
+ cast_to: Type[ResponseT],
1734
+ body: Body | None = None,
1735
+ files: RequestFiles | None = None,
1736
+ options: RequestOptions = {},
1737
+ ) -> ResponseT:
1738
+ opts = FinalRequestOptions.construct(
1739
+ method="put", url=path, json_data=body, files=await async_to_httpx_files(files), **options
1740
+ )
1741
+ return await self.request(cast_to, opts)
1742
+
1743
+ async def delete(
1744
+ self,
1745
+ path: str,
1746
+ *,
1747
+ cast_to: Type[ResponseT],
1748
+ body: Body | None = None,
1749
+ options: RequestOptions = {},
1750
+ ) -> ResponseT:
1751
+ opts = FinalRequestOptions.construct(method="delete", url=path, json_data=body, **options)
1752
+ return await self.request(cast_to, opts)
1753
+
1754
+ def get_api_list(
1755
+ self,
1756
+ path: str,
1757
+ *,
1758
+ model: Type[_T],
1759
+ page: Type[AsyncPageT],
1760
+ body: Body | None = None,
1761
+ options: RequestOptions = {},
1762
+ method: str = "get",
1763
+ ) -> AsyncPaginator[_T, AsyncPageT]:
1764
+ opts = FinalRequestOptions.construct(method=method, url=path, json_data=body, **options)
1765
+ return self._request_api_list(model, page, opts)
1766
+
1767
+
1768
+ def make_request_options(
1769
+ *,
1770
+ query: Query | None = None,
1771
+ extra_headers: Headers | None = None,
1772
+ extra_query: Query | None = None,
1773
+ extra_body: Body | None = None,
1774
+ idempotency_key: str | None = None,
1775
+ timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1776
+ post_parser: PostParser | NotGiven = NOT_GIVEN,
1777
+ ) -> RequestOptions:
1778
+ """Create a dict of type RequestOptions without keys of NotGiven values."""
1779
+ options: RequestOptions = {}
1780
+ if extra_headers is not None:
1781
+ options["headers"] = extra_headers
1782
+
1783
+ if extra_body is not None:
1784
+ options["extra_json"] = cast(AnyMapping, extra_body)
1785
+
1786
+ if query is not None:
1787
+ options["params"] = query
1788
+
1789
+ if extra_query is not None:
1790
+ options["params"] = {**options.get("params", {}), **extra_query}
1791
+
1792
+ if not isinstance(timeout, NotGiven):
1793
+ options["timeout"] = timeout
1794
+
1795
+ if idempotency_key is not None:
1796
+ options["idempotency_key"] = idempotency_key
1797
+
1798
+ if is_given(post_parser):
1799
+ # internal
1800
+ options["post_parser"] = post_parser # type: ignore
1801
+
1802
+ return options
1803
+
1804
+
1805
+ class ForceMultipartDict(Dict[str, None]):
1806
+ def __bool__(self) -> bool:
1807
+ return True
1808
+
1809
+
1810
+ class OtherPlatform:
1811
+ def __init__(self, name: str) -> None:
1812
+ self.name = name
1813
+
1814
+ @override
1815
+ def __str__(self) -> str:
1816
+ return f"Other:{self.name}"
1817
+
1818
+
1819
+ Platform = Union[
1820
+ OtherPlatform,
1821
+ Literal[
1822
+ "MacOS",
1823
+ "Linux",
1824
+ "Windows",
1825
+ "FreeBSD",
1826
+ "OpenBSD",
1827
+ "iOS",
1828
+ "Android",
1829
+ "Unknown",
1830
+ ],
1831
+ ]
1832
+
1833
+
1834
+ def get_platform() -> Platform:
1835
+ try:
1836
+ system = platform.system().lower()
1837
+ platform_name = platform.platform().lower()
1838
+ except Exception:
1839
+ return "Unknown"
1840
+
1841
+ if "iphone" in platform_name or "ipad" in platform_name:
1842
+ # Tested using Python3IDE on an iPhone 11 and Pythonista on an iPad 7
1843
+ # system is Darwin and platform_name is a string like:
1844
+ # - Darwin-21.6.0-iPhone12,1-64bit
1845
+ # - Darwin-21.6.0-iPad7,11-64bit
1846
+ return "iOS"
1847
+
1848
+ if system == "darwin":
1849
+ return "MacOS"
1850
+
1851
+ if system == "windows":
1852
+ return "Windows"
1853
+
1854
+ if "android" in platform_name:
1855
+ # Tested using Pydroid 3
1856
+ # system is Linux and platform_name is a string like 'Linux-5.10.81-android12-9-00001-geba40aecb3b7-ab8534902-aarch64-with-libc'
1857
+ return "Android"
1858
+
1859
+ if system == "linux":
1860
+ # https://distro.readthedocs.io/en/latest/#distro.id
1861
+ distro_id = distro.id()
1862
+ if distro_id == "freebsd":
1863
+ return "FreeBSD"
1864
+
1865
+ if distro_id == "openbsd":
1866
+ return "OpenBSD"
1867
+
1868
+ return "Linux"
1869
+
1870
+ if platform_name:
1871
+ return OtherPlatform(platform_name)
1872
+
1873
+ return "Unknown"
1874
+
1875
+
1876
+ @lru_cache(maxsize=None)
1877
+ def platform_headers(version: str, *, platform: Platform | None) -> Dict[str, str]:
1878
+ return {
1879
+ "X-Stainless-Lang": "python",
1880
+ "X-Stainless-Package-Version": version,
1881
+ "X-Stainless-OS": str(platform or get_platform()),
1882
+ "X-Stainless-Arch": str(get_architecture()),
1883
+ "X-Stainless-Runtime": get_python_runtime(),
1884
+ "X-Stainless-Runtime-Version": get_python_version(),
1885
+ }
1886
+
1887
+
1888
+ class OtherArch:
1889
+ def __init__(self, name: str) -> None:
1890
+ self.name = name
1891
+
1892
+ @override
1893
+ def __str__(self) -> str:
1894
+ return f"other:{self.name}"
1895
+
1896
+
1897
+ Arch = Union[OtherArch, Literal["x32", "x64", "arm", "arm64", "unknown"]]
1898
+
1899
+
1900
+ def get_python_runtime() -> str:
1901
+ try:
1902
+ return platform.python_implementation()
1903
+ except Exception:
1904
+ return "unknown"
1905
+
1906
+
1907
+ def get_python_version() -> str:
1908
+ try:
1909
+ return platform.python_version()
1910
+ except Exception:
1911
+ return "unknown"
1912
+
1913
+
1914
+ def get_architecture() -> Arch:
1915
+ try:
1916
+ machine = platform.machine().lower()
1917
+ except Exception:
1918
+ return "unknown"
1919
+
1920
+ if machine in ("arm64", "aarch64"):
1921
+ return "arm64"
1922
+
1923
+ # TODO: untested
1924
+ if machine == "arm":
1925
+ return "arm"
1926
+
1927
+ if machine == "x86_64":
1928
+ return "x64"
1929
+
1930
+ # TODO: untested
1931
+ if sys.maxsize <= 2**32:
1932
+ return "x32"
1933
+
1934
+ if machine:
1935
+ return OtherArch(machine)
1936
+
1937
+ return "unknown"
1938
+
1939
+
1940
+ def _merge_mappings(
1941
+ obj1: Mapping[_T_co, Union[_T, Omit]],
1942
+ obj2: Mapping[_T_co, Union[_T, Omit]],
1943
+ ) -> Dict[_T_co, _T]:
1944
+ """Merge two mappings of the same type, removing any values that are instances of `Omit`.
1945
+
1946
+ In cases with duplicate keys the second mapping takes precedence.
1947
+ """
1948
+ merged = {**obj1, **obj2}
1949
+ return {key: value for key, value in merged.items() if not isinstance(value, Omit)}