forked from VIZ-Blockchain/viz-go-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
77 lines (62 loc) · 1.69 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
76
77
package viz
import (
"errors"
"net/url"
"github.com/VIZ-Blockchain/viz-go-lib/api"
"github.com/VIZ-Blockchain/viz-go-lib/transports"
"github.com/VIZ-Blockchain/viz-go-lib/transports/http"
"github.com/VIZ-Blockchain/viz-go-lib/transports/websocket"
)
var (
ErrInitializeTransport = errors.New("Failed to initialize transport.")
)
// Client can be used to access GOLOS remote APIs.
// There is a function for every available GOLOS API,
// for example, Client.API.GetDatabaseInfo() corresponds to database_api -> get_database_info.
type Client struct {
cc transports.CallCloser
asyncProtocol bool
API *api.API
Config api.Config
// Current keys for operations
CurrentKeys *Keys
}
// NewClient creates a new RPC client that use the given CallCloser internally.
// Initialize only server present API. Absent API initialized as nil value.
func NewClient(apiURL string) (*Client, error) {
// Parse URL
u, err := url.Parse(apiURL)
if err != nil {
return nil, err
}
// Initializing Transport
var call transports.CallCloser
switch u.Scheme {
case "wss", "ws":
call, err = websocket.NewTransport(apiURL)
if err != nil {
return nil, err
}
case "https", "http":
call, err = http.NewTransport(apiURL)
if err != nil {
return nil, err
}
default:
return nil, ErrInitializeTransport
}
client := &Client{cc: call}
client.asyncProtocol = false
client.API = api.NewAPI(client.cc)
conf, err := client.API.GetConfig()
if err != nil {
return nil, err
}
client.Config = *conf
return client, nil
}
// Close should be used to close the client when no longer needed.
// It simply calls Close() on the underlying CallCloser.
func (client *Client) Close() error {
return client.cc.Close()
}