-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolysdk.go
55 lines (48 loc) · 1.18 KB
/
polysdk.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
package polysdk
import (
"net"
"net/http"
"time"
"github.com/quanxiang-cloud/go-polysdk/internal/signature"
"github.com/quanxiang-cloud/go-polysdk/config"
)
const (
httpTimeout = 10
httpMaxIdleConns = 3
)
// NewPolyClient create a ploy client from config file
func NewPolyClient(cfg *config.PolyConfig) (*PolyClient, error) {
sign, err := signature.NewSignerFromKey(cfg.Key.SecretKey)
if err != nil {
return nil, err
}
r := &PolyClient{
remoteURL: cfg.RemoteURL,
accessKeyID: cfg.Key.AccessKeyID,
sign: sign,
httpClient: http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(time.Second * httpTimeout)
c, err := net.DialTimeout(netw, addr, time.Second*httpTimeout)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
return c, nil
},
MaxIdleConns: httpMaxIdleConns,
DisableKeepAlives: false,
},
},
}
return r, nil
}
// PolyClient is a client for polyapi
type PolyClient struct {
timeAdjust int64 // adjust time clock with server
remoteURL string
accessKeyID string
sign signature.Signer
httpClient http.Client
}