forked from HuobiRDCenter/huobi_Golang
/
websocketv1requestbuilder.go
65 lines (51 loc) · 1.4 KB
/
websocketv1requestbuilder.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
package requestbuilder
import (
"github.com/Turing-Chu/huobi_golang/internal/model"
model2 "github.com/Turing-Chu/huobi_golang/pkg/model"
"time"
)
type WebSocketV1RequestBuilder struct {
akKey string
akValue string
smKey string
smValue string
svKey string
svValue string
tKey string
tValue string
host string
path string
signer *Signer
}
func (p *WebSocketV1RequestBuilder) Init(accessKey string, secretKey string, host string, path string) *WebSocketV1RequestBuilder {
p.akKey = "AccessKeyId"
p.akValue = accessKey
p.smKey = "SignatureMethod"
p.smValue = "HmacSHA256"
p.svKey = "SignatureVersion"
p.svValue = "2"
p.tKey = "Timestamp"
p.host = host
p.path = path
p.signer = new(Signer).Init(secretKey)
return p
}
func (p *WebSocketV1RequestBuilder) Build() (string, error) {
time := time.Now().UTC()
return p.build(time)
}
func (p *WebSocketV1RequestBuilder) build(utcDate time.Time) (string, error) {
time := utcDate.Format("2006-01-02T15:04:05")
req := new(model2.GetRequest).Init()
req.AddParam(p.akKey, p.akValue)
req.AddParam(p.smKey, p.smValue)
req.AddParam(p.svKey, p.svValue)
req.AddParam(p.tKey, time)
signature := p.signer.Sign("GET", p.host, p.path, req.BuildParams())
auth := new(model.WebSocketV1AuthenticationRequest).Init()
auth.AccessKeyId = p.akValue
auth.Timestamp = time
auth.Signature = signature
result, err := model2.ToJson(auth)
return result, err
}