forked from totothink/antchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
75 lines (62 loc) · 1.54 KB
/
client.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
package antchain
import (
"fmt"
"net"
"net/http"
"time"
"github.com/spacemonkeygo/openssl"
)
type Config struct {
Endpoint string // 请求地址
TenantID string // 租户ID
BIZID string // 链ID
AccessID string // 访问ID
AccessKey string // 访问密钥
Account string // 链账户
MykmsKeyID string // 托管标识
ChainCallForBiz string // 商业接口调用路径
ChainCall string // 其他接口调用路径
}
type Client struct {
Config
Token string // 访问token
TokenExpiresAt int64 // 令牌过期时间
httpClient *http.Client
AppPrivateKey openssl.PrivateKey
}
// Client 发送请求使用的客户端
func NewClient(config Config) (client Client, err error) {
if config.Endpoint == "" {
config.Endpoint = ENDPOINT
}
if config.BIZID == "" {
config.BIZID = BIZID
}
if config.ChainCallForBiz == "" {
config.ChainCallForBiz = CHAIN_CALL_FOR_BIZ
}
if config.ChainCall == "" {
config.ChainCall = CHAIN_CALL
}
appPrivateKey, err := openssl.LoadPrivateKeyFromPEM([]byte(config.AccessKey))
if err != nil {
fmt.Print(err)
}
httpClient := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
IdleConnTimeout: 3 * time.Minute,
TLSHandshakeTimeout: 10 * time.Second,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 10 * time.Minute,
DualStack: true,
}).DialContext,
},
}
return Client{
Config: config,
httpClient: httpClient,
AppPrivateKey: appPrivateKey,
}, nil
}