forked from goat-systems/go-tezos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
114 lines (98 loc) · 2.82 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
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
107
108
109
110
111
112
113
114
package gotezos
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"time"
)
// TezosRPCClient is a struct to represent the client to reach a Tezos node
type TezosRPCClient struct {
Host string
Port string
logfunction func(level, msg string)
logger *log.Logger
isWebClient bool
}
// NewTezosRPCClient creates a new RPC client using the specified hostname and port.
// Also acceptable is the hostname of a web-endpoint that supports https.
func NewTezosRPCClient(hostname string, port string) *TezosRPCClient {
t := TezosRPCClient{}
// Strip off posible trailing '/'
hLen := len(hostname)
if hostname[hLen-1] == '/' {
hostname = hostname[:hLen-1]
}
// Strip off URI scheme
if hostname[:8] == "https://" {
hostname = hostname[8:]
t.isWebClient = true
} else if hostname[:7] == "http://" {
hostname = hostname[7:]
}
t.Host = hostname
t.Port = port
t.logfunction = func(level, msg string) {
fmt.Println(level + ": " + msg)
}
t.SetLogger(log.New(os.Stdout, hostname, 0))
return &t
}
// SetLogger set the logger for the RPC Client
func (gt *TezosRPCClient) SetLogger(log *log.Logger) {
gt.logger = log
}
// IsWebClient tells the TezosRPCClient calling it that it is a web client
func (gt *TezosRPCClient) IsWebClient(b bool) {
gt.isWebClient = b
}
// GetResponse gets the raw response using TezosRPCClient with the path and args to query
func (gt *TezosRPCClient) GetResponse(method string, path string, args string) (ResponseRaw, error) {
var url string
if gt.isWebClient {
url = fmt.Sprintf("https://%s:%s%s", gt.Host, gt.Port, path)
} else {
url = fmt.Sprintf("http://%s:%s%s", gt.Host, gt.Port, path)
}
var jsonStr = []byte(args)
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonStr))
if err != nil {
gt.logger.Println("Error in GetResponse: " + err.Error())
return ResponseRaw{}, err
}
var netTransport = &http.Transport{ // TODO make gt as config option, but with defaults like this
Dial: (&net.Dialer{
Timeout: 3 * time.Second,
}).Dial,
TLSHandshakeTimeout: 3 * time.Second,
}
var netClient = &http.Client{
Timeout: time.Second * 3,
Transport: netTransport,
}
resp, err := netClient.Do(req)
if err != nil {
gt.logger.Println("Error in GetResponse: " + err.Error())
return ResponseRaw{}, err
}
var b []byte
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
gt.logger.Println("Error in GetResponse - readAll bytes: " + err.Error())
return ResponseRaw{}, err
}
netTransport.CloseIdleConnections()
defer resp.Body.Close()
return ResponseRaw{b}, nil
}
// Healthcheck a function just to perform a query to see if an RPC Client's endpoint is alive (heartbeat)
func (gt *TezosRPCClient) Healthcheck() bool {
_, err := gt.GetResponse("GET", "/chains/main/blocks", "")
if err == nil {
return true // healthy
}
return false // unhelaty
}