-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.go
134 lines (119 loc) · 2.92 KB
/
base.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
package clients
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"github.com/golang/glog"
)
var UserAgent string
type BaseClient struct {
*http.Client
BaseUrl string
BaseHeaders map[string]string
}
type Request struct {
Method, URL string
Body io.Reader
ContentType string
Headers map[string]string
}
type HTTPStatusError struct {
Status int
Body string
}
func (e *HTTPStatusError) Error() string {
return fmt.Sprintf("http status %d %s: %q", e.Status, http.StatusText(e.Status), e.Body)
}
func (c *BaseClient) DoRequest(ctx context.Context, r Request, output interface{}) error {
req, err := c.newRequest(ctx, r)
if err != nil {
return err
}
client := http.DefaultClient
if c.Client != nil {
client = c.Client
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if (r.Method == "GET" && resp.StatusCode != http.StatusOK) || (resp.StatusCode >= 300) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
glog.Warningf("Error reading response body: url=%s err=%v", r.URL, err)
}
return &HTTPStatusError{resp.StatusCode, string(body)}
}
if output == nil {
return nil
}
return json.NewDecoder(resp.Body).Decode(output)
}
func (c *BaseClient) newRequest(ctx context.Context, r Request) (*http.Request, error) {
url := c.BaseUrl + r.URL
req, err := http.NewRequestWithContext(ctx, r.Method, url, r.Body)
if err != nil {
return nil, err
}
if UserAgent != "" {
req.Header.Set("User-Agent", UserAgent)
}
for key, value := range c.BaseHeaders {
req.Header.Set(key, value)
}
for key, value := range r.Headers {
req.Header.Set(key, value)
}
if r.ContentType != "" {
req.Header.Set("Content-Type", r.ContentType)
}
return req, nil
}
type part struct {
name string
filename string
fileContentType string
data io.Reader
}
func multipartBody(parts []part) (body io.ReadCloser, contentType string) {
body, pipe := io.Pipe()
mw := multipart.NewWriter(pipe)
go func() (err error) {
defer func() { pipe.CloseWithError(err) }()
for _, p := range parts {
err = writePart(mw, p.name, p.filename, p.fileContentType, p.data)
if err != nil {
return
}
}
err = mw.Close()
return
}()
return body, mw.FormDataContentType()
}
func writePart(mw *multipart.Writer, name, filename, contentType string, data io.Reader) error {
partw, err := mw.CreatePart(mimeHeader(name, filename, contentType))
if err != nil {
return err
}
_, err = io.Copy(partw, data)
return err
}
func mimeHeader(name, filename, contentType string) textproto.MIMEHeader {
contentDisposition := fmt.Sprintf(`form-data; name=%q`, name)
if filename != "" {
contentDisposition += fmt.Sprintf(`; filename=%q`, filename)
}
mime := textproto.MIMEHeader{}
mime.Set("Content-Disposition", contentDisposition)
if contentType != "" {
mime.Set("Content-Type", contentType)
}
return mime
}