-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequester.go
executable file
·155 lines (131 loc) · 4.15 KB
/
requester.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
package rcc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/baidu/brcc/brcc-go-sdk/logutil"
"go.uber.org/zap"
)
// this is a static check
var _ IRequester = (*httpRequester)(nil)
// IRequester
type IRequester interface {
Get(url string, respData interface{}) error
Post(url string, data interface{}, respData interface{}) error
Delete(url string, respData interface{}) error
}
type httpRequester struct {
ctx context.Context
client *http.Client
}
func newHTTPRequester(ctx context.Context, client *http.Client) IRequester {
return &httpRequester{
ctx: ctx,
client: client,
}
}
// Get
func (r *httpRequester) Get(url string, respData interface{}) error {
resp, err := r.client.Get(url)
response := &ApiResponse{
Data: respData,
}
return r.procRsp(resp, err, response)
}
// Post
func (r *httpRequester) Post(url string, data interface{}, respData interface{}) error {
jsonData, _ := json.Marshal(data)
buffer := bytes.NewBuffer(jsonData)
resp, err := r.client.Post(url, "application/json;charset=UTF-8", buffer)
response := &ApiResponse{
Data: respData,
}
return r.procRsp(resp, err, response)
}
// Post2
func (r *httpRequester) Post2(url string, data interface{}, Headers map[string]string, respData interface{}) error {
jsonData, _ := json.Marshal(data)
buffer := bytes.NewBuffer(jsonData)
req, _ := http.NewRequest("POST", url, buffer)
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
for key, val := range Headers {
req.Header.Set(key, val)
}
resp, err := r.client.Do(req)
response := &ApiResponse{
Data: respData,
}
return r.procRsp(resp, err, response)
}
// Put
func (r *httpRequester) Put(url string, data interface{}, respData interface{}) error {
jsonData, _ := json.Marshal(data)
buffer := bytes.NewBuffer(jsonData)
req, _ := http.NewRequest("PUT", url, buffer)
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
resp, err := r.client.Do(req)
response := &ApiResponse{
Data: respData,
}
return r.procRsp(resp, err, response)
}
// Delete
func (r *httpRequester) Delete(url string, respData interface{}) error {
req, _ := http.NewRequest("DELETE", url, nil)
resp, err := r.client.Do(req)
response := &ApiResponse{
Data: respData,
}
return r.procRsp(resp, err, response)
}
type ApiResponse struct {
Status int `json:"status"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func (r *httpRequester) procRsp(resp *http.Response, err error, response *ApiResponse) error {
if err != nil {
errorString := fmt.Sprintf("rcc server resp error %s", err)
logutil.DefaultLogger().Warn(errorString)
return fmt.Errorf(errorString)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// Discard all body if status code is not 200
bodyBytes, _ := ioutil.ReadAll(resp.Body)
errorString := fmt.Sprintf("rcc server resp code not ok, method=%s, url=%s, code=%d, body=%s",
resp.Request.Method, resp.Request.URL.Path, resp.StatusCode, bodyBytes)
logRspError(errorString, resp)
return fmt.Errorf(errorString)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
errorString := fmt.Sprintf("procRsp ioutil.ReadAll error, method=%s, url=%s, code=%d, error=%s",
resp.Request.Method, resp.Request.URL.Path, resp.StatusCode, err)
logRspError(errorString, resp)
return fmt.Errorf(errorString)
}
err = json.Unmarshal(bodyBytes, response)
if err != nil {
errorString := fmt.Sprintf("procRsp json.Unmarshal error, method=%s, url=%s, code=%d, body=%s, error=%s",
resp.Request.Method, resp.Request.URL.Path, resp.StatusCode, bodyBytes, err)
logRspError(errorString, resp)
return fmt.Errorf(errorString)
}
if response.Status != 0 {
errorString := fmt.Sprintf("rcc response status not zero, method=%s, url=%s, code=%d, body=%s",
resp.Request.Method, resp.Request.URL.Path, resp.StatusCode, bodyBytes)
logRspError(errorString, resp)
return fmt.Errorf(errorString)
}
return nil
}
func logRspError(errorString string, resp *http.Response) {
logutil.DefaultLogger().Warn(errorString,
zap.String("method", resp.Request.Method),
zap.String("url", resp.Request.URL.Path),
zap.Int("status", resp.StatusCode))
}