-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
101 lines (80 loc) · 2.29 KB
/
request.go
File metadata and controls
101 lines (80 loc) · 2.29 KB
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
package weixin
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
//各种API请求的基础URL
const BaseURI = "https://api.weixin.qq.com/cgi-bin"
type BaseResponser interface {
CleanError()
FetchError() error
FetchErrorCode() int
}
//响应的基础结构,用于判断全局性的错误。所有的API响应都会包含该结构
type BaseResponse struct {
ErrCode int
ErrMsg string
}
func (resp *BaseResponse) FetchErrorCode() int {
return resp.ErrCode
}
func (resp *BaseResponse) CleanError() {
resp.ErrCode = 0
resp.ErrMsg = ""
}
func (resp *BaseResponse) FetchError() error {
if resp.ErrCode == 0 {
return nil
}
return fmt.Errorf("[%d]%s", resp.ErrCode, resp.ErrMsg)
}
//执行API请求,会自动添加AccessToken
func (client *Client) request(method, path string, getParams url.Values, bodyBytes []byte, respInfo BaseResponser) error {
//先带缓存请求
err := client.requestWithTokenCache(method, path, getParams, bodyBytes, respInfo)
if respInfo.FetchErrorCode() != 40001 {
return err
}
fmt.Println(respInfo.FetchError().Error(), "Retry")
//如果返回AccessToken相关的错误,则清缓存后重试一次
client.cleanAccessTokenCache()
respInfo.CleanError()
return client.requestWithTokenCache(method, path, getParams, bodyBytes, respInfo)
}
func (client *Client) requestWithTokenCache(method, path string, getParams url.Values, bodyBytes []byte, respInfo BaseResponser) error {
if getParams == nil {
getParams = url.Values{}
}
accessToken, err := client.getAccessToken()
if err != nil {
return fmt.Errorf("获取AccessToken错误: %s", err)
}
getParams.Set("access_token", accessToken)
uri := BaseURI + path + "?" + getParams.Encode()
req, err := http.NewRequest(method, uri, bytes.NewBuffer(bodyBytes))
if err != nil {
return fmt.Errorf("构造请求错误: %s", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("执行请求错误: %s", err)
}
defer resp.Body.Close()
if respInfo == nil {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("读取响应错误: %s", err)
}
fmt.Println(string(b))
return nil
}
err = json.NewDecoder(resp.Body).Decode(respInfo)
if err != nil {
return fmt.Errorf("读取响应错误: %s", err)
}
return respInfo.FetchError()
}