/
body_provider.go
95 lines (79 loc) · 2.15 KB
/
body_provider.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
/*******************************************************************************
* IBM Confidential
* OCO Source Materials
* IBM Cloud Container Service, 5737-D43
* (C) Copyright IBM Corp. 2018, 2019 All Rights Reserved.
* The source code for this program is not published or otherwise divested of
* its trade secrets, irrespective of what has been deposited with
* the U.S. Copyright Office.
******************************************************************************/
package payload
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
)
// JSONBodyProvider ...
type JSONBodyProvider struct{ payload interface{} }
// NewJSONBodyProvider ...
func NewJSONBodyProvider(p interface{}) *JSONBodyProvider {
return &JSONBodyProvider{payload: p}
}
// ContentType ...
func (p *JSONBodyProvider) ContentType() string {
return "application/json"
}
// Body ...
func (p *JSONBodyProvider) Body() (io.Reader, error) {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(p.payload)
if err != nil {
return nil, err
}
return buf, nil
}
// MultipartFileBody ...
type MultipartFileBody struct {
name string
contents io.Reader
multipartWriter *multipart.Writer
pipeReader *io.PipeReader
pipeWriter *io.PipeWriter
}
// NewMultipartFileBody ...
func NewMultipartFileBody(name string, contents io.Reader) *MultipartFileBody {
pr, pw := io.Pipe()
return &MultipartFileBody{
name: name,
contents: contents,
pipeReader: pr,
pipeWriter: pw,
multipartWriter: multipart.NewWriter(pw),
}
}
// ContentType ...
func (p *MultipartFileBody) ContentType() string {
return p.multipartWriter.FormDataContentType()
}
// Body ...
func (p *MultipartFileBody) Body() (io.Reader, error) {
go p.copyBody()
return p.pipeReader, nil
}
func (p *MultipartFileBody) copyBody() {
defer p.Close()
fileWriter, err := p.multipartWriter.CreateFormFile(p.name, "image")
if err != nil {
p.pipeWriter.CloseWithError(err)
}
_, err = io.Copy(fileWriter, p.contents)
if err != nil {
p.pipeWriter.CloseWithError(err)
}
}
// Close ...
func (p *MultipartFileBody) Close() {
p.multipartWriter.Close()
p.pipeWriter.Close()
}