|
| 1 | +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Mapping, cast |
| 6 | + |
| 7 | +import httpx |
| 8 | + |
| 9 | +from ... import _legacy_response |
| 10 | +from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven, FileTypes |
| 11 | +from ..._utils import ( |
| 12 | + extract_files, |
| 13 | + maybe_transform, |
| 14 | + deepcopy_minimal, |
| 15 | + async_maybe_transform, |
| 16 | +) |
| 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 ..._base_client import make_request_options |
| 21 | +from ...types.uploads import part_create_params |
| 22 | +from ...types.uploads.upload_part import UploadPart |
| 23 | + |
| 24 | +__all__ = ["Parts", "AsyncParts"] |
| 25 | + |
| 26 | + |
| 27 | +class Parts(SyncAPIResource): |
| 28 | + @cached_property |
| 29 | + def with_raw_response(self) -> PartsWithRawResponse: |
| 30 | + return PartsWithRawResponse(self) |
| 31 | + |
| 32 | + @cached_property |
| 33 | + def with_streaming_response(self) -> PartsWithStreamingResponse: |
| 34 | + return PartsWithStreamingResponse(self) |
| 35 | + |
| 36 | + def create( |
| 37 | + self, |
| 38 | + upload_id: str, |
| 39 | + *, |
| 40 | + data: FileTypes, |
| 41 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 42 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 43 | + extra_headers: Headers | None = None, |
| 44 | + extra_query: Query | None = None, |
| 45 | + extra_body: Body | None = None, |
| 46 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 47 | + ) -> UploadPart: |
| 48 | + """ |
| 49 | + Adds a |
| 50 | + [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an |
| 51 | + [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object. |
| 52 | + A Part represents a chunk of bytes from the file you are trying to upload. |
| 53 | +
|
| 54 | + Each Part can be at most 64 MB, and you can add Parts until you hit the Upload |
| 55 | + maximum of 8 GB. |
| 56 | +
|
| 57 | + It is possible to add multiple Parts in parallel. You can decide the intended |
| 58 | + order of the Parts when you |
| 59 | + [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete). |
| 60 | +
|
| 61 | + Args: |
| 62 | + data: The chunk of bytes for this Part. |
| 63 | +
|
| 64 | + extra_headers: Send extra headers |
| 65 | +
|
| 66 | + extra_query: Add additional query parameters to the request |
| 67 | +
|
| 68 | + extra_body: Add additional JSON properties to the request |
| 69 | +
|
| 70 | + timeout: Override the client-level default timeout for this request, in seconds |
| 71 | + """ |
| 72 | + if not upload_id: |
| 73 | + raise ValueError(f"Expected a non-empty value for `upload_id` but received {upload_id!r}") |
| 74 | + body = deepcopy_minimal({"data": data}) |
| 75 | + files = extract_files(cast(Mapping[str, object], body), paths=[["data"]]) |
| 76 | + # It should be noted that the actual Content-Type header that will be |
| 77 | + # sent to the server will contain a `boundary` parameter, e.g. |
| 78 | + # multipart/form-data; boundary=---abc-- |
| 79 | + extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} |
| 80 | + return self._post( |
| 81 | + f"/uploads/{upload_id}/parts", |
| 82 | + body=maybe_transform(body, part_create_params.PartCreateParams), |
| 83 | + files=files, |
| 84 | + options=make_request_options( |
| 85 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 86 | + ), |
| 87 | + cast_to=UploadPart, |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +class AsyncParts(AsyncAPIResource): |
| 92 | + @cached_property |
| 93 | + def with_raw_response(self) -> AsyncPartsWithRawResponse: |
| 94 | + return AsyncPartsWithRawResponse(self) |
| 95 | + |
| 96 | + @cached_property |
| 97 | + def with_streaming_response(self) -> AsyncPartsWithStreamingResponse: |
| 98 | + return AsyncPartsWithStreamingResponse(self) |
| 99 | + |
| 100 | + async def create( |
| 101 | + self, |
| 102 | + upload_id: str, |
| 103 | + *, |
| 104 | + data: FileTypes, |
| 105 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 106 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 107 | + extra_headers: Headers | None = None, |
| 108 | + extra_query: Query | None = None, |
| 109 | + extra_body: Body | None = None, |
| 110 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 111 | + ) -> UploadPart: |
| 112 | + """ |
| 113 | + Adds a |
| 114 | + [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an |
| 115 | + [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object. |
| 116 | + A Part represents a chunk of bytes from the file you are trying to upload. |
| 117 | +
|
| 118 | + Each Part can be at most 64 MB, and you can add Parts until you hit the Upload |
| 119 | + maximum of 8 GB. |
| 120 | +
|
| 121 | + It is possible to add multiple Parts in parallel. You can decide the intended |
| 122 | + order of the Parts when you |
| 123 | + [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete). |
| 124 | +
|
| 125 | + Args: |
| 126 | + data: The chunk of bytes for this Part. |
| 127 | +
|
| 128 | + extra_headers: Send extra headers |
| 129 | +
|
| 130 | + extra_query: Add additional query parameters to the request |
| 131 | +
|
| 132 | + extra_body: Add additional JSON properties to the request |
| 133 | +
|
| 134 | + timeout: Override the client-level default timeout for this request, in seconds |
| 135 | + """ |
| 136 | + if not upload_id: |
| 137 | + raise ValueError(f"Expected a non-empty value for `upload_id` but received {upload_id!r}") |
| 138 | + body = deepcopy_minimal({"data": data}) |
| 139 | + files = extract_files(cast(Mapping[str, object], body), paths=[["data"]]) |
| 140 | + # It should be noted that the actual Content-Type header that will be |
| 141 | + # sent to the server will contain a `boundary` parameter, e.g. |
| 142 | + # multipart/form-data; boundary=---abc-- |
| 143 | + extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} |
| 144 | + return await self._post( |
| 145 | + f"/uploads/{upload_id}/parts", |
| 146 | + body=await async_maybe_transform(body, part_create_params.PartCreateParams), |
| 147 | + files=files, |
| 148 | + options=make_request_options( |
| 149 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 150 | + ), |
| 151 | + cast_to=UploadPart, |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +class PartsWithRawResponse: |
| 156 | + def __init__(self, parts: Parts) -> None: |
| 157 | + self._parts = parts |
| 158 | + |
| 159 | + self.create = _legacy_response.to_raw_response_wrapper( |
| 160 | + parts.create, |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +class AsyncPartsWithRawResponse: |
| 165 | + def __init__(self, parts: AsyncParts) -> None: |
| 166 | + self._parts = parts |
| 167 | + |
| 168 | + self.create = _legacy_response.async_to_raw_response_wrapper( |
| 169 | + parts.create, |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +class PartsWithStreamingResponse: |
| 174 | + def __init__(self, parts: Parts) -> None: |
| 175 | + self._parts = parts |
| 176 | + |
| 177 | + self.create = to_streamed_response_wrapper( |
| 178 | + parts.create, |
| 179 | + ) |
| 180 | + |
| 181 | + |
| 182 | +class AsyncPartsWithStreamingResponse: |
| 183 | + def __init__(self, parts: AsyncParts) -> None: |
| 184 | + self._parts = parts |
| 185 | + |
| 186 | + self.create = async_to_streamed_response_wrapper( |
| 187 | + parts.create, |
| 188 | + ) |
0 commit comments