-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
69 lines (56 loc) · 1.44 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
package intersdk
import (
"bytes"
"crypto/tls"
"net/http"
)
type customTransport struct {
transport http.RoundTripper
accountHeader string
}
func (c *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("x-conta-corrente", c.accountHeader)
return c.transport.RoundTrip(req)
}
func NewClient(cert, key string, accountNumber *string) (*http.Client, error) {
t, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return nil, err
}
transport := createTransport(t)
client := &http.Client{
Transport: createCustomTransport(transport, accountNumber),
}
return client, nil
}
func createTransport(cert tls.Certificate) *http.Transport {
return &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
}
func createCustomTransport(transport *http.Transport, accountNumber *string) http.RoundTripper {
if accountNumber == nil {
return &customTransport{
transport: transport,
}
}
return &customTransport{
transport: transport,
accountHeader: *accountNumber,
}
}
func sendRequest(client *http.Client, method, url, token string, body []byte) (*http.Response, error) {
req, err := http.NewRequest(method, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
res, err := client.Do(req)
if err != nil {
return nil, err
}
return res, nil
}