forked from hbdmapi/huobi_futures_Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
private_url_builder.go
59 lines (45 loc) · 1.29 KB
/
private_url_builder.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
package reqbuilder
import (
"fmt"
"net/url"
"time"
)
type PrivateUrlBuilder struct {
host string
akKey string
akValue string
smKey string
smValue string
svKey string
svValue string
tKey string
signer *Signer
}
func (p *PrivateUrlBuilder) Init(accessKey string, secretKey string, host string) *PrivateUrlBuilder {
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.signer = new(Signer).Init(secretKey)
return p
}
func (p *PrivateUrlBuilder) Build(method string, path string, request *GetRequest) string {
time := time.Now().UTC()
return p.BuildWithTime(method, path, time, request)
}
func (p *PrivateUrlBuilder) BuildWithTime(method string, path string, utcDate time.Time, request *GetRequest) string {
time := utcDate.Format("2006-01-02T15:04:05")
req := new(GetRequest).InitFrom(request)
req.AddParam(p.akKey, p.akValue)
req.AddParam(p.smKey, p.smValue)
req.AddParam(p.svKey, p.svValue)
req.AddParam(p.tKey, time)
parameters := req.BuildParams()
signature := p.signer.Sign(method, p.host, path, parameters)
url := fmt.Sprintf("https://%s%s?%s&Signature=%s", p.host, path, parameters, url.QueryEscape(signature))
return url
}