-
Notifications
You must be signed in to change notification settings - Fork 27
/
httpclient.go
233 lines (207 loc) · 7.25 KB
/
httpclient.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package midtrans
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
type HttpClient interface {
Call(method string, url string, apiKey *string, options *ConfigOptions, body io.Reader, result interface{}) *Error
}
// HttpClientImplementation : this is for midtrans HttpClient Implementation
type HttpClientImplementation struct {
HttpClient *http.Client
Logger LoggerInterface
}
// Call the Midtrans API at specific `path` using the specified HTTP `method`. The result will be
// given to `result` if there is no error. If any error occurred, the return of this function is the `midtrans.Error`
// itself, otherwise nil.
func (c *HttpClientImplementation) Call(method string, url string, apiKey *string, options *ConfigOptions, body io.Reader, result interface{}) *Error {
// NewRequest is used by Call to generate an http.Request.
req, err := http.NewRequest(method, url, body)
if err != nil {
c.Logger.Error("Cannot create Midtrans request: %v", err)
return &Error{
Message: fmt.Sprintf("Error Request creation failed: %s", err.Error()),
RawError: err,
}
}
if options != nil {
if options.Ctx != nil {
req.WithContext(options.Ctx)
}
if options.IrisIdempotencyKey != nil {
req.Header.Add("X-Idempotency-Key", *options.IrisIdempotencyKey)
}
if options.PaymentIdempotencyKey != nil {
req.Header.Add("Idempotency-Key", *options.PaymentIdempotencyKey)
}
if options.PaymentOverrideNotification != nil {
req.Header.Add("X-Override-Notification", *options.PaymentOverrideNotification)
}
if options.PaymentAppendNotification != nil {
req.Header.Add("X-Append-Notification", *options.PaymentAppendNotification)
}
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", "Midtrans-Go_"+libraryVersion)
if apiKey != nil {
key := *apiKey
if key == "" {
err := &Error{
Message: "The API Key (ServerKey/IrisApiKey) is invalid, as it is an empty string. Please double-check your API key. " +
"You can check from the Midtrans Dashboard. " +
"See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys " +
"for the details or please contact us via https://midtrans.com/contact-us. ",
}
c.Logger.Error("Authentication: ", err.GetMessage())
return err
} else if strings.Contains(key, " ") {
err := &Error{
Message: "The API Key (ServerKey/IrisApiKey) contains white-space. Please double-check your API key. " +
"You can check the ServerKey from the Midtrans Dashboard. " +
"See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys " +
"for the details or please contact us via https://midtrans.com/contact-us. ",
}
c.Logger.Error("Authentication: ", err.GetMessage())
return err
} else {
req.SetBasicAuth(key, "")
}
}
c.Logger.Info("================ Request ================")
c.Logger.Info("%v Request %v %v", req.Method, req.URL, req.Proto)
logHttpHeaders(c.Logger, req.Header, true)
return c.DoRequest(req, result)
}
// DoRequest : is used by Call to execute an API request using HTTP client and parse the response into `result`.
func (c *HttpClientImplementation) DoRequest(req *http.Request, result interface{}) *Error {
start := time.Now()
res, err := c.HttpClient.Do(req)
if err != nil {
c.Logger.Error("Cannot send request: %v", err.Error())
var statusCode int
if res != nil {
statusCode = res.StatusCode
} else if strings.Contains(err.Error(), "timeout") {
statusCode = 408
} else {
statusCode = 0
}
return &Error{
Message: fmt.Sprintf("Error when request via HttpClient, Cannot send request with error: %s", err.Error()),
StatusCode: statusCode,
RawError: err,
}
}
if res != nil {
defer res.Body.Close()
c.Logger.Info("================== END ==================")
c.Logger.Info("Request completed in %v ", time.Since(start))
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
c.Logger.Error("Request failed: %v", err)
return &Error{
Message: "Cannot read response body: " + err.Error(),
StatusCode: res.StatusCode,
RawError: err,
}
}
rawResponse := newHTTPResponse(res, resBody)
c.Logger.Debug("=============== Response ===============")
// Loop through headers to perform log
logHttpHeaders(c.Logger, rawResponse.Header, false)
c.Logger.Debug("Response Body: %v", string(rawResponse.RawBody))
if result != nil {
if err = json.Unmarshal(resBody, &result); err != nil {
return &Error{
Message: fmt.Sprintf("Invalid body response, parse error during API request to Midtrans with message: %s", err.Error()),
StatusCode: res.StatusCode,
RawError: err,
RawApiResponse: rawResponse,
}
}
}
// Check status_code from Midtrans response body
if found, data := HasOwnProperty("status_code", resBody); found {
statusCode, _ := strconv.Atoi(data["status_code"].(string))
if statusCode >= 401 && statusCode != 407 {
errMessage := fmt.Sprintf("Midtrans API is returning API error. HTTP status code: %s API response: %s", strconv.Itoa(statusCode), string(resBody))
return &Error{
Message: errMessage,
StatusCode: statusCode,
RawError: errors.New(errMessage),
RawApiResponse: rawResponse,
}
}
}
// Check StatusCode from Midtrans HTTP response api StatusCode
if res.StatusCode >= 400 {
errMessage := fmt.Sprintf("Midtrans API is returning API error. HTTP status code: %s API response: %s", strconv.Itoa(res.StatusCode), string(resBody))
return &Error{
Message: errMessage,
StatusCode: res.StatusCode,
RawError: errors.New(errMessage),
RawApiResponse: rawResponse,
}
}
}
return nil
}
// ApiResponse : is a structs that may come from Midtrans API endpoints
type ApiResponse struct {
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
// response Header contain a map of all HTTP header keys to values.
Header http.Header
// response body
RawBody []byte
// request that was sent to obtain the response
Request *http.Request
}
// newHTTPResponse : internal function to set HTTP Raw response return to ApiResponse
func newHTTPResponse(res *http.Response, responseBody []byte) *ApiResponse {
return &ApiResponse{
Status: res.Status,
StatusCode: res.StatusCode,
Proto: res.Proto,
Header: res.Header,
RawBody: responseBody,
Request: res.Request,
}
}
// logHttpHeaders : internal function to perform log from headers
func logHttpHeaders(log LoggerInterface, header http.Header, isReq bool) {
// Loop through headers to perform log
for name, headers := range header {
name = strings.ToLower(name)
for _, h := range headers {
if name == "authorization" {
log.Debug("%v: %v", name, h)
} else {
if isReq {
log.Info("%v: %v", name, h)
} else {
log.Debug("%v: %v", name, h)
}
}
}
}
}
//HasOwnProperty : Convert HTTP raw response body to map and check if the body has own field
func HasOwnProperty(key string, body []byte) (bool, map[string]interface{}) {
d := make(map[string]interface{})
_ = json.Unmarshal(body, &d)
if _, found := d[key].(string); found {
return found, d
} else {
return found, d
}
}