forked from llehouerou/go-degiro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
62 lines (55 loc) · 2 KB
/
transaction.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
package degiro
import (
"net/url"
"sort"
"time"
"github.com/shopspring/decimal"
)
type Transaction struct {
BuySell string `json:"buysell"`
Quantity int `json:"quantity"`
OrderType OrderType `json:"orderTypeId"`
CounterParty string `json:"counterParty"`
TotalInBaseCurrency decimal.Decimal `json:"totalInBaseCurrency"`
FeeInBaseCurrency decimal.Decimal `json:"feeInBaseCurrency"`
TotalPlusFeeInBaseCurrency decimal.Decimal `json:"totalPlusFeeInBaseCurrency"`
Transfered bool `json:"transfered"`
ProductId int `json:"productId"`
Price decimal.Decimal `json:"price"`
Date time.Time `json:"date"`
Total decimal.Decimal `json:"total"`
Id int `json:"id"`
}
type shortDateTime time.Time
func (t shortDateTime) EncodeValues(key string, v *url.Values) error {
v.Add(key, time.Time(t).Format("02/01/2006"))
return nil
}
func (c *Client) GetTransactions(fromDate time.Time, toDate time.Time) ([]Transaction, error) {
type getTransactionsResponse struct {
Transactions []Transaction `json:"data"`
}
response := &getTransactionsResponse{}
_, err := c.ReceiveSuccessReloginOn401(c.sling.New().
Get("reporting/secure/v4/transactions").
QueryStruct(&struct {
FromDate shortDateTime `url:"fromDate"`
ToDate shortDateTime `url:"toDate"`
AccountId int64 `url:"intAccount"`
SessionId string `url:"sessionId"`
}{
FromDate: shortDateTime(fromDate),
ToDate: shortDateTime(toDate),
AccountId: c.accountId,
SessionId: c.sessionId,
}), response)
if err != nil {
return nil, err
}
return response.Transactions, nil
}
func sortTransactionsByDateAscending(transactions []Transaction) {
sort.SliceStable(transactions, func(i, j int) bool {
return transactions[i].Date.Before(transactions[j].Date)
})
}