forked from veritrans/go-midtrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
136 lines (111 loc) · 3.11 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
package midtrans
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"strconv"
"time"
)
// Client struct
type Client struct {
APIEnvType EnvironmentType
ClientKey string
ServerKey string
LogLevel int
Logger *log.Logger
}
// NewClient : this function will always be called when the library is in use
func NewClient() Client {
return Client{
APIEnvType: Sandbox,
// LogLevel is the logging level used by the Midtrans library
// 0: No logging
// 1: Errors only
// 2: Errors + informational (default)
// 3: Errors + informational + debug
LogLevel: 2,
Logger: log.New(os.Stderr, "", log.LstdFlags),
}
}
// ===================== HTTP CLIENT ================================================
var defHTTPTimeout = 80 * time.Second
var httpClient = &http.Client{Timeout: defHTTPTimeout}
// NewRequest : send new request
func (c *Client) NewRequest(method string, fullPath string, body io.Reader) (*http.Request, error) {
logLevel := c.LogLevel
logger := c.Logger
req, err := http.NewRequest(method, fullPath, body)
if err != nil {
if logLevel > 0 {
logger.Println("Request creation failed: ", err)
}
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(c.ServerKey, "")
return req, nil
}
// ExecuteRequest : execute request
func (c *Client) ExecuteRequest(req *http.Request, v interface{}) error {
logLevel := c.LogLevel
logger := c.Logger
if logLevel > 1 {
logger.Println("Request ", req.Method, ": ", req.URL.Host, req.URL.Path)
}
start := time.Now()
res, err := httpClient.Do(req)
if err != nil {
if logLevel > 0 {
logger.Println("Cannot send request: ", err)
}
return err
}
defer res.Body.Close()
if logLevel > 2 {
logger.Println("Completed in ", time.Since(start))
}
if err != nil {
if logLevel > 0 {
logger.Println("Request failed: ", err)
}
return err
}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
if logLevel > 0 {
logger.Println("Cannot read response body: ", err)
}
return err
}
if logLevel > 2 {
logger.Println("Midtrans response: ", string(resBody))
}
if v != nil {
if err = json.Unmarshal(resBody, v); err != nil {
return err
}
// we're safe to reflect status_code if response not an array
if reflect.ValueOf(v).Elem().Kind() != reflect.Slice {
if reflect.ValueOf(v).Elem().FieldByName("StatusCode").IsValid() && len(reflect.ValueOf(v).Elem().FieldByName("StatusCode").String()) == 0 {
reflect.ValueOf(v).Elem().FieldByName("StatusCode").SetString(strconv.Itoa(res.StatusCode))
}
}
}
return nil
}
// Call the Midtrans API at specific `path` using the specified HTTP `method`. The result will be
// given to `v` if there is no error. If any error occurred, the return of this function is the error
// itself, otherwise nil.
func (c *Client) Call(method, path string, body io.Reader, v interface{}) error {
req, err := c.NewRequest(method, path, body)
if err != nil {
return err
}
return c.ExecuteRequest(req, v)
}
// ===================== END HTTP CLIENT ================================================