-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
81 lines (68 loc) · 1.76 KB
/
utils.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
package p2pb2b
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
/// A general utility for making unauthenticated api requests
func (clt *Client) APIRequest(method, endpoint string) ([]byte, error) {
ctx := clt.Ctx
var req *http.Request
var err error
if method == http.MethodGet {
req, err = http.NewRequest(method, clt.URL+endpoint, nil)
}
if method == http.MethodPost {
//run through all of the signing procedures
}
req.WithContext(ctx)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
fmt.Print(string(body))
return body, nil
}
/// A general utility for making authenticated API requests
/// Using HMACSha512 signing
func (clt *Client) AuthAPIRequest(postBody interface{}, method, endpoint string) ([]byte, error) {
ctx := clt.Ctx
var req *http.Request
var err error
APIKey := clt.APIKey
APISecret := clt.APISecret
byteJson, _ := json.Marshal(postBody)
payload := base64.StdEncoding.EncodeToString(byteJson)
signer := hmac.New(sha512.New, []byte(APISecret))
signer.Write([]byte(payload))
hexSig := hex.EncodeToString(signer.Sum(nil))
url := clt.URL + endpoint
req, _ = http.NewRequest(method, url, bytes.NewBuffer(byteJson))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-TXC-APIKEY", APIKey)
req.Header.Set("X-TXC-PAYLOAD", payload)
req.Header.Set("X-TXC-SIGNATURE", hexSig)
req.WithContext(ctx)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
fmt.Print(string(body))
return body, nil
}