-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.go
59 lines (48 loc) · 1.28 KB
/
util.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 util
import (
"context"
jww "github.com/spf13/jwalterweatherman"
"github.com/bytom/vapor/api"
"github.com/bytom/vapor/blockchain/rpc"
"github.com/bytom/vapor/env"
)
const (
// Success indicates the rpc calling is successful.
Success = iota
// ErrLocalExe indicates error occurs before the rpc calling.
ErrLocalExe
// ErrConnect indicates error occurs connecting to the vapord, e.g.,
// vapord can't parse the received arguments.
ErrConnect
// ErrLocalParse indicates error occurs locally when parsing the response.
ErrLocalParse
// ErrRemote indicates error occurs in vapord.
ErrRemote
)
var (
coreURL = env.String("BYTOM_URL", "http://127.0.0.1:9889")
)
// Wraper rpc's client
func MustRPCClient() *rpc.Client {
env.Parse()
return &rpc.Client{BaseURL: *coreURL}
}
// Wrapper rpc call api.
func ClientCall(path string, req ...interface{}) (interface{}, int) {
var response = &api.Response{}
var request interface{}
if req != nil {
request = req[0]
}
client := MustRPCClient()
client.Call(context.Background(), path, request, response)
switch response.Status {
case api.FAIL:
jww.ERROR.Println(response.Msg)
return nil, ErrRemote
case "":
jww.ERROR.Println("Unable to connect to the vapord")
return nil, ErrConnect
}
return response.Data, Success
}