-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorders.go
88 lines (76 loc) · 2.19 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
package uexchange
import (
"encoding/json"
"errors"
"fmt"
"net/url"
)
// GetOrdersService - get orders service with optional params
type GetOrdersService struct {
ExchangeClient *Client
OrderType string // order type: open/close/cancel/hold
TaskType string // task type: buy/sell
}
// NewGetOrdersService - create new get orders service
func (c *Client) NewGetOrdersService() *GetOrdersService {
return &GetOrdersService{
ExchangeClient: c,
}
}
// SetOrderType - set order type: open/close/cancel/hold
func (s *GetOrdersService) SetOrderType(newType string) *GetOrdersService {
s.OrderType = newType
return s
}
// SetTaskType - set task type: buy/sell
func (s *GetOrdersService) SetTaskType(taskType string) *GetOrdersService {
s.TaskType = taskType
return s
}
// Do request
func (s *GetOrdersService) Do() (*OrdersDataContainer, error) {
requestFieldsMap := url.Values{}
if s.OrderType != "" {
requestFieldsMap.Add("status", s.OrderType)
}
if s.TaskType != "" {
requestFieldsMap.Add("task", s.TaskType)
}
body, err := s.ExchangeClient.sendRequest(
s.ExchangeClient.getAPIURL("orders"), // endpoint
"GET", // request type
requestFieldsMap, // reuqest fields
)
if err != nil {
return nil, err
}
// decode response
var response APIOrdersResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, errors.New("decode request response: " + err.Error())
}
if !response.Success {
return nil, fmt.Errorf("get orders list: %s", response.Error)
}
return &response.Result, nil
}
// GetOrderHistory - get orders history
func (c *Client) GetOrderHistory(orderID string) (*OrdersHistoryDataContainer, error) {
reqFields := url.Values{}
reqFields.Add("order_id", orderID)
body, err := c.sendRequest(c.getAPIURL("orders/history"), "POST", reqFields)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
}
// decode response
var response APIOrdersHistoryResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("get order history: %s", response.Error)
}
return &response.Result, nil
}