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