Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitgetLimited
GitHub Repository: BitgetLimited/V3-bitget-api-sdk
Path: blob/master/bitget-golang-sdk-api/internal/common/bitgetrestclient.go
735 views
1
package common
2
3
import (
4
"bitget/config"
5
"bitget/constants"
6
"bitget/internal"
7
"io/ioutil"
8
"net/http"
9
"strings"
10
"time"
11
)
12
13
type BitgetRestClient struct {
14
ApiKey string
15
ApiSecretKey string
16
Passphrase string
17
BaseUrl string
18
HttpClient http.Client
19
Signer *Signer
20
}
21
22
func (p *BitgetRestClient) Init() *BitgetRestClient {
23
p.ApiKey = config.ApiKey
24
p.ApiSecretKey = config.SecretKey
25
p.BaseUrl = config.BaseUrl
26
p.Passphrase = config.PASSPHRASE
27
p.Signer = new(Signer).Init(config.SecretKey)
28
p.HttpClient = http.Client{
29
Timeout: time.Duration(config.TimeoutSecond) * time.Second,
30
}
31
return p
32
}
33
34
func (p *BitgetRestClient) DoPost(uri string, params string) (string, error) {
35
timesStamp := internal.TimesStamp()
36
//body, _ := internal.BuildJsonParams(params)
37
38
sign := p.Signer.Sign(constants.POST, uri, params, timesStamp)
39
if constants.RSA == config.SignType {
40
sign = p.Signer.SignByRSA(constants.POST, uri, params, timesStamp)
41
}
42
requestUrl := config.BaseUrl + uri
43
44
buffer := strings.NewReader(params)
45
request, err := http.NewRequest(constants.POST, requestUrl, buffer)
46
47
internal.Headers(request, p.ApiKey, timesStamp, sign, p.Passphrase)
48
if err != nil {
49
return "", err
50
}
51
response, err := p.HttpClient.Do(request)
52
53
if err != nil {
54
return "", err
55
}
56
57
defer response.Body.Close()
58
59
bodyStr, err := ioutil.ReadAll(response.Body)
60
if err != nil {
61
return "", err
62
}
63
64
responseBodyString := string(bodyStr)
65
return responseBodyString, err
66
}
67
68
func (p *BitgetRestClient) DoGet(uri string, params map[string]string) (string, error) {
69
timesStamp := internal.TimesStamp()
70
body := internal.BuildGetParams(params)
71
//fmt.Println(body)
72
73
sign := p.Signer.Sign(constants.GET, uri, body, timesStamp)
74
75
requestUrl := p.BaseUrl + uri + body
76
77
request, err := http.NewRequest(constants.GET, requestUrl, nil)
78
if err != nil {
79
return "", err
80
}
81
internal.Headers(request, p.ApiKey, timesStamp, sign, p.Passphrase)
82
83
response, err := p.HttpClient.Do(request)
84
85
if err != nil {
86
return "", err
87
}
88
89
defer response.Body.Close()
90
91
bodyStr, err := ioutil.ReadAll(response.Body)
92
if err != nil {
93
return "", err
94
}
95
96
responseBodyString := string(bodyStr)
97
return responseBodyString, err
98
}
99
100