forked from LuterGS/goBithumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_http.go
109 lines (85 loc) · 2.61 KB
/
request_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
package gobithumb
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
type httpRequester struct {
connectKey string
secretKey string
basicUrl string
publicClient *http.Client
privateClient *http.Client
}
func newHttpRequester(connectKey string, secretKey string) *httpRequester {
httpRequester := httpRequester{}
httpRequester.connectKey = connectKey
httpRequester.secretKey = secretKey
httpRequester.basicUrl = "https://api.bithumb.com"
httpRequester.publicClient = &http.Client{}
httpRequester.privateClient = &http.Client{}
return &httpRequester
}
func (h *httpRequester) requestPublic(order publicOrder, data string) ([]byte, error) {
request, err := http.NewRequest("GET", h.basicUrl+string(order)+"/"+data, nil)
if err != nil {
return nil, err
}
response, err := h.publicClient.Do(request)
if err != nil {
return nil, err
}
byteResponse, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return byteResponse, nil
}
func (h *httpRequester) requestPrivate(passVal map[string]string) []byte {
// request body 설정
requestBody := url.Values{}
for index, data := range passVal {
requestBody.Set(index, data)
}
requestBodyString := requestBody.Encode()
// nonce 및 api-sign 가져오기
nonce := fmt.Sprint(time.Now().UnixNano() / int64(time.Millisecond))
apiSignVal := h.encryptData(passVal["endpoint"], requestBodyString, nonce)
// request 객체 생성
request, err := http.NewRequest("POST", h.basicUrl+passVal["endpoint"], bytes.NewBufferString(requestBodyString))
if err != nil {
panic("Failed to create Request")
}
request.Header.Add("Api-Key", h.connectKey)
request.Header.Add("Api-Sign", apiSignVal)
request.Header.Add("Api-Nonce", nonce)
request.Header.Add("Content-type", "application/x-www-form-urlencoded")
request.Header.Add("Content-Length", strconv.Itoa(len(requestBodyString)))
response, err := h.privateClient.Do(request)
if err != nil {
panic("Failed to receive Data, check server stauts")
}
byteResponse, err := ioutil.ReadAll(response.Body)
if err != nil {
panic("Failed to receive Data")
}
return byteResponse
}
func (h *httpRequester) encryptData(endpoint string, body string, nonce string) string {
reqRawString := endpoint + string(0) + body + string(0) + nonce
hmacParsed := hmac.New(sha512.New, []byte(h.secretKey))
hmacParsed.Write([]byte(reqRawString))
hexData := hex.EncodeToString(hmacParsed.Sum(nil))
byteHexData := []byte(hexData)
hmacParsed.Reset()
result := base64.StdEncoding.EncodeToString(byteHexData)
return result
}