-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
156 lines (151 loc) · 3.9 KB
/
request.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
package satisgo
import (
"crypto/sha512"
"crypto/tls"
"encoding/base64"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"time"
"github.com/buger/jsonparser"
"github.com/fatih/color"
)
func (p *Satis) makeCall(req *http.Request) (int, []byte, error) {
var insecure *tls.Config
if p.env == dev {
insecure = &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: true,
}
} else {
insecure = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
insecure.BuildNameToCertificate()
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: insecure,
}
client := &http.Client{
Transport: tr,
Timeout: 3 * time.Second,
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", p.bearer))
if req.Method == http.MethodPost {
req.Header.Set("Idempotency-Key", generateUUID())
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := checkIntegrity(resp)
if err != nil {
return -1, nil, err
}
// this block needs to be taken out ------------------------
err = handleHeader(resp.StatusCode)
if err != nil {
//Parse body to find error code and message
code, e := jsonparser.GetInt(body, "code")
if e != nil {
return -1, nil, err
}
msg, e := jsonparser.GetString(body, "message")
if e != nil {
return -1, nil, err
}
return -1, nil, fmt.Errorf("%s --> CODE %d: %s", err.Error(), code, msg)
}
// till here -------------------------------------------------
return resp.StatusCode, body, nil
}
func checkIntegrity(r *http.Response) ([]byte, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
color.Yellow(string(body))
color.Blue(fmt.Sprint(r.Header))
//checkin content lenght
lenght, ok := r.Header["Content-Length"]
if ok != true {
return nil, fmt.Errorf("Content-Lenght value in header does not exist")
}
if len(lenght) != 1 {
return nil, fmt.Errorf("Multiple Content-Lenght values in header")
}
l, err := strconv.Atoi(lenght[0])
if err != nil {
return nil, err
}
if len(body) != l {
return nil, fmt.Errorf("Content-Lenght value in header is not true")
}
if r.StatusCode == 204 {
return []byte(""), nil
}
//checking content type
t, ok := r.Header["Content-Type"]
if ok != true {
return nil, fmt.Errorf("Content-Type value in header does not exist")
}
if len(t) != 1 {
return nil, fmt.Errorf("Multiple Content-Type values in header")
}
if t[0] != "application/json" && t[0] != "application/json;charset=utf-8" {
return nil, fmt.Errorf("Content-Type value in header is not correct")
}
//check digest
digest, ok := r.Header["Digest"]
if ok != true {
return nil, fmt.Errorf("Content-Type value in header does not exist")
}
if len(digest) != 1 {
return nil, fmt.Errorf("Multiple Content-Type values in header")
}
hash := sha512.New()
_, err = hash.Write(body)
if err != nil {
return nil, err
}
dig := "SHA-512=" + base64.StdEncoding.EncodeToString(hash.Sum(nil))
if digest[0] != dig {
return nil, fmt.Errorf("Digest is incorrect")
}
//check wlt
wlt, ok := r.Header["X-Satispay-Cid"]
if ok != true {
return nil, fmt.Errorf("X-Satispay-Cid value in header does not exist")
}
if len(wlt) != 1 {
return nil, fmt.Errorf("Multiple X-Satispay-Cid values in header")
}
w, err := jsonparser.GetString(body, "wlt")
if err != nil {
if err.Error() != "Key path not found" {
return nil, fmt.Errorf("Error Parsing WLT from body: %s", err.Error())
}
}
if err == nil {
if w != wlt[0] {
return nil, fmt.Errorf("WLT checks has gone wrong")
}
}
//Still NOT checking
// - SERVER header
// - DATE HEADER
return body, nil
}