/
client.go
71 lines (59 loc) · 1.49 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
package navexplorer
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
type ExplorerClient struct {
host string
network string
httpClient *http.Client
}
type Paginator struct {
CurrentPage int `json:"currentPage"`
First bool `json:"first"`
Last bool `json:"last"`
Total int64 `json:"total"`
Size int `json:"size"`
Pages int `json:"total_pages"`
Elements int `json:"number_of_elements"`
}
func NewClient(host string, network string) (c *ExplorerClient, err error) {
if len(host) == 0 {
err = errors.New("bad call missing argument host")
return
}
c = &ExplorerClient{host: host, network: network, httpClient: &http.Client{}}
return
}
func (c *ExplorerClient) call(method string) (response []byte, paginator Paginator, err error) {
req, err := http.NewRequest("GET", c.host+method, nil)
if err != nil {
return
}
req.Header.Add("Content-Type", "application/json;charset=utf-8")
req.Header.Add("Accept", "application/json")
req.Header.Add("Network", string(c.network))
resp, err := c.httpClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if resp.StatusCode != 200 {
err = errors.New("HTTP error: " + resp.Status)
return
}
paginationHeader := resp.Header.Get("X-Pagination")
if paginationHeader != "" {
err = json.Unmarshal([]byte(paginationHeader), &paginator)
if err != nil {
return
}
}
return data, paginator, nil
}