-
Notifications
You must be signed in to change notification settings - Fork 127
/
client.go
52 lines (46 loc) · 990 Bytes
/
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
package rpc
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
func callMixinRPC(node, method string, params []any) ([]byte, error) {
client := &http.Client{Timeout: 20 * time.Second}
body, err := json.Marshal(map[string]any{
"method": method,
"params": params,
})
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", node, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Close = true
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result struct {
Data any `json:"data"`
Error any `json:"error"`
}
dec := json.NewDecoder(resp.Body)
dec.UseNumber()
err = dec.Decode(&result)
if err != nil {
return nil, err
}
if result.Error != nil {
return nil, fmt.Errorf("callMixinRPC(%s, %s, %s) => %v", node, method, params, result.Error)
}
if result.Data == nil {
return nil, nil
}
return json.Marshal(result.Data)
}