-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiles.go
108 lines (88 loc) · 2.25 KB
/
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
package coze
import (
"context"
"io"
"net/http"
)
func (r *files) Upload(ctx context.Context, req *UploadFilesReq) (*UploadFilesResp, error) {
path := "/v1/files/upload"
resp := &uploadFilesResp{}
err := r.core.UploadFile(ctx, path, req.File, req.File.Name(), nil, resp)
if err != nil {
return nil, err
}
resp.FileInfo.setHTTPResponse(resp.HTTPResponse)
return resp.FileInfo, nil
}
func (r *files) Retrieve(ctx context.Context, req *RetrieveFilesReq) (*RetrieveFilesResp, error) {
method := http.MethodPost
uri := "/v1/files/retrieve"
resp := &retrieveFilesResp{}
err := r.core.Request(ctx, method, uri, nil, resp, withHTTPQuery("file_id", req.FileID))
if err != nil {
return nil, err
}
resp.FileInfo.setHTTPResponse(resp.HTTPResponse)
return resp.FileInfo, nil
}
type files struct {
core *core
}
func newFiles(core *core) *files {
return &files{core: core}
}
// FileInfo represents information about a file
type FileInfo struct {
// The ID of the uploaded file.
ID string `json:"id"`
// The total byte size of the file.
Bytes int `json:"bytes"`
// The upload time of the file, in the format of a 10-digit Unix timestamp in seconds (s).
CreatedAt int `json:"created_at"`
// The name of the file.
FileName string `json:"file_name"`
}
type FileTypes interface {
io.Reader
Name() string
}
type implFileInterface struct {
io.Reader
fileName string
}
func (r *implFileInterface) Name() string {
return r.fileName
}
type UploadFilesReq struct {
File FileTypes
}
func NewUploadFile(reader io.Reader, fileName string) FileTypes {
return &implFileInterface{
Reader: reader,
fileName: fileName,
}
}
// RetrieveFilesReq represents request for retrieving file
type RetrieveFilesReq struct {
FileID string `json:"file_id"`
}
// uploadFilesResp represents response for uploading file
type uploadFilesResp struct {
baseResponse
FileInfo *UploadFilesResp `json:"data"`
}
// UploadFilesResp represents response for uploading file
type UploadFilesResp struct {
baseModel
FileInfo
}
// retrieveFilesResp represents response for retrieving file
type retrieveFilesResp struct {
baseResponse
FileInfo *RetrieveFilesResp `json:"data"`
}
// RetrieveFilesResp represents response for retrieving file
type RetrieveFilesResp struct {
baseModel
FileInfo
}