-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi_files.go
297 lines (267 loc) · 7.58 KB
/
api_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package moonshot
import (
"bytes"
"context"
"fmt"
"os"
utils "github.com/northes/go-moonshot/internal/builder"
)
type IFiles interface {
Upload(ctx context.Context, req *FilesUploadRequest) (resp *FilesUploadResponse, err error)
UploadBytes(ctx context.Context, req *FilesUploadBytesRequest) (resp *FilesUploadBytesResponse, err error)
List(ctx context.Context) (res *FilesListResponse, err error)
Delete(ctx context.Context, fileID string) (resp *FilesDeleteResponse, err error)
BatchDelete(ctx context.Context, req *FilesBatchDeleteRequest) (resp *FilesBatchDeleteResponse, err error)
Info(ctx context.Context, fileID string) (resp *FilesInfoResponse, err error)
Content(ctx context.Context, fileID string) (resp *FileContentResponse, err error)
}
type files struct {
client *Client
}
// Files returns a new files controller
func (c *Client) Files() IFiles {
return &files{
client: c,
}
}
type FilesUploadRequest struct {
Name string
Path string
Purpose FilesPurpose
}
type FilesUploadResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
Status string `json:"status"`
StatusDetails string `json:"status_details"`
}
// Upload uploads a file to the server
func (f *files) Upload(ctx context.Context, req *FilesUploadRequest) (*FilesUploadResponse, error) {
const path = "/v1/files"
var b bytes.Buffer
builder := utils.NewFormBuilder(&b)
err := builder.WriteField("purpose", FilePurposeExtract.String())
if err != nil {
return nil, err
}
fileData, err := os.Open(req.Path)
if err != nil {
return nil, err
}
defer func(file *os.File) {
_ = file.Close()
}(fileData)
err = builder.CreateFormFile("file", fileData)
if err != nil {
return nil, err
}
err = builder.Close()
if err != nil {
return nil, err
}
resp, err := f.client.HTTPClient().
SetPath(path).
SetBody(&b).
SetContentType(builder.FormDataContentType()).
Post()
if err != nil {
return nil, err
}
if !resp.StatusOK() {
return nil, ResponseToError(resp)
}
uploadResponse := new(FilesUploadResponse)
err = resp.Unmarshal(uploadResponse)
if err != nil {
return nil, err
}
return uploadResponse, nil
}
type FilesUploadBytesRequest struct {
Name string
Bytes []byte
Purpose FilesPurpose
}
type FilesUploadBytesResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
Status string `json:"status"`
StatusDetails string `json:"status_details"`
}
// UploadBytes uploads a file to the server with bytes
func (f *files) UploadBytes(ctx context.Context, req *FilesUploadBytesRequest) (*FilesUploadBytesResponse, error) {
const path = "/v1/files"
var b bytes.Buffer
reader := bytes.NewReader(req.Bytes)
builder := utils.NewFormBuilder(&b)
err := builder.WriteField("purpose ", FilePurposeExtract.String())
if err != nil {
return nil, err
}
err = builder.CreateFormFileReader("file", reader, req.Name)
if err != nil {
return nil, err
}
err = builder.Close()
if err != nil {
return nil, err
}
resp, err := f.client.HTTPClient().
SetPath(path).
SetBody(&b).
SetContentType(builder.FormDataContentType()).
Post()
if err != nil {
return nil, err
}
if !resp.StatusOK() {
return nil, ResponseToError(resp)
}
uploadResponse := new(FilesUploadBytesResponse)
err = resp.Unmarshal(uploadResponse)
if err != nil {
return nil, err
}
return uploadResponse, nil
}
type FilesListRequest struct {
}
type FilesListResponse struct {
Object string `json:"object"`
Data []*FilesListResponseData `json:"data"`
}
type FilesListResponseData struct {
ID string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose FilesPurpose `json:"purpose"`
Status string `json:"status"`
StatusDetail string `json:"status_detail"`
}
// List lists all files
func (f *files) List(ctx context.Context) (*FilesListResponse, error) {
const path = "/v1/files"
resp, err := f.client.HTTPClient().SetPath(path).Get(ctx)
if err != nil {
return nil, err
}
if !resp.StatusOK() {
return nil, ResponseToError(resp)
}
listResponse := new(FilesListResponse)
err = resp.Unmarshal(listResponse)
if err != nil {
return nil, err
}
return listResponse, nil
}
type FilesDeleteResponse struct {
Deleted bool `json:"deleted"`
ID string `json:"id"`
Object string `json:"object"`
}
// Delete a file
func (f *files) Delete(ctx context.Context, fileID string) (*FilesDeleteResponse, error) {
const path = "/v1/files/%s"
fullPath := fmt.Sprintf(path, fileID)
resp, err := f.client.HTTPClient().SetPath(fullPath).Delete(ctx)
if err != nil {
return nil, err
}
if !resp.StatusOK() {
return nil, ResponseToError(resp)
}
deleteResponse := new(FilesDeleteResponse)
err = resp.Unmarshal(deleteResponse)
if err != nil {
return nil, err
}
return deleteResponse, nil
}
type FilesBatchDeleteRequest struct {
FileIDList []string `json:"file_ids"`
}
type FilesBatchDeleteResponse struct {
RespList []*FilesDeleteResponse `json:"resp_list"`
ErrorList []error `json:"error_list"`
}
// BatchDelete deletes multiple files
func (f *files) BatchDelete(ctx context.Context, req *FilesBatchDeleteRequest) (*FilesBatchDeleteResponse, error) {
if req == nil || len(req.FileIDList) == 0 {
return nil, fmt.Errorf("batch delete request must contain at least one file id")
}
deleteAllResp := &FilesBatchDeleteResponse{
RespList: make([]*FilesDeleteResponse, 0),
}
for _, fileID := range req.FileIDList {
deleteResp, err := f.Delete(ctx, fileID)
if err != nil {
return nil, err
}
deleteAllResp.RespList = append(deleteAllResp.RespList, deleteResp)
}
return deleteAllResp, nil
}
type FilesInfoResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
Status string `json:"status"`
StatusDetails string `json:"status_details"`
}
// Info returns information about a file
func (f *files) Info(ctx context.Context, fileID string) (*FilesInfoResponse, error) {
const path = "/v1/files/%s"
fullPath := fmt.Sprintf(path, fileID)
resp, err := f.client.HTTPClient().SetPath(fullPath).Get(ctx)
if err != nil {
return nil, err
}
if !resp.StatusOK() {
return nil, ResponseToError(resp)
}
infoResponse := new(FilesInfoResponse)
err = resp.Unmarshal(infoResponse)
if err != nil {
return nil, err
}
return infoResponse, nil
}
type FileContentResponse struct {
Content string `json:"content"`
FileType string `json:"file_type"`
Filename string `json:"filename"`
Title string `json:"title"`
Type string `json:"type"`
}
// Content returns the content of a file
func (f *files) Content(ctx context.Context, fileID string) (*FileContentResponse, error) {
const path = "/v1/files/%s/content"
fullPath := fmt.Sprintf(path, fileID)
resp, err := f.client.HTTPClient().SetPath(fullPath).Get(ctx)
if err != nil {
return nil, err
}
if !resp.StatusOK() {
return nil, ResponseToError(resp)
}
contentResponse := new(FileContentResponse)
err = resp.Unmarshal(contentResponse)
if err != nil {
return nil, err
}
return contentResponse, nil
}