-
Notifications
You must be signed in to change notification settings - Fork 13
/
multichain.go
65 lines (50 loc) · 1.31 KB
/
multichain.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
package multichain
import (
"fmt"
"errors"
)
const (
CONST_ID = "multichain-client"
)
type Response map[string]interface{}
func (r Response) Result() interface{} {
return r["result"]
}
func (client *Client) IsDebugMode() bool {
return client.debug
}
func (client *Client) DebugMode() *Client {
client.debug = true
return client
}
func (client *Client) msg(params []interface{}) map[string]interface{} {
return map[string]interface{}{
"jsonrpc": "1.0",
"id": CONST_ID,
"params": params,
}
}
func (client *Client) Command(method string, params []interface{}) map[string]interface{} {
msg := client.msg(params)
msg["method"] = fmt.Sprintf("%s", method)
return msg
}
func (client *Client) Post(msg interface{}) (Response, error) {
fmt.Println("POSTING: "+client.host, msg)
obj := make(Response)
if _, err := client.Resty.R().SetBody(msg).SetResult(&obj).SetBasicAuth(client.username, client.password).Post(client.host); err != nil {
return nil, err
}
if obj == nil || obj["error"] != nil {
e := obj["error"].(map[string]interface{})
var s string
m, ok := msg.(map[string]interface{})
if ok {
s = fmt.Sprintf("multichaind - '%s': %s", m["method"], e["message"].(string))
} else {
s = fmt.Sprintf("multichaind - %s", e["message"].(string))
}
return nil, errors.New(s)
}
return obj, nil
}