uploadpart.go

  1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3package openai
  4
  5import (
  6	"bytes"
  7	"context"
  8	"errors"
  9	"fmt"
 10	"io"
 11	"mime/multipart"
 12	"net/http"
 13
 14	"github.com/openai/openai-go/internal/apiform"
 15	"github.com/openai/openai-go/internal/apijson"
 16	"github.com/openai/openai-go/internal/requestconfig"
 17	"github.com/openai/openai-go/option"
 18	"github.com/openai/openai-go/packages/respjson"
 19	"github.com/openai/openai-go/shared/constant"
 20)
 21
 22// UploadPartService contains methods and other services that help with interacting
 23// with the openai API.
 24//
 25// Note, unlike clients, this service does not read variables from the environment
 26// automatically. You should not instantiate this service directly, and instead use
 27// the [NewUploadPartService] method instead.
 28type UploadPartService struct {
 29	Options []option.RequestOption
 30}
 31
 32// NewUploadPartService generates a new service that applies the given options to
 33// each request. These options are applied after the parent client's options (if
 34// there is one), and before any request-specific options.
 35func NewUploadPartService(opts ...option.RequestOption) (r UploadPartService) {
 36	r = UploadPartService{}
 37	r.Options = opts
 38	return
 39}
 40
 41// Adds a
 42// [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an
 43// [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object.
 44// A Part represents a chunk of bytes from the file you are trying to upload.
 45//
 46// Each Part can be at most 64 MB, and you can add Parts until you hit the Upload
 47// maximum of 8 GB.
 48//
 49// It is possible to add multiple Parts in parallel. You can decide the intended
 50// order of the Parts when you
 51// [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete).
 52func (r *UploadPartService) New(ctx context.Context, uploadID string, body UploadPartNewParams, opts ...option.RequestOption) (res *UploadPart, err error) {
 53	opts = append(r.Options[:], opts...)
 54	if uploadID == "" {
 55		err = errors.New("missing required upload_id parameter")
 56		return
 57	}
 58	path := fmt.Sprintf("uploads/%s/parts", uploadID)
 59	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
 60	return
 61}
 62
 63// The upload Part represents a chunk of bytes we can add to an Upload object.
 64type UploadPart struct {
 65	// The upload Part unique identifier, which can be referenced in API endpoints.
 66	ID string `json:"id,required"`
 67	// The Unix timestamp (in seconds) for when the Part was created.
 68	CreatedAt int64 `json:"created_at,required"`
 69	// The object type, which is always `upload.part`.
 70	Object constant.UploadPart `json:"object,required"`
 71	// The ID of the Upload object that this Part was added to.
 72	UploadID string `json:"upload_id,required"`
 73	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
 74	JSON struct {
 75		ID          respjson.Field
 76		CreatedAt   respjson.Field
 77		Object      respjson.Field
 78		UploadID    respjson.Field
 79		ExtraFields map[string]respjson.Field
 80		raw         string
 81	} `json:"-"`
 82}
 83
 84// Returns the unmodified JSON received from the API
 85func (r UploadPart) RawJSON() string { return r.JSON.raw }
 86func (r *UploadPart) UnmarshalJSON(data []byte) error {
 87	return apijson.UnmarshalRoot(data, r)
 88}
 89
 90type UploadPartNewParams struct {
 91	// The chunk of bytes for this Part.
 92	Data io.Reader `json:"data,omitzero,required" format:"binary"`
 93	paramObj
 94}
 95
 96func (r UploadPartNewParams) MarshalMultipart() (data []byte, contentType string, err error) {
 97	buf := bytes.NewBuffer(nil)
 98	writer := multipart.NewWriter(buf)
 99	err = apiform.MarshalRoot(r, writer)
100	if err == nil {
101		err = apiform.WriteExtras(writer, r.ExtraFields())
102	}
103	if err != nil {
104		writer.Close()
105		return nil, "", err
106	}
107	err = writer.Close()
108	if err != nil {
109		return nil, "", err
110	}
111	return buf.Bytes(), writer.FormDataContentType(), nil
112}