-
Notifications
You must be signed in to change notification settings - Fork 0
/
brapi.go
284 lines (206 loc) · 5.54 KB
/
brapi.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package brapigo
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"time"
)
const DEFAULT_BASE_URL = "https://brapi.dev/api"
func NewBrApi() BrApi {
return BrApi{baseUrl: DEFAULT_BASE_URL, Client: &http.Client{
Timeout: 10 * time.Second,
}}
}
func NewBrApiWithCustomBaseUrl(baseUrl string) BrApi {
return BrApi{baseUrl: baseUrl, Client: &http.Client{
Timeout: 10 * time.Second,
}}
}
type BrApi struct {
baseUrl string
Token string
Client *http.Client
}
type StockApiResponse struct {
Stocks []Stock `json:"stocks"`
}
type QuoteApiResponse struct {
Results []Quote `json:"results"`
}
type TickerApiResponse struct {
Stocks []string `json:"stocks"`
}
type Stock struct {
Stock string `json:"stock"`
Name string `json:"name"`
Close float64 `json:"close"`
Change float64 `json:"change"`
Volume int64 `json:"volume"`
MarketCap float64 `json:"market_cap"`
Logo string `json:"logo"`
Sector string `json:"sector"`
}
type Quote struct {
Symbol string `json:"symbol"`
ShortName string `json:"shortName"`
LongName string `json:"LongName"`
Currency string `json:"Currency"`
RegularMarketPrice float64 `json:"RegularMarketPrice"`
}
type CoinsApiResponse struct {
Coins []string `json:"coins"`
}
type CryptoCoin struct {
Currency string `json:"currency"`
CurrencyRateFromUSD float64 `json:"currencyRateFromUSD"`
CoinName string `json:"coinName"`
Coin string `json:"coin"`
RegularMarketChange float64 `json:"regularMarketChange"`
RegularMarketPrice float64 `json:"regularMarketPrice"`
RegularMarketChangePercent float64 `json:"regularMarketChangePercent"`
RegularMarketDayLow float64 `json:"regularMarketDayLow"`
RegularMarketDayHigh float64 `json:"regularMarketDayHigh"`
RegularMarketDayRange string `json:"regularMarketDayRange"`
RegularMarketVolume float64 `json:"regularMarketVolume"`
MarketCap float64 `json:"marketCap"`
RegularMarketTime int `json:"regularMarketTime"`
CoinImageUrl string `json:"coinImageUrl"`
}
func (a BrApi) FindAssetByTicker(tickers ...string) ([]Quote, error) {
tickersParam := strings.Join(tickers, ",")
req, err := http.NewRequest("GET", a.baseUrl+"/quote/"+tickersParam, nil)
q := req.URL.Query()
if a.Token != "" {
q.Add("token", a.Token)
}
req.URL.RawQuery = q.Encode()
resp, err := a.Client.Do(req)
return parseQuoteResponse(resp, err)
}
func (a BrApi) SearchTickets(keyword string) ([]string, error) {
req, err := http.NewRequest("GET", a.baseUrl+"/available", nil)
q := req.URL.Query()
q.Add("search", keyword)
if a.Token != "" {
q.Add("token", a.Token)
}
req.URL.RawQuery = q.Encode()
if err != nil {
return nil, err
}
resp, err := a.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
tickerResponse := TickerApiResponse{}
err = json.Unmarshal(responseData, &tickerResponse)
if err != nil {
return nil, err
}
return tickerResponse.Stocks, nil
}
func (a BrApi) ListStocks() ([]Stock, error) {
req, err := http.NewRequest("GET", a.baseUrl+"/quote/list", nil)
q := req.URL.Query()
if a.Token != "" {
q.Add("token", a.Token)
}
req.URL.RawQuery = q.Encode()
if err != nil {
return nil, err
}
resp, err := a.Client.Do(req)
return parseStockResponse(resp, err)
}
func (a BrApi) ListCryptoCoins() ([]string, error) {
req, err := http.NewRequest("GET", a.baseUrl+"/v2/crypto/available", nil)
q := req.URL.Query()
if a.Token != "" {
q.Add("token", a.Token)
}
req.URL.RawQuery = q.Encode()
if err != nil {
return nil, err
}
resp, err := a.Client.Do(req)
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
coinsResponse := CoinsApiResponse{}
err = json.Unmarshal(responseData, &coinsResponse)
if err != nil {
return nil, err
}
return coinsResponse.Coins, nil
}
func (a BrApi) FindCryptoCoin(coins []string, currency string) ([]CryptoCoin, error) {
if len(coins) == 0 {
return nil, errors.New("it is necessary to inform at least one cryptocoin")
}
req, err := http.NewRequest("GET", a.baseUrl+"/v2/crypto", nil)
q := req.URL.Query()
if a.Token != "" {
q.Add("token", a.Token)
}
q.Add("coin", strings.Join(coins, ","))
if currency != "" {
q.Add("currency", currency)
}
req.URL.RawQuery = q.Encode()
if err != nil {
return nil, err
}
resp, err := a.Client.Do(req)
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
coinsRes := struct {
Coins []CryptoCoin `json:"coins"`
}{}
err = json.Unmarshal(responseData, &coinsRes)
if err != nil {
return nil, err
}
return coinsRes.Coins, nil
}
func parseStockResponse(resp *http.Response, err error) ([]Stock, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
stocksData := StockApiResponse{}
err = json.Unmarshal(responseData, &stocksData)
if err != nil {
return nil, err
}
return stocksData.Stocks, nil
}
func parseQuoteResponse(resp *http.Response, err error) ([]Quote, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
quoteData := QuoteApiResponse{}
err = json.Unmarshal(responseData, "eData)
if err != nil {
return nil, err
}
return quoteData.Results, nil
}