-
Notifications
You must be signed in to change notification settings - Fork 30
/
rest_client.go
432 lines (347 loc) · 11.6 KB
/
rest_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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package sdk
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// Errors.
var (
ErrDepth = fmt.Errorf("invalid depth. Should be in interval 0 < x <= %d", MaxOrderbookDepth)
ErrNotFound = errors.New("not found")
)
const (
// RestAPIURL contains main api url for tinkoff invest api.
RestAPIURL = "https://api-invest.tinkoff.ru/openapi"
// MaxTimeout for provider request.
MaxTimeout = time.Second * 30
)
type (
// RestClient provide to rest methods from tinkoff invest api.
RestClient struct {
provider Provider
token string
url string
}
// BuildOption build options for rest client.
BuildOption func(*RestClient)
)
// WithProvider build rest client by custom provider client.
func WithProvider(p Provider) BuildOption {
return func(client *RestClient) {
client.provider = p
}
}
// WithURL build rest client by custom api url.
func WithURL(url string) BuildOption {
return func(client *RestClient) {
client.url = url
}
}
// NewRestClient build rest client by option.
func NewRestClient(token string, options ...BuildOption) *RestClient {
client := &RestClient{
provider: &defaultHTTP{
client: &http.Client{
Transport: http.DefaultTransport,
Timeout: MaxTimeout,
},
},
token: token,
url: RestAPIURL,
}
for i := range options {
options[i](client)
}
return client
}
// NewRestClientCustom for backward compatibility only.
// Deprecated: have to use NewRestClient by options.
func NewRestClientCustom(token, apiURL string) *RestClient {
return NewRestClient(token, WithURL(apiURL))
}
// InstrumentByFIGI see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_search_by_figi.
func (c *RestClient) InstrumentByFIGI(ctx context.Context, figi string) (Instrument, error) {
var response struct {
Payload Instrument `json:"payload"`
}
path := c.url + "/market/search/by-figi?figi=" + figi
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return Instrument{}, fmt.Errorf("provider get: %w", err)
}
return response.Payload, nil
}
// InstrumentByTicker see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_search_by_ticker.
func (c *RestClient) InstrumentByTicker(ctx context.Context, ticker string) ([]Instrument, error) {
var response struct {
Payload struct {
Instruments []Instrument `json:"instruments"`
} `json:"payload"`
}
path := c.url + "/market/search/by-ticker?ticker=" + ticker
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Instruments, nil
}
// Currencies see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_currencies.
func (c *RestClient) Currencies(ctx context.Context) ([]Instrument, error) {
var response struct {
Payload struct {
Instruments []Instrument `json:"instruments"`
} `json:"payload"`
}
path := c.url + "/market/currencies"
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Instruments, nil
}
// ETFs see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_etfs.
func (c *RestClient) ETFs(ctx context.Context) ([]Instrument, error) {
var response struct {
Payload struct {
Instruments []Instrument `json:"instruments"`
} `json:"payload"`
}
path := c.url + "/market/etfs"
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Instruments, nil
}
// Bonds see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_bonds.
func (c *RestClient) Bonds(ctx context.Context) ([]Instrument, error) {
var response struct {
Payload struct {
Instruments []Instrument `json:"instruments"`
} `json:"payload"`
}
path := c.url + "/market/bonds"
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Instruments, nil
}
// Stocks see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_stocks.
func (c *RestClient) Stocks(ctx context.Context) ([]Instrument, error) {
var response struct {
Payload struct {
Instruments []Instrument `json:"instruments"`
} `json:"payload"`
}
path := c.url + "/market/stocks"
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Instruments, nil
}
// Operations see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/operations/get_operations.
func (c *RestClient) Operations(ctx context.Context, accountID string, from, to time.Time, figi string) ([]Operation, error) {
var response struct {
Payload struct {
Operations []Operation `json:"operations"`
} `json:"payload"`
}
q := url.Values{
"from": []string{from.Format(time.RFC3339)},
"to": []string{to.Format(time.RFC3339)},
}
if figi != "" {
q.Set("figi", figi)
}
if accountID != DefaultAccount {
q.Set("brokerAccountId", accountID)
}
path := c.url + "/operations?" + q.Encode()
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Operations, nil
}
// Portfolio contains calls two method for get portfolio info.
func (c *RestClient) Portfolio(ctx context.Context, accountID string) (Portfolio, error) {
positions, err := c.PositionsPortfolio(ctx, accountID)
if err != nil {
return Portfolio{}, err
}
currencies, err := c.CurrenciesPortfolio(ctx, accountID)
if err != nil {
return Portfolio{}, err
}
return Portfolio{
Currencies: currencies,
Positions: positions,
}, nil
}
// PositionsPortfolio see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/portfolio/get_portfolio.
func (c *RestClient) PositionsPortfolio(ctx context.Context, accountID string) ([]PositionBalance, error) {
var response struct {
Payload struct {
Positions []PositionBalance `json:"positions"`
} `json:"payload"`
}
path := c.url + "/portfolio"
if accountID != DefaultAccount {
path += "?brokerAccountId=" + accountID
}
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Positions, nil
}
// CurrenciesPortfolio see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/portfolio/get_portfolio_currencies.
func (c *RestClient) CurrenciesPortfolio(ctx context.Context, accountID string) ([]CurrencyBalance, error) {
var response struct {
Payload struct {
Currencies []CurrencyBalance `json:"currencies"`
} `json:"payload"`
}
path := c.url + "/portfolio/currencies"
if accountID != DefaultAccount {
path += "?brokerAccountId=" + accountID
}
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Currencies, nil
}
// OrderCancel see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/orders/post_orders_cancel.
func (c *RestClient) OrderCancel(ctx context.Context, accountID, id string) error {
path := c.url + "/orders/cancel?orderId=" + id
if accountID != DefaultAccount {
path += "&brokerAccountId=" + accountID
}
err := c.provider.Post(ctx, path, c.token, nil, nil)
if err != nil {
return fmt.Errorf("provider post: %w", err)
}
return nil
}
// LimitOrder see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/orders/post_orders_limit_order.
func (c *RestClient) LimitOrder(
ctx context.Context,
accountID, figi string,
lots int,
operation OperationType,
price float64,
) (PlacedOrder, error) {
var response struct {
Payload PlacedOrder `json:"payload"`
}
path := c.url + "/orders/limit-order?figi=" + figi
if accountID != DefaultAccount {
path += "&brokerAccountId=" + accountID
}
payload := struct {
Lots int `json:"lots"`
Operation OperationType `json:"operation"`
Price float64 `json:"price"`
}{Lots: lots, Operation: operation, Price: price}
err := c.provider.Post(ctx, path, c.token, payload, &response)
if err != nil {
return PlacedOrder{}, fmt.Errorf("provider post: %w", err)
}
return response.Payload, nil
}
// MarketOrder see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/orders/post_orders_market_order.
func (c *RestClient) MarketOrder(ctx context.Context, accountID, figi string, lots int, operation OperationType) (PlacedOrder, error) {
var response struct {
Payload PlacedOrder `json:"payload"`
}
path := c.url + "/orders/market-order?figi=" + figi
if accountID != DefaultAccount {
path += "&brokerAccountId=" + accountID
}
payload := struct {
Lots int `json:"lots"`
Operation OperationType `json:"operation"`
}{Lots: lots, Operation: operation}
err := c.provider.Post(ctx, path, c.token, payload, &response)
if err != nil {
return PlacedOrder{}, fmt.Errorf("provider post: %w", err)
}
return response.Payload, nil
}
// Orders see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/orders/get_orders.
func (c *RestClient) Orders(ctx context.Context, accountID string) ([]Order, error) {
var response struct {
Payload []Order `json:"payload"`
}
path := c.url + "/orders"
if accountID != DefaultAccount {
path += "?brokerAccountId=" + accountID
}
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload, nil
}
// Candles see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_candles.
func (c *RestClient) Candles(ctx context.Context, from, to time.Time, interval CandleInterval, figi string) ([]Candle, error) {
var response struct {
Payload struct {
FIGI string `json:"figi"`
Interval CandleInterval `json:"interval"`
Candles []Candle `json:"candles"`
} `json:"payload"`
}
q := url.Values{
"from": []string{from.Format(time.RFC3339)},
"to": []string{to.Format(time.RFC3339)},
"interval": []string{string(interval)},
"figi": []string{figi},
}
path := c.url + "/market/candles?" + q.Encode()
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Candles, nil
}
// Orderbook see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/market/get_market_orderbook.
func (c *RestClient) Orderbook(ctx context.Context, depth int, figi string) (RestOrderBook, error) {
var response struct {
Payload RestOrderBook `json:"payload"`
}
if depth < 1 || depth > MaxOrderbookDepth {
return RestOrderBook{}, ErrDepth
}
q := url.Values{
"depth": []string{strconv.Itoa(depth)},
"figi": []string{figi},
}
path := c.url + "/market/orderbook?" + q.Encode()
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return RestOrderBook{}, fmt.Errorf("provider get: %w", err)
}
return response.Payload, nil
}
// Accounts see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/user/get_user_accounts.
func (c *RestClient) Accounts(ctx context.Context) ([]Account, error) {
var response struct {
Payload struct {
Accounts []Account `json:"accounts"`
} `json:"payload"`
}
path := c.url + "/user/accounts"
err := c.provider.Get(ctx, path, c.token, &response)
if err != nil {
return nil, fmt.Errorf("provider get: %w", err)
}
return response.Payload.Accounts, nil
}