-
Notifications
You must be signed in to change notification settings - Fork 3
/
mp_req.go
165 lines (153 loc) · 3.64 KB
/
mp_req.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
163
164
165
// Copyright 2020 YBCZ, Inc. All rights reserved.
//
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file in the root of the source
// tree.
package wx
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/blusewang/wx/mp_api"
"github.com/google/go-querystring/query"
"io"
"mime/multipart"
"net/http"
"net/url"
"reflect"
)
// Api请求数据体
type mpReq struct {
account MpAccount
path mp_api.MpApi
param interface{}
sendData interface{}
res interface{}
err error
}
// Query 填充查询信息
// access_token 会自动填充,无需指定
func (mp *mpReq) Query(d interface{}) *mpReq {
mp.param = d
return mp
}
// SendData 填充POST里的Body数据
func (mp *mpReq) SendData(d interface{}) *mpReq {
mp.sendData = d
return mp
}
// Bind 绑定请求结果的解码数据体
func (mp *mpReq) Bind(d interface{}) *mpReq {
if reflect.ValueOf(d).Kind() != reflect.Ptr {
mp.err = errors.New("mp.Bind must be Ptr")
}
mp.res = d
return mp
}
// Do 执行
func (mp *mpReq) Do() (err error) {
if mp.err != nil {
return mp.err
}
var v url.Values
v, err = query.Values(mp.param)
if err != nil {
return err
}
if mp.account.AccessToken != "" {
v.Set("access_token", mp.account.AccessToken)
}
if mp.account.ServerHost == "" {
mp.account.ServerHost = mp_api.ServerHostUniversal
}
var apiUrl = fmt.Sprintf("https://%v/%v?%v", mp.account.ServerHost, mp.path, v.Encode())
var resp *http.Response
if mp.sendData == nil {
resp, err = client().Get(apiUrl)
} else {
var buf = new(bytes.Buffer)
var coder = json.NewEncoder(buf)
coder.SetEscapeHTML(false)
if err = coder.Encode(mp.sendData); err != nil {
return
}
resp, err = client().Post(apiUrl, "application/json", buf)
}
if err != nil {
return
}
if mp.res == nil {
mp.res = &mp_api.MpBaseResp{}
}
if err = json.NewDecoder(resp.Body).Decode(mp.res); err != nil {
return
}
rv := reflect.ValueOf(mp.res).Elem()
for i := 0; i < rv.NumField(); i++ {
iv := rv.Field(i)
if iv.Type().String() == "mp_api.MpBaseResp" {
if iv.FieldByName("ErrCode").Int() > 0 {
err = errors.New(fmt.Sprintf("%v %v", iv.FieldByName("ErrCode").Int(), iv.FieldByName("ErrMsg").String()))
return
}
}
}
bs, has := mp.res.(*mp_api.MpBaseResp)
if has {
if bs.ErrCode > 0 {
err = errors.New(fmt.Sprintf("%v %v", bs.ErrCode, bs.ErrMsg))
}
}
return
}
// 上传文档。
// reader 一个打开的文件reader。
// fileExtension 该文件的后缀名。
func (mp *mpReq) Upload(reader io.Reader, fileExtension string) (err error) {
if mp.err != nil {
return mp.err
}
var v url.Values
v, err = query.Values(mp.param)
if err != nil {
return err
}
if mp.account.AccessToken != "" {
v.Set("access_token", mp.account.AccessToken)
}
if mp.account.ServerHost == "" {
mp.account.ServerHost = mp_api.ServerHostUniversal
}
var apiUrl = fmt.Sprintf("https://%v/%v?%v", mp.account.ServerHost, mp.path, v.Encode())
body := &bytes.Buffer{}
w := multipart.NewWriter(body)
wf, err := w.CreateFormFile("media", fmt.Sprintf("/tmp/%v.%v", NewRandStr(23), fileExtension))
if err != nil {
return
}
if _, err = io.Copy(wf, reader); err != nil {
return
}
// 关闭`w`令数据从缓冲区刷写入`body`
if err = w.Close(); err != nil {
return
}
resp, err := client().Post(apiUrl, w.FormDataContentType(), body)
if err != nil {
return
}
if mp.res == nil {
mp.res = &mp_api.MpBaseResp{}
}
if err = json.NewDecoder(resp.Body).Decode(mp.res); err != nil {
return
}
bs, has := mp.res.(*mp_api.MpBaseResp)
if has {
if bs.ErrCode > 0 {
err = errors.New(fmt.Sprintf("%v %v", bs.ErrCode, bs.ErrMsg))
}
}
return
}