forked from llehouerou/go-degiro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
74 lines (66 loc) · 1.93 KB
/
update.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
package degiro
import (
"fmt"
"time"
"github.com/sirupsen/logrus"
)
type updateResponse struct {
Orders updateOrdersResponse `json:"orders"`
Portfolio updatePositionsResponse `json:"portfolio"`
Balance updateBalanceResponse `json:"totalPortfolio"`
}
func (c *Client) update() error {
type updateParams struct {
Orders int `url:"orders"`
Portfolio int `url:"portfolio"`
TotalPortfolio int `url:"totalPortfolio"`
}
response := &updateResponse{}
_, err := c.ReceiveSuccessReloginOn401(c.sling.New().
Get(fmt.Sprintf("trading/secure/v5/update/%d;jsessionid=%s", c.accountId, c.sessionId)).
QueryStruct(updateParams{
Orders: c.ordersLastUpdate,
Portfolio: c.portfolioLastUpdate,
TotalPortfolio: c.totalPortfolioLastUpdate,
}), response)
if err != nil {
return fmt.Errorf("executing request: %v", err)
}
c.ordersLastUpdate = response.Orders.LastUpdated
c.updateOrderCacheFromResponse(response)
c.portfolioLastUpdate = response.Portfolio.LastUpdated
c.updatePositionCacheFromResponse(response)
if c.totalPortfolioLastUpdate != response.Balance.LastUpdated {
c.totalPortfolioLastUpdate = response.Balance.LastUpdated
if b, found := response.Balance.ConvertToBalance(); found {
c.balance.Set(b)
}
}
return nil
}
func (c *Client) updateOrderCacheFromResponse(response *updateResponse) {
added, updated, removed := response.Orders.ConvertToOrders()
c.orders.Add(added)
c.orders.Update(updated)
c.orders.Remove(removed)
}
func (c *Client) updatePositionCacheFromResponse(response *updateResponse) {
added, updated, removed := response.Portfolio.ConvertToPositions()
c.positions.Add(added)
c.positions.Update(updated)
c.positions.Remove(removed)
}
func (c *Client) startUpdating() {
go func() {
ticker := time.NewTicker(c.UpdatePeriod)
for {
select {
case <-ticker.C:
err := c.update()
if err != nil {
logrus.Errorf("updating: %v", err)
}
}
}
}()
}