-
Notifications
You must be signed in to change notification settings - Fork 2
/
history.go
112 lines (97 loc) · 3.18 KB
/
history.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
package uexchange
import (
"encoding/json"
"fmt"
"net/url"
)
// GetTradeHistory - get trading history by pairs
func (c *Client) GetTradeHistory(pairSymbol string) (*TradeHistoryDataContainer, error) {
reqFields := url.Values{}
reqFields.Add("pair", pairSymbol)
body, err := c.sendRequest(c.getAPIURL("history/trade"), requestTypeGET, reqFields)
if err != nil {
return nil, err
}
// decode response
var response APITradeHistoryResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("get trade history: %s", response.Error)
}
return &response.Result, nil
}
// GetAccountHistoryService - get operations history service
type GetAccountHistoryService struct {
// required
ExchangeClient *Client
RequestType string // history type: profile/trade/billing
// optional
FromID string // pagination offset: uuid
RecordType string // billing operation type (only for billing): payment/comission/withdraw or combined
Currency string // currency (only for billing type)
}
// NewGetAccountHistoryService - create new get account history service
func (c *Client) NewGetAccountHistoryService(requestType string) *GetAccountHistoryService {
// TODO: validate request type
return &GetAccountHistoryService{
ExchangeClient: c,
RequestType: requestType,
}
}
// SetRequestType - set request type for request
func (s *GetAccountHistoryService) SetRequestType(newRequestType string) *GetAccountHistoryService {
s.RequestType = newRequestType
return s
}
// SetFromID - set form ID for request: pagination offset: uuid
func (s *GetAccountHistoryService) SetFromID(newID string) *GetAccountHistoryService {
s.FromID = newID
return s
}
// SetRecordType - set record type for request:
// billing operation type (only for billing): payment/comission/withdraw or combined
func (s *GetAccountHistoryService) SetRecordType(newRecordType string) *GetAccountHistoryService {
// TODO: validate record type
s.RecordType = newRecordType
return s
}
// SetCurrency - set currency for request: currency (only for billing type)
func (s *GetAccountHistoryService) SetCurrency(newCurrency string) *GetAccountHistoryService {
s.Currency = newCurrency
return s
}
// Do request
func (s *GetAccountHistoryService) Do() (*OperationsHistoryDataContainer, error) {
requestFieldsMap := url.Values{}
requestFieldsMap.Add("type", s.RequestType)
if s.FromID != "" {
requestFieldsMap.Add("from_id", s.FromID)
}
if s.RecordType != "" {
requestFieldsMap.Add("record_type", s.RecordType)
}
if s.Currency != "" {
requestFieldsMap.Add("currency", s.Currency)
}
body, err := s.ExchangeClient.sendRequest(
s.ExchangeClient.getAPIURL("history"), // endpoint
"POST", // request type
requestFieldsMap, // request fields
)
if err != nil {
return nil, err
}
// decode response
var response APIOperationsHistoryResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("decode response: %s", err)
}
if !response.Success {
return nil, fmt.Errorf("get operations history: %s", response.Error)
}
return &response.Result, nil
}