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