-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
62 lines (49 loc) · 1.17 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
package xpla
import (
"encoding/json"
"io"
"net/http"
"github.com/pkg/errors"
)
type Client interface {
VerifiedCw20s() (*TokensRes, error)
VerifiedIbcs() (*IbcsRes, error)
}
type client struct {
http.Client
}
func NewClient() Client {
return &client{http.Client{}}
}
// VerifiedCw20s implements Client
func (c *client) VerifiedCw20s() (*TokensRes, error) {
res, err := get[TokensRes](&c.Client, "https://assets.xpla.io/cw20/tokens.json")
if err != nil {
return nil, errors.Wrap(err, "VerifiedCw20s")
}
return res, nil
}
// VerifiedIbcs implements Client
func (c *client) VerifiedIbcs() (*IbcsRes, error) {
res, err := get[IbcsRes](&c.Client, "https://assets.xpla.io/ibc/tokens.json")
if err != nil {
return nil, errors.Wrap(err, "VerifiedIbcs")
}
return res, nil
}
func get[T unmarshalable](c *http.Client, url string) (*T, error) {
res, err := c.Get(url)
if err != nil {
return nil, errors.Wrap(err, "xpla.get")
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "xpla.get")
}
t := new(T)
if err := json.Unmarshal(body, &t); err != nil {
return nil, errors.Wrap(err, "xpla.get")
}
return t, nil
}