-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_client.go
98 lines (91 loc) · 2.62 KB
/
api_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
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
package coinbasepro
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"github.com/chain-bot/prices/app/pkg/api/common"
"github.com/hashicorp/go-retryablehttp"
log "github.com/sirupsen/logrus"
"golang.org/x/time/rate"
)
type ApiClient struct {
*retryablehttp.Client
*rate.Limiter
}
func NewCoinbaseProAPIClient() *ApiClient {
// 3 callsPerSecond
rateLimiter := rate.NewLimiter(rate.Every(time.Second/3), 6)
httpClient := retryablehttp.NewClient()
httpClient.CheckRetry = common.DefaultCheckRetry
httpClient.RetryWaitMin = common.DefaultRetryMin
httpClient.RetryMax = common.MaxRetries
apiClient := ApiClient{
Client: httpClient,
Limiter: rateLimiter,
}
apiClient.RequestLogHook = func(logger retryablehttp.Logger, req *http.Request, retry int) {
if err := apiClient.Limiter.Wait(context.Background()); err != nil {
log.WithField("err", err.Error()).Errorf("waiting for rate limit")
return
}
}
return &apiClient
}
func (apiClient *ApiClient) GetExchangeIdentifier() string {
return COINBASEPRO
}
// Get CandleStick data from [startTime, endTime]
func (apiClient *ApiClient) getCandleStickData(
granularity int,
startTime time.Time,
endTime time.Time,
productID string,
) (candleStickResponse []*CandleStickData, err error) {
params := url.Values{}
params.Add("start", startTime.Format(time.RFC3339))
params.Add("end", endTime.Format(time.RFC3339))
params.Add("granularity", strconv.Itoa(granularity))
path := fmt.Sprintf(getCandles, productID)
urlString := fmt.Sprintf("%s%s?%s", baseURL, path, params.Encode())
resp, err := apiClient.sendUnAuthenticatedGetRequest(urlString)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err = json.Unmarshal(body, &candleStickResponse); err != nil {
return nil, err
}
return candleStickResponse, nil
}
func (apiClient *ApiClient) getProducts() (productsResponse ProductsResponse, err error) {
urlString := fmt.Sprintf("%s%s", baseURL, getExchangeProducts)
resp, err := apiClient.sendUnAuthenticatedGetRequest(urlString)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err = json.Unmarshal(body, &productsResponse); err != nil {
return nil, err
}
return productsResponse, nil
}
func (apiClient *ApiClient) sendUnAuthenticatedGetRequest(
urlString string,
) (*http.Response, error) {
httpReq, err := http.NewRequest("GET", urlString, nil)
if err != nil {
return nil, err
}
retryableRequest, err := retryablehttp.FromRequest(httpReq)
if err != nil {
return nil, err
}
return apiClient.Do(retryableRequest)
}