/
block.go
50 lines (39 loc) · 1.06 KB
/
block.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
package main
import (
"fmt"
"net/http"
"github.com/vechain/thor/api/blocks"
"github.com/vechain/thor/api/utils"
"github.com/vechain/thor/thor"
)
func getBlockByNumber(url string, n uint64) (*blocks.JSONCollapsedBlock, error) {
resp, err := getHttpResp("GET", fmt.Sprintf("%s%d", trim(url)+"/blocks/", n), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return getBlock(resp)
}
func getBlockById(url string, id thor.Bytes32) (*blocks.JSONCollapsedBlock, error) {
resp, err := getHttpResp("GET", trim(url)+"/blocks/"+id.String(), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return getBlock(resp)
}
func getBestBlock(url string) (*blocks.JSONCollapsedBlock, error) {
resp, err := getHttpResp("GET", trim(url)+"/blocks/best", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return getBlock(resp)
}
func getBlock(resp *http.Response) (*blocks.JSONCollapsedBlock, error) {
blk := new(blocks.JSONCollapsedBlock)
if err := utils.ParseJSON(resp.Body, &blk); err != nil {
return nil, err
}
return blk, nil
}