@@ -2,6 +2,7 @@ package sdk
22
33import (
44 "bytes"
5+ "context"
56 "encoding/json"
67 "fmt"
78 "io"
@@ -31,17 +32,17 @@ func NewRestClient(token string) *RestClient {
3132func NewRestClientCustom (token , apiURL string ) * RestClient {
3233 return & RestClient {
3334 httpClient : & http.Client {
34- Timeout : 5 * time .Second ,
35+ Timeout : 30 * time .Second ,
3536 },
3637 token : token ,
3738 apiURL : apiURL ,
3839 }
3940}
4041
41- func (c * RestClient ) SearchInstrumentByFIGI (figi string ) (Instrument , error ) {
42+ func (c * RestClient ) SearchInstrumentByFIGI (ctx context. Context , figi string ) (Instrument , error ) {
4243 path := c .apiURL + "/market/search/by-figi?figi=" + figi
4344
44- req , err := c .newRequest (http .MethodGet , path , nil )
45+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
4546 if err != nil {
4647 return Instrument {}, err
4748 }
@@ -63,38 +64,38 @@ func (c *RestClient) SearchInstrumentByFIGI(figi string) (Instrument, error) {
6364 return resp .Payload , nil
6465}
6566
66- func (c * RestClient ) SearchInstrumentByTicker (ticker string ) ([]Instrument , error ) {
67+ func (c * RestClient ) SearchInstrumentByTicker (ctx context. Context , ticker string ) ([]Instrument , error ) {
6768 path := c .apiURL + "/market/search/by-ticker?ticker=" + ticker
6869
69- return c .instruments (path )
70+ return c .instruments (ctx , path )
7071}
7172
72- func (c * RestClient ) Currencies () ([]Instrument , error ) {
73+ func (c * RestClient ) Currencies (ctx context. Context ) ([]Instrument , error ) {
7374 path := c .apiURL + "/market/currencies"
7475
75- return c .instruments (path )
76+ return c .instruments (ctx , path )
7677}
7778
78- func (c * RestClient ) ETFs () ([]Instrument , error ) {
79+ func (c * RestClient ) ETFs (ctx context. Context ) ([]Instrument , error ) {
7980 path := c .apiURL + "/market/etfs"
8081
81- return c .instruments (path )
82+ return c .instruments (ctx , path )
8283}
8384
84- func (c * RestClient ) Bonds () ([]Instrument , error ) {
85+ func (c * RestClient ) Bonds (ctx context. Context ) ([]Instrument , error ) {
8586 path := c .apiURL + "/market/bonds"
8687
87- return c .instruments (path )
88+ return c .instruments (ctx , path )
8889}
8990
90- func (c * RestClient ) Stocks () ([]Instrument , error ) {
91+ func (c * RestClient ) Stocks (ctx context. Context ) ([]Instrument , error ) {
9192 path := c .apiURL + "/market/stocks"
9293
93- return c .instruments (path )
94+ return c .instruments (ctx , path )
9495}
9596
96- func (c * RestClient ) instruments (path string ) ([]Instrument , error ) {
97- req , err := c .newRequest (http .MethodGet , path , nil )
97+ func (c * RestClient ) instruments (ctx context. Context , path string ) ([]Instrument , error ) {
98+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
9899 if err != nil {
99100 return nil , err
100101 }
@@ -118,7 +119,7 @@ func (c *RestClient) instruments(path string) ([]Instrument, error) {
118119 return resp .Payload .Instruments , nil
119120}
120121
121- func (c * RestClient ) Operations (from , to time.Time , figi string ) ([]Operation , error ) {
122+ func (c * RestClient ) Operations (ctx context. Context , from , to time.Time , figi string ) ([]Operation , error ) {
122123 q := url.Values {
123124 "from" : []string {from .Format (time .RFC3339 )},
124125 "to" : []string {to .Format (time .RFC3339 )},
@@ -129,7 +130,7 @@ func (c *RestClient) Operations(from, to time.Time, figi string) ([]Operation, e
129130
130131 path := c .apiURL + "/operations?" + q .Encode ()
131132
132- req , err := c .newRequest (http .MethodGet , path , nil )
133+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
133134 if err != nil {
134135 return nil , err
135136 }
@@ -153,13 +154,13 @@ func (c *RestClient) Operations(from, to time.Time, figi string) ([]Operation, e
153154 return resp .Payload .Operations , nil
154155}
155156
156- func (c * RestClient ) Portfolio () (Portfolio , error ) {
157- positions , err := c .PositionsPortfolio ()
157+ func (c * RestClient ) Portfolio (ctx context. Context ) (Portfolio , error ) {
158+ positions , err := c .PositionsPortfolio (ctx )
158159 if err != nil {
159160 return Portfolio {}, err
160161 }
161162
162- currencies , err := c .CurrenciesPortfolio ()
163+ currencies , err := c .CurrenciesPortfolio (ctx )
163164 if err != nil {
164165 return Portfolio {}, err
165166 }
@@ -170,10 +171,10 @@ func (c *RestClient) Portfolio() (Portfolio, error) {
170171 }, nil
171172}
172173
173- func (c * RestClient ) PositionsPortfolio () ([]PositionBalance , error ) {
174+ func (c * RestClient ) PositionsPortfolio (ctx context. Context ) ([]PositionBalance , error ) {
174175 path := c .apiURL + "/portfolio"
175176
176- req , err := c .newRequest (http .MethodGet , path , nil )
177+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
177178 if err != nil {
178179 return nil , err
179180 }
@@ -197,10 +198,10 @@ func (c *RestClient) PositionsPortfolio() ([]PositionBalance, error) {
197198 return resp .Payload .Positions , nil
198199}
199200
200- func (c * RestClient ) CurrenciesPortfolio () ([]CurrencyBalance , error ) {
201+ func (c * RestClient ) CurrenciesPortfolio (ctx context. Context ) ([]CurrencyBalance , error ) {
201202 path := c .apiURL + "/portfolio/currencies"
202203
203- req , err := c .newRequest (http .MethodGet , path , nil )
204+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
204205 if err != nil {
205206 return nil , err
206207 }
@@ -224,13 +225,13 @@ func (c *RestClient) CurrenciesPortfolio() ([]CurrencyBalance, error) {
224225 return resp .Payload .Currencies , nil
225226}
226227
227- func (c * RestClient ) OrderCancel (id string ) error {
228+ func (c * RestClient ) OrderCancel (ctx context. Context , id string ) error {
228229 path := c .apiURL + "/orders/cancel?orderId=" + id
229230
230- return c .postJSONThrow (path , nil )
231+ return c .postJSONThrow (ctx , path , nil )
231232}
232233
233- func (c * RestClient ) LimitOrder (figi string , lots int , operation OperationType , price float64 ) (PlacedLimitOrder , error ) {
234+ func (c * RestClient ) LimitOrder (ctx context. Context , figi string , lots int , operation OperationType , price float64 ) (PlacedLimitOrder , error ) {
234235 path := c .apiURL + "/orders/limit-order?figi=" + figi
235236
236237 payload := struct {
@@ -244,7 +245,7 @@ func (c *RestClient) LimitOrder(figi string, lots int, operation OperationType,
244245 return PlacedLimitOrder {}, errors .Errorf ("can't marshal request to %s body=%+v" , path , payload )
245246 }
246247
247- req , err := c .newRequest (http .MethodPost , path , bytes .NewReader (bb ))
248+ req , err := c .newRequest (ctx , http .MethodPost , path , bytes .NewReader (bb ))
248249 if err != nil {
249250 return PlacedLimitOrder {}, err
250251 }
@@ -266,10 +267,10 @@ func (c *RestClient) LimitOrder(figi string, lots int, operation OperationType,
266267 return resp .Payload , nil
267268}
268269
269- func (c * RestClient ) Orders () ([]Order , error ) {
270+ func (c * RestClient ) Orders (ctx context. Context ) ([]Order , error ) {
270271 path := c .apiURL + "/orders"
271272
272- req , err := c .newRequest (http .MethodGet , path , nil )
273+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
273274 if err != nil {
274275 return nil , err
275276 }
@@ -291,7 +292,7 @@ func (c *RestClient) Orders() ([]Order, error) {
291292 return resp .Payload , nil
292293}
293294
294- func (c * RestClient ) Candles (from , to time.Time , interval CandleInterval , figi string ) ([]Candle , error ) {
295+ func (c * RestClient ) Candles (ctx context. Context , from , to time.Time , interval CandleInterval , figi string ) ([]Candle , error ) {
295296 q := url.Values {
296297 "from" : []string {from .Format (time .RFC3339 )},
297298 "to" : []string {to .Format (time .RFC3339 )},
@@ -300,7 +301,7 @@ func (c *RestClient) Candles(from, to time.Time, interval CandleInterval, figi s
300301 }
301302 path := c .apiURL + "/market/candles?" + q .Encode ()
302303
303- req , err := c .newRequest (http .MethodGet , path , nil )
304+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
304305 if err != nil {
305306 return nil , err
306307 }
@@ -326,7 +327,7 @@ func (c *RestClient) Candles(from, to time.Time, interval CandleInterval, figi s
326327 return resp .Payload .Candles , nil
327328}
328329
329- func (c * RestClient ) Orderbook (depth int , figi string ) (RestOrderBook , error ) {
330+ func (c * RestClient ) Orderbook (ctx context. Context , depth int , figi string ) (RestOrderBook , error ) {
330331 if depth < 1 || depth > MaxOrderbookDepth {
331332 return RestOrderBook {}, ErrDepth
332333 }
@@ -337,7 +338,7 @@ func (c *RestClient) Orderbook(depth int, figi string) (RestOrderBook, error) {
337338 }
338339 path := c .apiURL + "/market/orderbook?" + q .Encode ()
339340
340- req , err := c .newRequest (http .MethodGet , path , nil )
341+ req , err := c .newRequest (ctx , http .MethodGet , path , nil )
341342 if err != nil {
342343 return RestOrderBook {}, err
343344 }
@@ -359,7 +360,7 @@ func (c *RestClient) Orderbook(depth int, figi string) (RestOrderBook, error) {
359360 return resp .Payload , nil
360361}
361362
362- func (c * RestClient ) postJSONThrow (url string , body interface {}) error {
363+ func (c * RestClient ) postJSONThrow (ctx context. Context , url string , body interface {}) error {
363364 var bb []byte
364365 var err error
365366
@@ -370,7 +371,7 @@ func (c *RestClient) postJSONThrow(url string, body interface{}) error {
370371 }
371372 }
372373
373- req , err := c .newRequest (http .MethodPost , url , bytes .NewReader (bb ))
374+ req , err := c .newRequest (ctx , http .MethodPost , url , bytes .NewReader (bb ))
374375 if err != nil {
375376 return err
376377 }
@@ -379,7 +380,7 @@ func (c *RestClient) postJSONThrow(url string, body interface{}) error {
379380 return err
380381}
381382
382- func (c * RestClient ) newRequest (method , url string , body io.Reader ) (* http.Request , error ) {
383+ func (c * RestClient ) newRequest (ctx context. Context , method , url string , body io.Reader ) (* http.Request , error ) {
383384 req , err := http .NewRequest (method , url , body )
384385 if err != nil {
385386 return nil , errors .Errorf ("can't create http request to %s" , url )
@@ -388,7 +389,7 @@ func (c *RestClient) newRequest(method, url string, body io.Reader) (*http.Reque
388389 req .Header .Set ("Content-Type" , "application/json" )
389390 req .Header .Set ("Authorization" , "Bearer " + c .token )
390391
391- return req , nil
392+ return req . WithContext ( ctx ) , nil
392393}
393394
394395func (c * RestClient ) doRequest (req * http.Request ) ([]byte , error ) {
0 commit comments