-
Notifications
You must be signed in to change notification settings - Fork 0
/
fireblocks_sdk.go
106 lines (85 loc) · 2.18 KB
/
fireblocks_sdk.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package fireblocksdk
import (
"encoding/json"
"net/http"
"time"
"github.com/pkg/errors"
)
type PostRequestOption struct {
idempotencyKey string
}
type GetRequestOption struct {
paged bool
}
func WithIdempotencyKey(idempotencyKey string) func(*PostRequestOption) {
return func(option *PostRequestOption) {
option.idempotencyKey = idempotencyKey
}
}
func WithPaged(paged bool) func(*GetRequestOption) {
return func(option *GetRequestOption) {
option.paged = paged
}
}
type SDKOptions struct {
HTTPTimeoutMilliseconds time.Duration
tokenExpirySeconds int64
auth IAuthProvider
client IAPIClient
}
type FireblocksSDK struct {
baseURL string
client IAPIClient
auth IAuthProvider
}
func WithAuthProvider(auth IAuthProvider) func(o *SDKOptions) {
return func(o *SDKOptions) {
o.auth = auth
}
}
func WithAPIClient(client IAPIClient) func(o *SDKOptions) {
return func(o *SDKOptions) {
o.client = client
}
}
func WithHTTPTimout(timeout time.Duration) func(o *SDKOptions) {
return func(o *SDKOptions) {
o.HTTPTimeoutMilliseconds = timeout
}
}
func WithTokenTimeout(exp int64) func(o *SDKOptions) {
return func(o *SDKOptions) {
o.tokenExpirySeconds = exp
}
}
func CreateSDK(apikey string, privateKey []byte, baseURL string, opts ...func(o *SDKOptions)) (*FireblocksSDK, error) {
opt := &SDKOptions{tokenExpirySeconds: DefaultTokenExpiry()}
for _, o := range opts {
o(opt)
}
if opt.auth == nil {
provider, err := NewAuthProvider(apikey, privateKey, WithTokenExpiry(opt.tokenExpirySeconds))
if err != nil {
return nil, err
}
opt.auth = provider
}
if opt.client == nil {
opt.client = NewAPIClient(opt.auth, baseURL)
}
sdk := &FireblocksSDK{
baseURL: baseURL,
client: opt.client,
auth: opt.auth,
}
return sdk, nil
}
// GetSupportedAssets Gets all assets that are currently supported by Fireblocks
func (sdk *FireblocksSDK) GetSupportedAssets() (resp []*AssetTypeResponse, err error) {
body, status, err := sdk.client.DoGetRequest("/supported_assets", nil)
if err == nil && status == http.StatusOK {
err = json.Unmarshal(body, &resp)
return
}
return resp, errors.Wrap(err, "failed to make request")
}