forked from goat-systems/go-tezos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goTezosNode.go
84 lines (70 loc) · 1.77 KB
/
goTezosNode.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
package goTezos
import (
"bytes"
"fmt"
"net/http"
"net"
"time"
"io/ioutil"
"log"
"os"
)
type TezosRPCClient struct {
Host string
Port string
logfunction func(level, msg string)
logger *log.Logger
}
func NewTezosRPCClient(hostname string, port string) *TezosRPCClient{
t := TezosRPCClient{}
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
}
func (this *TezosRPCClient) SetLogger(log *log.Logger) {
this.logger = log
}
func (this *TezosRPCClient) GetResponse(method string, args string) (ResponseRaw, error) {
url := "http://" + this.Host + this.Port + "" + method
var jsonStr = []byte(args)
req, err := http.NewRequest("GET", url, bytes.NewBuffer(jsonStr))
if err != nil {
this.logger.Println("Error in GetResponse: "+ err.Error())
return ResponseRaw{}, err
}
var netTransport = &http.Transport{ // TODO make this 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 {
this.logger.Println("Error in GetResponse: "+ err.Error())
return ResponseRaw{}, err
}
var b []byte
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
this.logger.Println("Error in GetResponse - readAll bytes: "+ err.Error())
return ResponseRaw{}, err
}
netTransport.CloseIdleConnections()
defer resp.Body.Close()
return ResponseRaw{b}, nil
}
func (this *TezosRPCClient) Healthcheck() bool {
_, err := this.GetResponse("/chains/main/blocks", "")
if err == nil {
return true // healthy
}
return false // unhelaty
}