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 "net/url"
14
15 "github.com/openai/openai-go/internal/apiform"
16 "github.com/openai/openai-go/internal/apijson"
17 "github.com/openai/openai-go/internal/apiquery"
18 "github.com/openai/openai-go/internal/requestconfig"
19 "github.com/openai/openai-go/option"
20 "github.com/openai/openai-go/packages/pagination"
21 "github.com/openai/openai-go/packages/param"
22 "github.com/openai/openai-go/packages/respjson"
23 "github.com/openai/openai-go/shared/constant"
24)
25
26// ContainerFileService contains methods and other services that help with
27// interacting with the openai API.
28//
29// Note, unlike clients, this service does not read variables from the environment
30// automatically. You should not instantiate this service directly, and instead use
31// the [NewContainerFileService] method instead.
32type ContainerFileService struct {
33 Options []option.RequestOption
34 Content ContainerFileContentService
35}
36
37// NewContainerFileService generates a new service that applies the given options
38// to each request. These options are applied after the parent client's options (if
39// there is one), and before any request-specific options.
40func NewContainerFileService(opts ...option.RequestOption) (r ContainerFileService) {
41 r = ContainerFileService{}
42 r.Options = opts
43 r.Content = NewContainerFileContentService(opts...)
44 return
45}
46
47// Create a Container File
48//
49// You can send either a multipart/form-data request with the raw file content, or
50// a JSON request with a file ID.
51func (r *ContainerFileService) New(ctx context.Context, containerID string, body ContainerFileNewParams, opts ...option.RequestOption) (res *ContainerFileNewResponse, err error) {
52 opts = append(r.Options[:], opts...)
53 if containerID == "" {
54 err = errors.New("missing required container_id parameter")
55 return
56 }
57 path := fmt.Sprintf("containers/%s/files", containerID)
58 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
59 return
60}
61
62// Retrieve Container File
63func (r *ContainerFileService) Get(ctx context.Context, containerID string, fileID string, opts ...option.RequestOption) (res *ContainerFileGetResponse, err error) {
64 opts = append(r.Options[:], opts...)
65 if containerID == "" {
66 err = errors.New("missing required container_id parameter")
67 return
68 }
69 if fileID == "" {
70 err = errors.New("missing required file_id parameter")
71 return
72 }
73 path := fmt.Sprintf("containers/%s/files/%s", containerID, fileID)
74 err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
75 return
76}
77
78// List Container files
79func (r *ContainerFileService) List(ctx context.Context, containerID string, query ContainerFileListParams, opts ...option.RequestOption) (res *pagination.CursorPage[ContainerFileListResponse], err error) {
80 var raw *http.Response
81 opts = append(r.Options[:], opts...)
82 opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
83 if containerID == "" {
84 err = errors.New("missing required container_id parameter")
85 return
86 }
87 path := fmt.Sprintf("containers/%s/files", containerID)
88 cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
89 if err != nil {
90 return nil, err
91 }
92 err = cfg.Execute()
93 if err != nil {
94 return nil, err
95 }
96 res.SetPageConfig(cfg, raw)
97 return res, nil
98}
99
100// List Container files
101func (r *ContainerFileService) ListAutoPaging(ctx context.Context, containerID string, query ContainerFileListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[ContainerFileListResponse] {
102 return pagination.NewCursorPageAutoPager(r.List(ctx, containerID, query, opts...))
103}
104
105// Delete Container File
106func (r *ContainerFileService) Delete(ctx context.Context, containerID string, fileID string, opts ...option.RequestOption) (err error) {
107 opts = append(r.Options[:], opts...)
108 opts = append([]option.RequestOption{option.WithHeader("Accept", "")}, opts...)
109 if containerID == "" {
110 err = errors.New("missing required container_id parameter")
111 return
112 }
113 if fileID == "" {
114 err = errors.New("missing required file_id parameter")
115 return
116 }
117 path := fmt.Sprintf("containers/%s/files/%s", containerID, fileID)
118 err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, nil, opts...)
119 return
120}
121
122type ContainerFileNewResponse struct {
123 // Unique identifier for the file.
124 ID string `json:"id,required"`
125 // Size of the file in bytes.
126 Bytes int64 `json:"bytes,required"`
127 // The container this file belongs to.
128 ContainerID string `json:"container_id,required"`
129 // Unix timestamp (in seconds) when the file was created.
130 CreatedAt int64 `json:"created_at,required"`
131 // The type of this object (`container.file`).
132 Object constant.ContainerFile `json:"object,required"`
133 // Path of the file in the container.
134 Path string `json:"path,required"`
135 // Source of the file (e.g., `user`, `assistant`).
136 Source string `json:"source,required"`
137 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
138 JSON struct {
139 ID respjson.Field
140 Bytes respjson.Field
141 ContainerID respjson.Field
142 CreatedAt respjson.Field
143 Object respjson.Field
144 Path respjson.Field
145 Source respjson.Field
146 ExtraFields map[string]respjson.Field
147 raw string
148 } `json:"-"`
149}
150
151// Returns the unmodified JSON received from the API
152func (r ContainerFileNewResponse) RawJSON() string { return r.JSON.raw }
153func (r *ContainerFileNewResponse) UnmarshalJSON(data []byte) error {
154 return apijson.UnmarshalRoot(data, r)
155}
156
157type ContainerFileGetResponse struct {
158 // Unique identifier for the file.
159 ID string `json:"id,required"`
160 // Size of the file in bytes.
161 Bytes int64 `json:"bytes,required"`
162 // The container this file belongs to.
163 ContainerID string `json:"container_id,required"`
164 // Unix timestamp (in seconds) when the file was created.
165 CreatedAt int64 `json:"created_at,required"`
166 // The type of this object (`container.file`).
167 Object constant.ContainerFile `json:"object,required"`
168 // Path of the file in the container.
169 Path string `json:"path,required"`
170 // Source of the file (e.g., `user`, `assistant`).
171 Source string `json:"source,required"`
172 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
173 JSON struct {
174 ID respjson.Field
175 Bytes respjson.Field
176 ContainerID respjson.Field
177 CreatedAt respjson.Field
178 Object respjson.Field
179 Path respjson.Field
180 Source respjson.Field
181 ExtraFields map[string]respjson.Field
182 raw string
183 } `json:"-"`
184}
185
186// Returns the unmodified JSON received from the API
187func (r ContainerFileGetResponse) RawJSON() string { return r.JSON.raw }
188func (r *ContainerFileGetResponse) UnmarshalJSON(data []byte) error {
189 return apijson.UnmarshalRoot(data, r)
190}
191
192type ContainerFileListResponse struct {
193 // Unique identifier for the file.
194 ID string `json:"id,required"`
195 // Size of the file in bytes.
196 Bytes int64 `json:"bytes,required"`
197 // The container this file belongs to.
198 ContainerID string `json:"container_id,required"`
199 // Unix timestamp (in seconds) when the file was created.
200 CreatedAt int64 `json:"created_at,required"`
201 // The type of this object (`container.file`).
202 Object constant.ContainerFile `json:"object,required"`
203 // Path of the file in the container.
204 Path string `json:"path,required"`
205 // Source of the file (e.g., `user`, `assistant`).
206 Source string `json:"source,required"`
207 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
208 JSON struct {
209 ID respjson.Field
210 Bytes respjson.Field
211 ContainerID respjson.Field
212 CreatedAt respjson.Field
213 Object respjson.Field
214 Path respjson.Field
215 Source respjson.Field
216 ExtraFields map[string]respjson.Field
217 raw string
218 } `json:"-"`
219}
220
221// Returns the unmodified JSON received from the API
222func (r ContainerFileListResponse) RawJSON() string { return r.JSON.raw }
223func (r *ContainerFileListResponse) UnmarshalJSON(data []byte) error {
224 return apijson.UnmarshalRoot(data, r)
225}
226
227type ContainerFileNewParams struct {
228 // Name of the file to create.
229 FileID param.Opt[string] `json:"file_id,omitzero"`
230 // The File object (not file name) to be uploaded.
231 File io.Reader `json:"file,omitzero" format:"binary"`
232 paramObj
233}
234
235func (r ContainerFileNewParams) MarshalMultipart() (data []byte, contentType string, err error) {
236 buf := bytes.NewBuffer(nil)
237 writer := multipart.NewWriter(buf)
238 err = apiform.MarshalRoot(r, writer)
239 if err == nil {
240 err = apiform.WriteExtras(writer, r.ExtraFields())
241 }
242 if err != nil {
243 writer.Close()
244 return nil, "", err
245 }
246 err = writer.Close()
247 if err != nil {
248 return nil, "", err
249 }
250 return buf.Bytes(), writer.FormDataContentType(), nil
251}
252
253type ContainerFileListParams struct {
254 // A cursor for use in pagination. `after` is an object ID that defines your place
255 // in the list. For instance, if you make a list request and receive 100 objects,
256 // ending with obj_foo, your subsequent call can include after=obj_foo in order to
257 // fetch the next page of the list.
258 After param.Opt[string] `query:"after,omitzero" json:"-"`
259 // A limit on the number of objects to be returned. Limit can range between 1 and
260 // 100, and the default is 20.
261 Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
262 // Sort order by the `created_at` timestamp of the objects. `asc` for ascending
263 // order and `desc` for descending order.
264 //
265 // Any of "asc", "desc".
266 Order ContainerFileListParamsOrder `query:"order,omitzero" json:"-"`
267 paramObj
268}
269
270// URLQuery serializes [ContainerFileListParams]'s query parameters as
271// `url.Values`.
272func (r ContainerFileListParams) URLQuery() (v url.Values, err error) {
273 return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
274 ArrayFormat: apiquery.ArrayQueryFormatBrackets,
275 NestedFormat: apiquery.NestedQueryFormatBrackets,
276 })
277}
278
279// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
280// order and `desc` for descending order.
281type ContainerFileListParamsOrder string
282
283const (
284 ContainerFileListParamsOrderAsc ContainerFileListParamsOrder = "asc"
285 ContainerFileListParamsOrderDesc ContainerFileListParamsOrder = "desc"
286)