forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
204 lines (186 loc) · 4.49 KB
/
http_client.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
197
198
199
200
201
202
203
204
package gopay
import (
"crypto/tls"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"sync"
)
const (
POST = "POST"
GET = "GET"
TypeJSON = "json"
TypeXML = "xml"
TypeUrlencoded = "urlencoded"
TypeForm = "form"
TypeFormData = "form-data"
)
var Types = map[string]string{
TypeJSON: "application/json",
TypeXML: "application/xml",
TypeForm: "application/x-www-form-urlencoded",
TypeFormData: "application/x-www-form-urlencoded",
TypeUrlencoded: "application/x-www-form-urlencoded",
}
type Client struct {
HttpClient *http.Client
Transport *http.Transport
Url string
Method string
RequestType string
FormString string
ContentType string
UnmarshalType string
Types map[string]string
JsonByte []byte
Errors []error
mu sync.RWMutex
}
// NewHttpClient , default tls.Config{InsecureSkipVerify: true}
func NewHttpClient() (client *Client) {
c := new(Client)
c.HttpClient = &http.Client{}
c.Transport = &http.Transport{}
c.Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
c.Transport.DisableKeepAlives = true
c.RequestType = TypeUrlencoded
c.UnmarshalType = TypeJSON
c.Errors = make([]error, 0)
return c
}
func (c *Client) SetTLSConfig(tlsCfg *tls.Config) (client *Client) {
c.mu.Lock()
c.Transport.TLSClientConfig = tlsCfg
c.mu.Unlock()
return c
}
func (c *Client) Post(url string) (client *Client) {
c.mu.Lock()
c.Method = POST
c.Url = url
c.mu.Unlock()
return c
}
func (c *Client) Type(typeStr string) (client *Client) {
if _, ok := Types[typeStr]; ok {
c.mu.Lock()
c.RequestType = typeStr
c.mu.Unlock()
} else {
c.Errors = append(c.Errors, errors.New("Type func: incorrect type \""+typeStr+"\""))
}
return c
}
func (c *Client) Get(url string) (client *Client) {
c.mu.Lock()
c.Method = GET
c.Url = url
c.mu.Unlock()
return c
}
func (c *Client) SendStruct(v interface{}) (client *Client) {
bs, err := json.Marshal(v)
if err != nil {
c.Errors = append(c.Errors, err)
return c
}
c.mu.Lock()
c.JsonByte = bs
c.mu.Unlock()
return c
}
func (c *Client) SendString(v string) (client *Client) {
c.mu.Lock()
c.FormString = v
c.mu.Unlock()
return c
}
func (c *Client) EndStruct(v interface{}) (res *http.Response, errs []error) {
res, bs, errs := c.EndBytes()
if errs != nil && len(errs) > 0 {
c.Errors = append(c.Errors, errs...)
return nil, c.Errors
}
c.mu.RLock()
defer c.mu.RUnlock()
switch c.UnmarshalType {
case TypeJSON:
err := json.Unmarshal(bs, &v)
if err != nil {
c.Errors = append(c.Errors, fmt.Errorf("json.Unmarshal(%s):%w", string(bs), err))
return nil, c.Errors
}
return res, nil
case TypeXML:
err := xml.Unmarshal(bs, &v)
if err != nil {
c.Errors = append(c.Errors, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err))
return nil, c.Errors
}
return res, nil
default:
c.Errors = append(c.Errors, errors.New("UnmarshalType Type Wrong"))
return nil, c.Errors
}
}
func (c *Client) EndBytes() (res *http.Response, bs []byte, errs []error) {
if len(c.Errors) > 0 {
return nil, nil, c.Errors
}
var reader = strings.NewReader(NULL)
req, err := func() (*http.Request, error) {
c.mu.RLock()
defer c.mu.RUnlock()
switch c.Method {
case GET:
//todo: nothing
case POST:
switch c.RequestType {
case TypeJSON:
if c.JsonByte != nil {
reader = strings.NewReader(string(c.JsonByte))
}
c.ContentType = Types[TypeJSON]
case TypeForm, TypeFormData, TypeUrlencoded:
reader = strings.NewReader(c.FormString)
c.ContentType = Types[TypeForm]
case TypeXML:
reader = strings.NewReader(c.FormString)
c.ContentType = Types[TypeXML]
c.UnmarshalType = TypeXML
default:
return nil, errors.New("Request type Error ")
}
default:
return nil, errors.New("Only support Get and Post ")
}
req, err := http.NewRequest(c.Method, c.Url, reader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", c.ContentType)
c.HttpClient.Transport = c.Transport
return req, nil
}()
if err != nil {
c.Errors = append(c.Errors, err)
return nil, nil, c.Errors
}
res, err = c.HttpClient.Do(req)
if err != nil {
c.Errors = append(c.Errors, err)
return nil, nil, c.Errors
}
defer res.Body.Close()
bs, err = ioutil.ReadAll(io.LimitReader(res.Body, int64(2<<20))) // default 2MB change the size you want
if err != nil {
c.Errors = append(c.Errors, err)
return nil, nil, c.Errors
}
return res, bs, nil
}