-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorders.go
244 lines (183 loc) · 6.89 KB
/
orders.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
package tasty
import (
"fmt"
"net/http"
)
// Reconfirm an order
// ** This is currently untested.
// Requires the order to be an Equity Offering
// Unable to submit equity offering orders even in cert environment
// equity_offering_not_supported.
func (c *Client) ReconfirmOrder(accountNumber string, id int) (Order, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/%d/reconfirm", accountNumber, id)
type ordersResponse struct {
Order Order `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodPost, path, nil, nil, ordersRes)
if err != nil {
return Order{}, resp, err
}
return ordersRes.Order, resp, nil
}
// Create an order and then runs the preflights without placing the order.
func (c *Client) SubmitOrderDryRun(accountNumber string, order NewOrder) (OrderResponse, *OrderErrorResponse, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/dry-run", accountNumber)
type ordersResponse struct {
OrderResponse OrderResponse `json:"data"`
OrderError *OrderErrorResponse `json:"error"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodPost, path, nil, order, ordersRes)
if err != nil {
return OrderResponse{}, nil, resp, err
}
return ordersRes.OrderResponse, ordersRes.OrderError, resp, nil
}
// Create an order for the client.
func (c *Client) SubmitOrder(accountNumber string, order NewOrder) (OrderResponse, *OrderErrorResponse, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders", accountNumber)
type ordersResponse struct {
OrderResponse OrderResponse `json:"data"`
OrderError *OrderErrorResponse `json:"error"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodPost, path, nil, order, ordersRes)
if err != nil {
return OrderResponse{}, nil, resp, err
}
return ordersRes.OrderResponse, ordersRes.OrderError, resp, nil
}
// Returns a list of live orders for the resource.
func (c *Client) GetAccountLiveOrders(accountNumber string) ([]Order, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/live", accountNumber)
type ordersResponse struct {
Data struct {
Orders []Order `json:"items"`
} `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodGet, path, nil, nil, ordersRes)
if err != nil {
return []Order{}, resp, err
}
return ordersRes.Data.Orders, resp, nil
}
// Returns a paginated list of the account's orders (as identified by the provided
// authentication token) based on sort param. If no sort is passed in, it defaults
// to descending order.
func (c *Client) GetAccountOrders(accountNumber string, query OrdersQuery) ([]Order, Pagination, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders", accountNumber)
type ordersResponse struct {
Data struct {
Orders []Order `json:"items"`
} `json:"data"`
Pagination Pagination `json:"pagination"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodGet, path, query, nil, ordersRes)
if err != nil {
return []Order{}, Pagination{}, resp, err
}
return ordersRes.Data.Orders, ordersRes.Pagination, resp, nil
}
// Runs through preflights for cancel-replace and edit without routing.
func (c *Client) SubmitOrderECRDryRun(accountNumber string, id int, orderECR NewOrderECR) (OrderResponse, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/%d/dry-run", accountNumber, id)
type ordersResponse struct {
OrderResponse OrderResponse `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodPost, path, nil, orderECR, ordersRes)
if err != nil {
return OrderResponse{}, resp, err
}
return ordersRes.OrderResponse, resp, nil
}
// Returns a single order based on the id.
func (c *Client) GetOrder(accountNumber string, id int) (Order, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/%d", accountNumber, id)
type ordersResponse struct {
Order Order `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodGet, path, nil, nil, ordersRes)
if err != nil {
return Order{}, resp, err
}
return ordersRes.Order, resp, nil
}
// Requests order cancellation.
func (c *Client) CancelOrder(accountNumber string, id int) (Order, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/%d", accountNumber, id)
type ordersResponse struct {
Order Order `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodDelete, path, nil, nil, ordersRes)
if err != nil {
return Order{}, resp, err
}
return ordersRes.Order, resp, nil
}
// Replaces a live order with a new one. Subsequent fills of the original
// order will abort the replacement.
func (c *Client) ReplaceOrder(accountNumber string, id int, orderECR NewOrderECR) (Order, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/%d", accountNumber, id)
type ordersResponse struct {
Order Order `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodPut, path, nil, orderECR, ordersRes)
if err != nil {
return Order{}, resp, err
}
return ordersRes.Order, resp, nil
}
// Edit price and execution properties of a live order by replacement.
// Subsequent fills of the original order will abort the replacement.
func (c *Client) PatchOrder(accountNumber string, id int, orderECR NewOrderECR) (Order, *http.Response, error) {
path := fmt.Sprintf("/accounts/%s/orders/%d", accountNumber, id)
type ordersResponse struct {
Order Order `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodPatch, path, nil, orderECR, ordersRes)
if err != nil {
return Order{}, resp, err
}
return ordersRes.Order, resp, nil
}
// Returns a list of live orders for the resource.
// Requires account numbers param to pull orders from.
func (c *Client) GetCustomerLiveOrders(customerID string, query OrdersQuery) ([]Order, *http.Response, error) {
path := fmt.Sprintf("/customers/%s/orders/live", customerID)
type ordersResponse struct {
Data struct {
Orders []Order `json:"items"`
} `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodGet, path, query, nil, ordersRes)
if err != nil {
return []Order{}, resp, err
}
return ordersRes.Data.Orders, resp, nil
}
// Returns a paginated list of the customer's orders (as identified by the provided
// authentication token) based on sort param. If no sort is passed in, it defaults
// to descending order. Requires account numbers param to pull orders from.
func (c *Client) GetCustomerOrders(customerID string, query OrdersQuery) ([]Order, *http.Response, error) {
path := fmt.Sprintf("/customers/%s/orders", customerID)
type ordersResponse struct {
Data struct {
Orders []Order `json:"items"`
} `json:"data"`
}
ordersRes := new(ordersResponse)
resp, err := c.request(http.MethodGet, path, query, nil, ordersRes)
if err != nil {
return []Order{}, resp, err
}
return ordersRes.Data.Orders, resp, nil
}