-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
196 lines (169 loc) · 5.34 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package simplehttp
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strings"
"time"
)
type Client struct {
endpoint *url.URL
client http.Client
}
func NewClient(endpoint string) *Client {
s := strings.TrimRight(endpoint, "/")
u, err := url.Parse(s)
if err != nil {
fmt.Println("invalid endpoint")
return nil
}
return &Client{
endpoint: u,
client: http.Client{
Timeout: 20 * time.Second,
Transport: &http.Transport{DisableKeepAlives: true, Proxy: http.ProxyFromEnvironment},
},
}
}
func MapToUrlValues(content map[string]interface{}) url.Values {
query := url.Values{}
for k, v := range content {
var queryVal string
typeOfValue := reflect.TypeOf(v)
valueOfValue := reflect.ValueOf(v)
if typeOfValue.Kind().String() == "string" {
queryVal = fmt.Sprintf("%s", v)
} else if typeOfValue.String() == "*string" {
queryVal = *v.(*string)
} else if typeOfValue.Kind().String() == "ptr" && typeOfValue.Elem().Kind().String() == "string" {
queryVal = fmt.Sprintf("%s", valueOfValue.Elem())
} else {
j, err := json.Marshal(v)
if err != nil {
fmt.Println("marshal value failed", v)
continue
}
queryVal = string(j)
}
query.Add(k, queryVal)
}
return query
}
func (c *Client) BuildRequestUrl(path string, query map[string]interface{}) *url.URL {
values := MapToUrlValues(query)
u := *c.endpoint
u.Path += path
u.RawQuery = values.Encode()
return &u
}
func (c *Client) BuildRequestUrlWithRawQuery(path string, rawQuery string) *url.URL {
u := *c.endpoint
u.Path += path
u.RawQuery = rawQuery
return &u
}
func (c *Client) Get(path string, query map[string]interface{}, header http.Header) ([]byte, error) {
u := c.BuildRequestUrl(path, query)
return c.sendHttp("GET", u.String(), header, nil)
}
// GetString
//
// 某些api对参数顺序有要求, 只能自行构建query字符串
func (c *Client) GetString(path string, rawQuery string, header http.Header) ([]byte, error) {
u := c.BuildRequestUrlWithRawQuery(path, rawQuery)
return c.sendHttp("GET", u.String(), header, nil)
}
func (c *Client) PostForm(path string, params map[string]interface{}, header http.Header) ([]byte, error) {
return c.sendFormRequest("POST", path, params, header)
}
// PostFormString
//
// 某些api对参数顺序有要求, 只能自行构建body字符串
func (c *Client) PostFormString(path string, body string, header http.Header) ([]byte, error) {
return c.sendFormString("POST", path, body, header)
}
func (c *Client) PostJson(path string, params map[string]interface{}, header http.Header) ([]byte, error) {
return c.sendJsonRequest("POST", path, params, header)
}
func (c *Client) PutForm(path string, params map[string]interface{}, header http.Header) ([]byte, error) {
return c.sendFormRequest("PUT", path, params, header)
}
// PutFormString
//
// 某些api对参数顺序有要求, 只能自行构建body字符串
func (c *Client) PutFormString(path string, body string, header http.Header) ([]byte, error) {
return c.sendFormString("PUT", path, body, header)
}
func (c *Client) PutJson(path string, params map[string]interface{}, header http.Header) ([]byte, error) {
return c.sendJsonRequest("PUT", path, params, header)
}
func (c *Client) DeleteForm(path string, params map[string]interface{}, header http.Header) ([]byte, error) {
return c.sendFormRequest("DELETE", path, params, header)
}
// DeleteFormString
//
// 某些api对参数顺序有要求, 只能自行构建body字符串
func (c *Client) DeleteFormString(path string, body string, header http.Header) ([]byte, error) {
return c.sendFormString("DELETE", path, body, header)
}
func (c *Client) DeleteJson(path string, params map[string]interface{}, header http.Header) ([]byte, error) {
return c.sendJsonRequest("DELETE", path, params, header)
}
func (c *Client) sendHttp(verb, u string, header http.Header, body io.Reader) ([]byte, error) {
req, err := http.NewRequest(verb, u, body)
if err != nil {
return nil, err
}
if header != nil {
req.Header = header
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return respBody, fmt.Errorf(resp.Status)
}
return respBody, nil
}
func (c *Client) sendFormRequest(verb, path string, params map[string]interface{}, header http.Header) ([]byte, error) {
values := MapToUrlValues(params)
data := values.Encode()
return c.sendFormString(verb, path, data, header)
}
func (c *Client) sendJsonRequest(verb, path string, params map[string]interface{}, header http.Header) ([]byte, error) {
data, _ := json.Marshal(params)
body := strings.NewReader(string(data))
u := c.BuildRequestUrl(path, nil)
var h http.Header
if header != nil {
h = header
} else {
h = http.Header{}
}
if body.Len() > 0 {
h.Set("Content-Type", "application/json")
h.Set("Content-Length", fmt.Sprintf("%d", body.Len()))
}
return c.sendHttp(verb, u.String(), h, body)
}
func (c *Client) sendFormString(verb, path string, data string, header http.Header) ([]byte, error) {
body := strings.NewReader(data)
u := c.BuildRequestUrl(path, nil)
var h http.Header
if header != nil {
h = header
} else {
h = http.Header{}
}
if body.Len() > 0 {
h.Set("Content-Type", "application/x-www-form-urlencoded")
h.Set("Content-Length", fmt.Sprintf("%d", body.Len()))
}
return c.sendHttp(verb, u.String(), h, body)
}