-
Notifications
You must be signed in to change notification settings - Fork 35
/
http.go
142 lines (120 loc) · 2.95 KB
/
http.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
package weixin
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"os"
"github.com/arstd/log"
)
type wxResp interface {
// 似有的 error 方法,保证外部(其他包)定义的 struct 只能内嵌
// WXError 的才能实现这个方法,才能作为当前包 http 方法的参数
error() error
}
type WXError struct {
ErrCode int `json:"errcode,omitempty"`
Errmsg string `json:"errmsg,omitempty"`
}
func (e *WXError) error() error {
if e.ErrCode == 0 {
return nil
}
return e
}
func (e *WXError) Error() string {
return fmt.Sprintf("weixin: (%d)%s", e.ErrCode, e.Errmsg)
}
var client = http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 128,
},
}
// GetUnmarshal HTTP 工具类, GET 并解析返回的报文,如果有错误,返回 error
func Get(url string, wxr wxResp) error {
resp, err := client.Get(url)
if err != nil {
return err
}
return parseWXResp(resp, wxr)
}
const contentType = "application/json"
// Post HTTP 工具类, POST 并解析返回的报文,如果有错误,返回 error
func Post(url string, v interface{}, wxr wxResp) (err error) {
js, ok := v.([]byte)
if !ok {
js, err = json.Marshal(v)
if err != nil {
return err
}
}
resp, err := client.Post(url, contentType, bytes.NewBuffer(js))
if err != nil {
return err
}
return parseWXResp(resp, wxr)
}
// Upload 工具类, 上传文件
func Upload(url, fieldName string, file *os.File, wxr wxResp, desc ...string) (err error) {
buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
//关键的一步操作
// fw, err := w.CreateFormField(file.Name())
fw, err := w.CreateFormFile(fieldName, file.Name())
if err != nil {
return err
}
_, err = io.Copy(fw, file)
if err != nil {
return err
}
contentType := w.FormDataContentType()
if len(desc) > 0 {
w.WriteField("description", desc[0])
}
w.Close()
log.Debugf("url=%s, fieldName=%s, fileName=%s", url, fieldName, file.Name())
resp, err := client.Post(url, contentType, buf)
if err != nil {
return err
}
return parseWXResp(resp, wxr)
}
func Download(url string) (filename string, body []byte, err error) {
resp, err := client.Get(url)
if err != nil {
return "", nil, err
}
var params map[string]string
if cd := resp.Header.Get("Content-Disposition"); cd == "" {
return "", nil, errors.New("missing Content-Disposition header")
} else if _, params, err = mime.ParseMediaType(cd); err != nil {
return "", nil, err
} else if filename = params["filename"]; filename == "" {
return "", nil, errors.New("no filename in Content-Disposition header")
}
body, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
return filename, body, err
}
func parseWXResp(resp *http.Response, wxr wxResp) error {
js, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
log.Trace("%s", js)
if wxr == nil {
wxr = &WXError{}
}
err = json.Unmarshal(js, wxr)
if err != nil {
return err
}
return wxr.error()
}