files.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import Mapping, cast
  4. from typing_extensions import Literal
  5. import httpx
  6. from .... import _legacy_response
  7. from .content import (
  8. Content,
  9. AsyncContent,
  10. ContentWithRawResponse,
  11. AsyncContentWithRawResponse,
  12. ContentWithStreamingResponse,
  13. AsyncContentWithStreamingResponse,
  14. )
  15. from ...._types import Body, Omit, Query, Headers, NoneType, NotGiven, FileTypes, omit, not_given
  16. from ...._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform
  17. from ...._compat import cached_property
  18. from ...._resource import SyncAPIResource, AsyncAPIResource
  19. from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  20. from ....pagination import SyncCursorPage, AsyncCursorPage
  21. from ...._base_client import AsyncPaginator, make_request_options
  22. from ....types.containers import file_list_params, file_create_params
  23. from ....types.containers.file_list_response import FileListResponse
  24. from ....types.containers.file_create_response import FileCreateResponse
  25. from ....types.containers.file_retrieve_response import FileRetrieveResponse
  26. __all__ = ["Files", "AsyncFiles"]
  27. class Files(SyncAPIResource):
  28. @cached_property
  29. def content(self) -> Content:
  30. return Content(self._client)
  31. @cached_property
  32. def with_raw_response(self) -> FilesWithRawResponse:
  33. """
  34. This property can be used as a prefix for any HTTP method call to return
  35. the raw response object instead of the parsed content.
  36. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  37. """
  38. return FilesWithRawResponse(self)
  39. @cached_property
  40. def with_streaming_response(self) -> FilesWithStreamingResponse:
  41. """
  42. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  43. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  44. """
  45. return FilesWithStreamingResponse(self)
  46. def create(
  47. self,
  48. container_id: str,
  49. *,
  50. file: FileTypes | Omit = omit,
  51. file_id: str | Omit = omit,
  52. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  53. # The extra values given here take precedence over values defined on the client or passed to this method.
  54. extra_headers: Headers | None = None,
  55. extra_query: Query | None = None,
  56. extra_body: Body | None = None,
  57. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  58. ) -> FileCreateResponse:
  59. """
  60. Create a Container File
  61. You can send either a multipart/form-data request with the raw file content, or
  62. a JSON request with a file ID.
  63. Args:
  64. file: The File object (not file name) to be uploaded.
  65. file_id: Name of the file to create.
  66. extra_headers: Send extra headers
  67. extra_query: Add additional query parameters to the request
  68. extra_body: Add additional JSON properties to the request
  69. timeout: Override the client-level default timeout for this request, in seconds
  70. """
  71. if not container_id:
  72. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  73. body = deepcopy_minimal(
  74. {
  75. "file": file,
  76. "file_id": file_id,
  77. }
  78. )
  79. files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
  80. # It should be noted that the actual Content-Type header that will be
  81. # sent to the server will contain a `boundary` parameter, e.g.
  82. # multipart/form-data; boundary=---abc--
  83. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  84. return self._post(
  85. f"/containers/{container_id}/files",
  86. body=maybe_transform(body, file_create_params.FileCreateParams),
  87. files=files,
  88. options=make_request_options(
  89. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  90. ),
  91. cast_to=FileCreateResponse,
  92. )
  93. def retrieve(
  94. self,
  95. file_id: str,
  96. *,
  97. container_id: str,
  98. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  99. # The extra values given here take precedence over values defined on the client or passed to this method.
  100. extra_headers: Headers | None = None,
  101. extra_query: Query | None = None,
  102. extra_body: Body | None = None,
  103. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  104. ) -> FileRetrieveResponse:
  105. """
  106. Retrieve Container File
  107. Args:
  108. extra_headers: Send extra headers
  109. extra_query: Add additional query parameters to the request
  110. extra_body: Add additional JSON properties to the request
  111. timeout: Override the client-level default timeout for this request, in seconds
  112. """
  113. if not container_id:
  114. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  115. if not file_id:
  116. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  117. return self._get(
  118. f"/containers/{container_id}/files/{file_id}",
  119. options=make_request_options(
  120. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  121. ),
  122. cast_to=FileRetrieveResponse,
  123. )
  124. def list(
  125. self,
  126. container_id: str,
  127. *,
  128. after: str | Omit = omit,
  129. limit: int | Omit = omit,
  130. order: Literal["asc", "desc"] | Omit = omit,
  131. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  132. # The extra values given here take precedence over values defined on the client or passed to this method.
  133. extra_headers: Headers | None = None,
  134. extra_query: Query | None = None,
  135. extra_body: Body | None = None,
  136. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  137. ) -> SyncCursorPage[FileListResponse]:
  138. """List Container files
  139. Args:
  140. after: A cursor for use in pagination.
  141. `after` is an object ID that defines your place
  142. in the list. For instance, if you make a list request and receive 100 objects,
  143. ending with obj_foo, your subsequent call can include after=obj_foo in order to
  144. fetch the next page of the list.
  145. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  146. 100, and the default is 20.
  147. order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
  148. order and `desc` for descending order.
  149. extra_headers: Send extra headers
  150. extra_query: Add additional query parameters to the request
  151. extra_body: Add additional JSON properties to the request
  152. timeout: Override the client-level default timeout for this request, in seconds
  153. """
  154. if not container_id:
  155. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  156. return self._get_api_list(
  157. f"/containers/{container_id}/files",
  158. page=SyncCursorPage[FileListResponse],
  159. options=make_request_options(
  160. extra_headers=extra_headers,
  161. extra_query=extra_query,
  162. extra_body=extra_body,
  163. timeout=timeout,
  164. query=maybe_transform(
  165. {
  166. "after": after,
  167. "limit": limit,
  168. "order": order,
  169. },
  170. file_list_params.FileListParams,
  171. ),
  172. ),
  173. model=FileListResponse,
  174. )
  175. def delete(
  176. self,
  177. file_id: str,
  178. *,
  179. container_id: str,
  180. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  181. # The extra values given here take precedence over values defined on the client or passed to this method.
  182. extra_headers: Headers | None = None,
  183. extra_query: Query | None = None,
  184. extra_body: Body | None = None,
  185. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  186. ) -> None:
  187. """
  188. Delete Container File
  189. Args:
  190. extra_headers: Send extra headers
  191. extra_query: Add additional query parameters to the request
  192. extra_body: Add additional JSON properties to the request
  193. timeout: Override the client-level default timeout for this request, in seconds
  194. """
  195. if not container_id:
  196. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  197. if not file_id:
  198. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  199. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  200. return self._delete(
  201. f"/containers/{container_id}/files/{file_id}",
  202. options=make_request_options(
  203. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  204. ),
  205. cast_to=NoneType,
  206. )
  207. class AsyncFiles(AsyncAPIResource):
  208. @cached_property
  209. def content(self) -> AsyncContent:
  210. return AsyncContent(self._client)
  211. @cached_property
  212. def with_raw_response(self) -> AsyncFilesWithRawResponse:
  213. """
  214. This property can be used as a prefix for any HTTP method call to return
  215. the raw response object instead of the parsed content.
  216. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  217. """
  218. return AsyncFilesWithRawResponse(self)
  219. @cached_property
  220. def with_streaming_response(self) -> AsyncFilesWithStreamingResponse:
  221. """
  222. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  223. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  224. """
  225. return AsyncFilesWithStreamingResponse(self)
  226. async def create(
  227. self,
  228. container_id: str,
  229. *,
  230. file: FileTypes | Omit = omit,
  231. file_id: str | Omit = omit,
  232. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  233. # The extra values given here take precedence over values defined on the client or passed to this method.
  234. extra_headers: Headers | None = None,
  235. extra_query: Query | None = None,
  236. extra_body: Body | None = None,
  237. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  238. ) -> FileCreateResponse:
  239. """
  240. Create a Container File
  241. You can send either a multipart/form-data request with the raw file content, or
  242. a JSON request with a file ID.
  243. Args:
  244. file: The File object (not file name) to be uploaded.
  245. file_id: Name of the file to create.
  246. extra_headers: Send extra headers
  247. extra_query: Add additional query parameters to the request
  248. extra_body: Add additional JSON properties to the request
  249. timeout: Override the client-level default timeout for this request, in seconds
  250. """
  251. if not container_id:
  252. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  253. body = deepcopy_minimal(
  254. {
  255. "file": file,
  256. "file_id": file_id,
  257. }
  258. )
  259. files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
  260. # It should be noted that the actual Content-Type header that will be
  261. # sent to the server will contain a `boundary` parameter, e.g.
  262. # multipart/form-data; boundary=---abc--
  263. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  264. return await self._post(
  265. f"/containers/{container_id}/files",
  266. body=await async_maybe_transform(body, file_create_params.FileCreateParams),
  267. files=files,
  268. options=make_request_options(
  269. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  270. ),
  271. cast_to=FileCreateResponse,
  272. )
  273. async def retrieve(
  274. self,
  275. file_id: str,
  276. *,
  277. container_id: str,
  278. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  279. # The extra values given here take precedence over values defined on the client or passed to this method.
  280. extra_headers: Headers | None = None,
  281. extra_query: Query | None = None,
  282. extra_body: Body | None = None,
  283. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  284. ) -> FileRetrieveResponse:
  285. """
  286. Retrieve Container File
  287. Args:
  288. extra_headers: Send extra headers
  289. extra_query: Add additional query parameters to the request
  290. extra_body: Add additional JSON properties to the request
  291. timeout: Override the client-level default timeout for this request, in seconds
  292. """
  293. if not container_id:
  294. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  295. if not file_id:
  296. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  297. return await self._get(
  298. f"/containers/{container_id}/files/{file_id}",
  299. options=make_request_options(
  300. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  301. ),
  302. cast_to=FileRetrieveResponse,
  303. )
  304. def list(
  305. self,
  306. container_id: str,
  307. *,
  308. after: str | Omit = omit,
  309. limit: int | Omit = omit,
  310. order: Literal["asc", "desc"] | Omit = omit,
  311. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  312. # The extra values given here take precedence over values defined on the client or passed to this method.
  313. extra_headers: Headers | None = None,
  314. extra_query: Query | None = None,
  315. extra_body: Body | None = None,
  316. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  317. ) -> AsyncPaginator[FileListResponse, AsyncCursorPage[FileListResponse]]:
  318. """List Container files
  319. Args:
  320. after: A cursor for use in pagination.
  321. `after` is an object ID that defines your place
  322. in the list. For instance, if you make a list request and receive 100 objects,
  323. ending with obj_foo, your subsequent call can include after=obj_foo in order to
  324. fetch the next page of the list.
  325. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  326. 100, and the default is 20.
  327. order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
  328. order and `desc` for descending order.
  329. extra_headers: Send extra headers
  330. extra_query: Add additional query parameters to the request
  331. extra_body: Add additional JSON properties to the request
  332. timeout: Override the client-level default timeout for this request, in seconds
  333. """
  334. if not container_id:
  335. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  336. return self._get_api_list(
  337. f"/containers/{container_id}/files",
  338. page=AsyncCursorPage[FileListResponse],
  339. options=make_request_options(
  340. extra_headers=extra_headers,
  341. extra_query=extra_query,
  342. extra_body=extra_body,
  343. timeout=timeout,
  344. query=maybe_transform(
  345. {
  346. "after": after,
  347. "limit": limit,
  348. "order": order,
  349. },
  350. file_list_params.FileListParams,
  351. ),
  352. ),
  353. model=FileListResponse,
  354. )
  355. async def delete(
  356. self,
  357. file_id: str,
  358. *,
  359. container_id: str,
  360. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  361. # The extra values given here take precedence over values defined on the client or passed to this method.
  362. extra_headers: Headers | None = None,
  363. extra_query: Query | None = None,
  364. extra_body: Body | None = None,
  365. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  366. ) -> None:
  367. """
  368. Delete Container File
  369. Args:
  370. extra_headers: Send extra headers
  371. extra_query: Add additional query parameters to the request
  372. extra_body: Add additional JSON properties to the request
  373. timeout: Override the client-level default timeout for this request, in seconds
  374. """
  375. if not container_id:
  376. raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
  377. if not file_id:
  378. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  379. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  380. return await self._delete(
  381. f"/containers/{container_id}/files/{file_id}",
  382. options=make_request_options(
  383. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  384. ),
  385. cast_to=NoneType,
  386. )
  387. class FilesWithRawResponse:
  388. def __init__(self, files: Files) -> None:
  389. self._files = files
  390. self.create = _legacy_response.to_raw_response_wrapper(
  391. files.create,
  392. )
  393. self.retrieve = _legacy_response.to_raw_response_wrapper(
  394. files.retrieve,
  395. )
  396. self.list = _legacy_response.to_raw_response_wrapper(
  397. files.list,
  398. )
  399. self.delete = _legacy_response.to_raw_response_wrapper(
  400. files.delete,
  401. )
  402. @cached_property
  403. def content(self) -> ContentWithRawResponse:
  404. return ContentWithRawResponse(self._files.content)
  405. class AsyncFilesWithRawResponse:
  406. def __init__(self, files: AsyncFiles) -> None:
  407. self._files = files
  408. self.create = _legacy_response.async_to_raw_response_wrapper(
  409. files.create,
  410. )
  411. self.retrieve = _legacy_response.async_to_raw_response_wrapper(
  412. files.retrieve,
  413. )
  414. self.list = _legacy_response.async_to_raw_response_wrapper(
  415. files.list,
  416. )
  417. self.delete = _legacy_response.async_to_raw_response_wrapper(
  418. files.delete,
  419. )
  420. @cached_property
  421. def content(self) -> AsyncContentWithRawResponse:
  422. return AsyncContentWithRawResponse(self._files.content)
  423. class FilesWithStreamingResponse:
  424. def __init__(self, files: Files) -> None:
  425. self._files = files
  426. self.create = to_streamed_response_wrapper(
  427. files.create,
  428. )
  429. self.retrieve = to_streamed_response_wrapper(
  430. files.retrieve,
  431. )
  432. self.list = to_streamed_response_wrapper(
  433. files.list,
  434. )
  435. self.delete = to_streamed_response_wrapper(
  436. files.delete,
  437. )
  438. @cached_property
  439. def content(self) -> ContentWithStreamingResponse:
  440. return ContentWithStreamingResponse(self._files.content)
  441. class AsyncFilesWithStreamingResponse:
  442. def __init__(self, files: AsyncFiles) -> None:
  443. self._files = files
  444. self.create = async_to_streamed_response_wrapper(
  445. files.create,
  446. )
  447. self.retrieve = async_to_streamed_response_wrapper(
  448. files.retrieve,
  449. )
  450. self.list = async_to_streamed_response_wrapper(
  451. files.list,
  452. )
  453. self.delete = async_to_streamed_response_wrapper(
  454. files.delete,
  455. )
  456. @cached_property
  457. def content(self) -> AsyncContentWithStreamingResponse:
  458. return AsyncContentWithStreamingResponse(self._files.content)