forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
form_builder.go
49 lines (40 loc) · 1002 Bytes
/
form_builder.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
package openai
import (
"io"
"mime/multipart"
"os"
)
type formBuilder interface {
createFormFile(fieldname string, file *os.File) error
writeField(fieldname, value string) error
close() error
formDataContentType() string
}
type defaultFormBuilder struct {
writer *multipart.Writer
}
func newFormBuilder(body io.Writer) *defaultFormBuilder {
return &defaultFormBuilder{
writer: multipart.NewWriter(body),
}
}
func (fb *defaultFormBuilder) createFormFile(fieldname string, file *os.File) error {
fieldWriter, err := fb.writer.CreateFormFile(fieldname, file.Name())
if err != nil {
return err
}
_, err = io.Copy(fieldWriter, file)
if err != nil {
return err
}
return nil
}
func (fb *defaultFormBuilder) writeField(fieldname, value string) error {
return fb.writer.WriteField(fieldname, value)
}
func (fb *defaultFormBuilder) close() error {
return fb.writer.Close()
}
func (fb *defaultFormBuilder) formDataContentType() string {
return fb.writer.FormDataContentType()
}