-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
148 lines (121 loc) · 3.48 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
package ovo
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
// Requester is http request interface.
type Requester interface {
Call(ctx context.Context, method, url, appID, key string, header http.Header, request interface{}, response interface{}) (statusCode int, err error)
}
type requester struct {
client *http.Client
logger Logger
}
func defaultRequester(client *http.Client, logger Logger) *requester {
return &requester{
client: client,
logger: logger,
}
}
// Call to prepare request and execute.
func (r *requester) Call(ctx context.Context, method, url, appID, key string, header http.Header, request interface{}, response interface{}) (int, error) {
loc, _ := time.LoadLocation("Asia/Jakarta")
now := time.Now().In(loc)
reqBody, err := json.Marshal(request)
if err != nil {
r.logger.Error(err.Error())
return http.StatusInternalServerError, ErrInternal
}
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(reqBody))
if err != nil {
r.logger.Error(err.Error())
return http.StatusInternalServerError, ErrInternal
}
if header != nil {
req.Header = header
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(fmt.Sprintf("%s%d", appID, now.Unix())))
sha := hex.EncodeToString(h.Sum(nil))
req.Header.Add("app-id", appID)
req.Header.Add("random", strconv.FormatInt(now.Unix(), 10))
req.Header.Add("hmac", sha)
r.logger.Debug("%s %s", method, url)
r.logRequestHeader(req.Header)
r.logRequestBody(reqBody)
defer func() { r.logger.Info("%s %s [%s]", method, url, time.Since(now)) }()
return r.doRequest(req, response)
}
func (r *requester) doRequest(req *http.Request, response interface{}) (int, error) {
resp, err := r.client.Do(req)
if err != nil {
r.logger.Error(err.Error())
return http.StatusInternalServerError, ErrInternal
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
r.logger.Error(err.Error())
return http.StatusInternalServerError, ErrInternal
}
r.logResponseBody(resp.StatusCode, respBody)
if resp.StatusCode != http.StatusOK {
var tx Transaction
if err := json.Unmarshal(respBody, &tx); err != nil {
r.logger.Error(err.Error())
return resp.StatusCode, errors.New(string(respBody))
}
return resp.StatusCode, ovoErr[tx.ResponseCode]
}
if err := json.Unmarshal(respBody, &response); err != nil {
r.logger.Error(err.Error())
return http.StatusInternalServerError, ErrInternal
}
return resp.StatusCode, nil
}
func (r *requester) logRequestHeader(header http.Header) {
if header == nil || len(header) == 0 {
return
}
for k, h := range header {
for _, v := range h {
r.logger.Debug("header: %s: %s", k, v)
}
}
}
func (r *requester) logRequestBody(request []byte) {
if request == nil {
return
}
var out bytes.Buffer
if err := json.Indent(&out, request, "", " "); err != nil {
r.logger.Error(err.Error())
r.logger.Debug("request: %s", string(request))
return
}
r.logger.Debug("request: %s", out.String())
}
func (r *requester) logResponseBody(code int, response []byte) {
if response == nil {
return
}
var out bytes.Buffer
if err := json.Indent(&out, response, "", " "); err != nil {
r.logger.Error(err.Error())
r.logger.Debug("response: %d %s", code, string(response))
return
}
r.logger.Debug("response: %d %s", code, out.String())
}