/
MultipartWriter.go
162 lines (131 loc) · 4.13 KB
/
MultipartWriter.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
package restclient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"github.com/app-nerds/kit/v6/restclient/responsegetter"
"github.com/sirupsen/logrus"
)
type MultipartWriterConfig struct {
Authorization string
BaseURL string
DebugMode bool
HTTPClient HTTPClientInterface
Logger *logrus.Entry
}
type MultipartWriter struct {
baseURL string
debugMode bool
httpClient HTTPClientInterface
logger *logrus.Entry
authorization string
writer *multipart.Writer
body *bytes.Buffer
}
/*
NewMultipartWriter creates a new MultipartWriter
*/
func NewMultipartWriter(config MultipartWriterConfig) *MultipartWriter {
result := &MultipartWriter{
baseURL: config.BaseURL,
debugMode: config.DebugMode,
httpClient: config.HTTPClient,
logger: config.Logger.WithField("component", "MultipartWriter"),
authorization: config.Authorization,
}
result.body = &bytes.Buffer{}
result.writer = multipart.NewWriter(result.body)
return result
}
/*
AddField adds a field to this multipart form. It accepts two types: string, json.Marshaler.
For the latter, this would be any struct or type that implements the json.Marshaler
interface.
*/
func (mw *MultipartWriter) AddField(name string, body interface{}) error {
var (
err error
ok bool
b []byte
)
if _, ok = body.(string); ok {
s, _ := body.(string)
return mw.writer.WriteField(name, s)
}
if b, err = json.Marshal(body); err != nil {
return fmt.Errorf("error converting body to JSON: %w", err)
}
return mw.writer.WriteField(name, string(b))
}
/*
AddFile adds a file to this multipart form.
*/
func (mw *MultipartWriter) AddFile(name, fileName string, fileContents io.Reader) error {
var (
err error
fileWriter io.Writer
)
if fileWriter, err = mw.writer.CreateFormFile(name, fileName); err != nil {
return fmt.Errorf("error initializing file form field: %w", err)
}
if _, err = io.Copy(fileWriter, fileContents); err != nil {
return fmt.Errorf("error copying file contents to buffer: %w", err)
}
return nil
}
/*
POST sends a multipart form via POST. The fields being sent should be added prior to calling
POST by using the AddField and AddFile methods.
*/
func (mw *MultipartWriter) POST(path string, successReceiver, errorReceiver interface{}) (*http.Response, error) {
var (
err error
request *http.Request
response *http.Response
)
_ = mw.writer.Close()
if request, err = http.NewRequest(http.MethodPost, mw.buildURL(path), mw.body); err != nil {
return response, fmt.Errorf("error creating multipart POST request: %w", err)
}
contentType := mw.writer.FormDataContentType()
request.Header.Add("Content-Type", contentType)
if mw.authorization != "" {
request.Header.Add("Authorization", mw.authorization)
}
if response, err = mw.httpClient.Do(request); err != nil {
return response, fmt.Errorf("error executing multipart POST request: %w", err)
}
defer response.Body.Close()
return response, responsegetter.Get(response, successReceiver, errorReceiver, mw.logger, mw.debugMode)
}
/*
PUT sends a multipart form via PUT. The fields being sent should be added prior to calling
PUT by using the AddField and AddFile methods.
*/
func (mw *MultipartWriter) PUT(path string, successReceiver, errorReceiver interface{}) (*http.Response, error) {
var (
err error
request *http.Request
response *http.Response
)
_ = mw.writer.Close()
if request, err = http.NewRequest(http.MethodPut, mw.buildURL(path), mw.body); err != nil {
return response, fmt.Errorf("error creating multipart POST request: %w", err)
}
contentType := mw.writer.FormDataContentType()
request.Header.Add("Content-Type", contentType)
if mw.authorization != "" {
request.Header.Add("Authorization", mw.authorization)
}
if response, err = mw.httpClient.Do(request); err != nil {
return response, fmt.Errorf("error executing multipart PUT request: %w", err)
}
defer response.Body.Close()
return response, responsegetter.Get(response, successReceiver, errorReceiver, mw.logger, mw.debugMode)
}
func (mw *MultipartWriter) buildURL(path string) string {
return fmt.Sprintf("%s%s", mw.baseURL, path)
}