-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrest_sandbox_client.go
117 lines (90 loc) · 3.45 KB
/
rest_sandbox_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
package sdk
import (
"context"
"fmt"
)
// SandboxRestClient rest client for sandbox tinkoff invest.
type SandboxRestClient struct {
*RestClient
}
// NewSandboxRestClient returns new SandboxRestClient by token.
func NewSandboxRestClient(token string) *SandboxRestClient {
return &SandboxRestClient{RestClient: NewRestClient(token, WithURL(RestAPIURL+"/sandbox"))}
}
// NewSandboxRestClientCustom returns new custom SandboxRestClient by token and api url.
func NewSandboxRestClientCustom(token, apiURL string) *SandboxRestClient {
return &SandboxRestClient{RestClient: NewRestClient(token, WithURL(apiURL))}
}
// Register see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/sandbox/post_sandbox_register.
func (c *SandboxRestClient) Register(ctx context.Context, accountType AccountType) (Account, error) {
var response struct {
Payload Account `json:"payload"`
}
path := c.url + "/sandbox/register"
payload := struct {
AccountType AccountType `json:"brokerAccountType"`
}{AccountType: accountType}
err := c.provider.Post(ctx, path, c.token, payload, &response)
if err != nil {
return Account{}, fmt.Errorf("provider post: %w", err)
}
return response.Payload, nil
}
// Clear see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/sandbox/post_sandbox_clear.
func (c *SandboxRestClient) Clear(ctx context.Context, accountID string) error {
path := c.url + "/sandbox/clear"
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
}
// Remove see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/sandbox/post_sandbox_remove.
func (c *SandboxRestClient) Remove(ctx context.Context, accountID string) error {
path := c.url + "/sandbox/remove"
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
}
// SetCurrencyBalance see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/sandbox/post_sandbox_currencies_balance.
func (c *SandboxRestClient) SetCurrencyBalance(ctx context.Context, accountID string, currency Currency, balance float64) error {
path := c.url + "/sandbox/currencies/balance"
payload := struct {
Currency Currency `json:"currency"`
Balance float64 `json:"balance"`
AccountID string `json:"brokerAccountId,omitempty"`
}{Currency: currency, Balance: balance}
if accountID != DefaultAccount {
payload.AccountID = accountID
}
err := c.provider.Post(ctx, path, c.token, payload, nil)
if err != nil {
return fmt.Errorf("provider post: %w", err)
}
return nil
}
// SetPositionsBalance see docs https://tinkoffcreditsystems.github.io/invest-openapi/swagger-ui/#/sandbox/post_sandbox_positions_balance.
func (c *SandboxRestClient) SetPositionsBalance(ctx context.Context, accountID, figi string, balance float64) error {
path := c.url + "/sandbox/positions/balance"
payload := struct {
FIGI string `json:"figi"`
Balance float64 `json:"balance"`
AccountID string `json:"brokerAccountId,omitempty"`
}{FIGI: figi, Balance: balance}
if accountID != DefaultAccount {
payload.AccountID = accountID
}
err := c.provider.Post(ctx, path, c.token, payload, nil)
if err != nil {
return fmt.Errorf("provider post: %w", err)
}
return nil
}