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