-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
125 lines (102 loc) · 2.65 KB
/
client.go
File metadata and controls
125 lines (102 loc) · 2.65 KB
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
115
116
117
118
119
120
121
122
123
124
125
// Package explorerclient provides a simple HTTP client for the Threefold explorer API
// This package simplifies and reduces the dependencies from the Threefold implementation (https://github.com/threefoldtech/tfexplorer/tree/master/client)
// This also means not all functionality is used and only the API endpoints needed for Bancadati are implemented.
package explorerclient
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"path/filepath"
"github.com/pkg/errors"
)
const (
// DefaultBaseURL represents the default API URL for the Threefold Explorer
DefaultBaseURL = "https://explorer.grid.tf/explorer/"
)
var (
successCodes = []int{
http.StatusOK,
http.StatusCreated,
}
)
// NewClient returns a new TF explorer client
func NewClient(baseURL string) (*Client, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, fmt.Errorf("%s is an invalid base URL: %w", baseURL, err)
}
cl := &Client{
baseURL: u,
http: &http.Client{},
}
return cl, nil
}
// Client represents a TF explorer client
type Client struct {
http *http.Client
baseURL *url.URL
}
func (c *Client) geturl(p ...string) string {
b := *c.baseURL
b.Path = path.Join(b.Path, filepath.Join(p...))
return b.String()
}
func (c *Client) get(u string, query url.Values, output interface{}, expect ...int) (*http.Response, error) {
if len(query) > 0 {
u = fmt.Sprintf("%s?%s", u, query.Encode())
}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("failed to create new HTTP request: %w", err)
}
response, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
return response, c.process(response, output, expect...)
}
func (c *Client) process(response *http.Response, output interface{}, expect ...int) error {
defer response.Body.Close()
if len(expect) == 0 {
expect = successCodes
}
in := func(i int, l []int) bool {
for _, x := range l {
if x == i {
return true
}
}
return false
}
dec := json.NewDecoder(response.Body)
if !in(response.StatusCode, expect) {
var output struct {
E string `json:"error"`
}
if err := dec.Decode(&output); err != nil {
return errors.Wrapf(HTTPError{
err: err,
resp: response,
}, "failed to load error while processing invalid return code of: %s", response.Status)
}
return HTTPError{
err: fmt.Errorf(output.E),
resp: response,
}
}
if output == nil {
//discard output
ioutil.ReadAll(response.Body)
return nil
}
if err := dec.Decode(output); err != nil {
return HTTPError{
err: errors.Wrap(err, "failed to load output"),
resp: response,
}
}
return nil
}