-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
174 lines (150 loc) · 4.19 KB
/
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
package authorize_net
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
)
const (
SandboxEndpoint = "https://apitest.authorize.net/xml/v1/request.api"
Endpoint = "https://api.authorize.net/xml/v1/request.api"
DefaultTimeOut = 30 * time.Second
DefaultKeepAlive = 30 * time.Second
DefaultMaxIdleConns = 100
DefaultMaxIdleConnsPerHost = 100
DefaultIdleConnTimeout = 90 * time.Second
DefaultTLSHandshakeTimeout = 10 * time.Second
DefaultExpectContinueTimeout = 1 * time.Second
DefaultMaxConnsPerHost = 50
ContentType = "Content-Type"
Accept = "Accept"
ApplicationJSON = "application/json; charset=utf-8"
UserAgent = "User-Agent"
Authorization = "Authorization"
Bearer = "Bearer "
)
type Logger interface {
Printf(format string, v ...interface{})
}
type HttpClientCfg struct {
BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
HTTPClient *http.Client
}
type Client struct {
cfg *HttpClientCfg
log Logger
}
func NewAPIClient(cfg *HttpClientCfg, logger Logger) *Client {
if cfg == nil {
cfg = &HttpClientCfg{}
}
if cfg.HTTPClient == nil {
// build a "default" http client
cfg.HTTPClient = &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: DefaultMaxIdleConnsPerHost,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: DefaultTimeOut,
KeepAlive: DefaultKeepAlive,
}).DialContext,
MaxIdleConns: DefaultMaxIdleConns,
IdleConnTimeout: DefaultIdleConnTimeout,
TLSHandshakeTimeout: DefaultTLSHandshakeTimeout,
ExpectContinueTimeout: DefaultExpectContinueTimeout,
MaxConnsPerHost: DefaultMaxConnsPerHost,
},
}
}
result := Client{
cfg: cfg,
log: logger,
}
return &result
}
// prepareRequest build the request
func (c Client) prepareRequest(
ctx context.Context,
path string, method string,
postBody interface{}) (*http.Request, error) {
var (
body *bytes.Buffer
err error
)
// Detect postBody type and post.
if postBody != nil {
body = &bytes.Buffer{}
if err = json.NewEncoder(body).Encode(postBody); err != nil {
return nil, err
}
}
// Setup path and query parameters
uri, err := url.Parse(path)
if err != nil {
return nil, err
}
// Generate a new request
var request *http.Request
if body != nil {
if request, err = http.NewRequest(method, uri.String(), body); err != nil {
return nil, err
}
} else {
if request, err = http.NewRequest(method, uri.String(), nil); err != nil {
return nil, err
}
}
// set Content-Type header
request.Header.Set(ContentType, ApplicationJSON)
// set Accept header
request.Header.Set(Accept, ApplicationJSON)
// Override request host, if applicable
if c.cfg.Host != "" {
request.Host = c.cfg.Host
}
// Add the user agent to the request.
request.Header.Add(UserAgent, c.cfg.UserAgent)
// yeah, context should never be nil, but we never know
if ctx != nil {
// add context to the request, for cancellation
request = request.WithContext(ctx)
}
// add rest of the "default" headers (if any)
for header, value := range c.cfg.DefaultHeader {
request.Header.Add(header, value)
}
return request, nil
}
// do do the request.
func (c Client) do(request *http.Request, result interface{}, printRaw bool) error {
response, err := c.cfg.HTTPClient.Do(request)
if err != nil {
c.log.Printf("Error : %v\n", err)
return err
}
body, err := ioutil.ReadAll(response.Body)
_ = response.Body.Close()
if response.StatusCode != http.StatusOK {
}
if printRaw {
fmt.Println("---")
fmt.Println(string(body))
fmt.Println("---")
}
// The BOM identifies that the text is UTF-8 encoded, but it should be removed before decoding.
body = bytes.TrimPrefix(body, []byte{239, 187, 191})
if err := json.Unmarshal(body, &result); err != nil {
c.log.Printf("Error : %v\nresponse : %q", err, string(body))
return err
}
return nil
}