-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
65 lines (52 loc) · 1.16 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
package bitfinex
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
// Client holds details that allow communication with the Bitfinex API.
type Client struct {
Host string
HTTPClient *http.Client
}
// New returns a new Client.
func New() *Client {
t := time.Second * 2
return &Client{
Host: "https://api-pub.bitfinex.com",
HTTPClient: &http.Client{Timeout: t},
}
}
// Tickers returns the details for the given ticker symbols.
//
// See https://docs.bitfinex.com/reference#rest-public-tickers
func (c *Client) Tickers(pairs []string) ([]Ticker, error) {
path := fmt.Sprintf("/v2/tickers?symbols=%s", strings.Join(pairs, ","))
body, err := c.get(path)
if err != nil {
return nil, err
}
tickers, err := ParseTickers(body)
if err != nil {
return nil, err
}
return tickers, nil
}
// get a response from a URL.
//
// This method will handle closing off the body.
func (c Client) get(path string) ([]byte, error) {
url := c.Host + path
resp, err := c.HTTPClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}