1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-06-18 05:00:03 +02:00
antelope-api-healthcheck/src/eosapi/functions.go
Henrik Hautakoski 3aba653038 Define eosapi.ReqParams struct that includes a optional Host parameter.
If left empty, Host parsed from eosapi.ReqParams.Url will be used (like before).
2020-06-23 12:56:47 +02:00

78 lines
1.5 KiB
Go

package eosapi;
import (
"strings"
"time"
"net/url"
"io/ioutil"
"github.com/imroc/req"
"github.com/liamylian/jsontime/v2"
)
type ReqParams struct {
Url string
Host string
}
var json = v2.ConfigWithCustomTimeFormat
func init() {
// EOS Api does not specify timezone in timestamps (they are always UTC tho).
v2.SetDefaultTimeFormat("2006-01-02T15:04:05", time.UTC)
}
func send(p ReqParams, method string, path string) (*req.Resp, error) {
host := p.Host
if len(host) < 1 {
u, err := url.Parse(p.Url)
if err != nil {
return nil, err
}
host = strings.Split(u.Host, ":")[0]
}
// Go's net.http (that `req` uses) sends the port in the host header.
// nodeos api does not like that, so we need to provide our
// own Host header with just the host.
headers := req.Header{
"Host": host,
}
r := req.New()
return r.Do(method, p.Url + path, headers)
}
// GetInfo - Fetches get_info from API
// ---------------------------------------------------------
func GetInfo(params ReqParams) (Info, error) {
var info Info
r, err := send(params, "GET", "/v1/chain/get_info")
if err == nil {
resp := r.Response()
body, _ := ioutil.ReadAll(resp.Body)
// Parse json
err = json.Unmarshal(body, &info)
}
return info, err
}
func GetHealth(params ReqParams) (Health, error) {
var health Health;
r, err := send(params, "GET", "/v2/health")
if err == nil {
resp := r.Response()
body, _ := ioutil.ReadAll(resp.Body)
// Parse json
err = json.Unmarshal(body, &health)
}
return health, err
}