parts.py 7.9 KB

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