-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
sendRequest.go
95 lines (82 loc) · 1.82 KB
/
sendRequest.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
package requests
import (
"bytes"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
func Decorate(req *http.Request) error {
req.Header.Set("tenant-id", TENANT_ID)
req.Header.Set("user-type", USER_TYPE)
req.Header.Set("app-content-encrypted", CONTENT_ENCRYPTED)
req.Header.Set("Authorization", PARAM_AUTHENTICATION)
return nil
}
func CreateRequest(
endpoint string,
httpMethod string,
request string,
contentType string,
token string,
eventId string,
) (*http.Request, error) {
appSendDate := time.Now().UnixMilli()
if len(request) != 0 {
request = EncryptRequest(
endpoint,
appSendDate,
TENANT_ID,
token,
request,
contentType)
}
bodyReader := bytes.NewReader([]byte(request))
req, err := http.NewRequest(httpMethod, endpoint, bodyReader)
if err != nil {
return nil, err
}
Decorate(req)
req.Header.Set("app-send-date", strconv.FormatInt(appSendDate, 10))
req.Header.Set("original-content-type", contentType)
if len(token) != 0 {
req.Header.Set("blade-auth", token)
}
if len(eventId) != 0 {
req.Header.Set("event-id", eventId)
}
replace := strings.Replace(endpoint, BASE_URL_P, "/", -1)
req.Header.Set("app-verification-string",
CalculateRequestVerification(
replace,
appSendDate,
TENANT_ID,
contentType,
request,
token))
return req, nil
}
func DecryptAnswer(resp *http.Response) ([]byte, error) {
var body string
var bodyRaw []byte
var err error
if resp.StatusCode == http.StatusOK {
bodyRaw, err = io.ReadAll(resp.Body)
if err != nil {
return bodyRaw, err
}
if resp.Header.Get("app-content-encrypted") == "1" {
body = DecryptResponse(
resp.Header.Get("app-send-date"),
resp.Header.Get("original-content-type"),
string(bodyRaw))
} else {
body = string(bodyRaw)
}
} else {
err = fmt.Errorf(resp.Status)
}
return []byte(body), err
}