-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwatchlist.go
149 lines (118 loc) · 4.31 KB
/
watchlist.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
package igmarkets
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// WatchlistsResponse - Response for getting all watchlists
type WatchlistsResponse struct {
Watchlists []Watchlist
}
// Watchlist - Response from Watchlist endpoint
type Watchlist struct {
DefaultSystemWatchlist bool `json:"defaultSystemWatchlist"`
Deleteable bool `json:"deleteable"`
Editable bool `json:"editable"`
ID string `json:"id"`
Name string `json:"name"`
}
// WatchlistData - Response from Watchlist endpoint
type WatchlistData struct {
Markets []MarketData `json:"markets"`
}
// WatchlistRequest - For adding epic to watchlist
type WatchlistRequest struct {
Epic string `json:"epic"`
}
// CreateWatchlistResponse - Response for creating new watchlist
type CreateWatchlistResponse struct {
Status string `json:"status"`
WatchlistID string `json:"watchlistId"`
}
// CreateWatchlistRequest - Request for creating new watchlist
type CreateWatchlistRequest struct {
Epics []string `json:"epics"`
Name string `json:"name"`
}
// DeleteFromWatchlist - Delete epic from watchlist
func (ig *IGMarkets) DeleteFromWatchlist(ctx context.Context, watchListID, epic string) error {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/gateway/deal/watchlists/%s/%s",
ig.APIURL, watchListID, epic), bodyReq)
if err != nil {
return fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
_, err = ig.doRequest(ctx, req, 1, nil)
return err
}
// AddToWatchlist - Add epic to existing watchlist
func (ig *IGMarkets) AddToWatchlist(ctx context.Context, watchListID, epic string) error {
wreq := WatchlistRequest{Epic: epic}
bodyReq, err := json.Marshal(&wreq)
if err != nil {
return fmt.Errorf("igmarkets: cannot marshal: %v", err)
}
req, err := http.NewRequest("PUT", ig.APIURL+"/gateway/deal/watchlists/"+watchListID, bytes.NewReader(bodyReq))
if err != nil {
return fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
_, err = ig.doRequest(ctx, req, 1, nil)
return err
}
// GetWatchlist - Get specific watchlist
func (ig *IGMarkets) GetWatchlist(ctx context.Context, watchListID string) (*WatchlistData, error) {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("GET", ig.APIURL+"/gateway/deal/watchlists/"+watchListID, bodyReq)
if err != nil {
return &WatchlistData{}, fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 1, WatchlistData{})
igResponse, _ := igResponseInterface.(*WatchlistData)
return igResponse, err
}
// GetAllWatchlists - Get all watchlist
func (ig *IGMarkets) GetAllWatchlists(ctx context.Context) (*[]Watchlist, error) {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("GET", ig.APIURL+"/gateway/deal/watchlists", bodyReq)
if err != nil {
return &[]Watchlist{}, fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 1, WatchlistsResponse{})
igResponse, _ := igResponseInterface.(*WatchlistsResponse)
return &igResponse.Watchlists, err
}
// DeleteWatchlist - Delete whole watchlist
func (ig *IGMarkets) DeleteWatchlist(ctx context.Context, watchListID string) error {
bodyReq := new(bytes.Buffer)
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/gateway/deal/watchlists/%s",
ig.APIURL, watchListID), bodyReq)
if err != nil {
return fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
_, err = ig.doRequest(ctx, req, 1, nil)
return err
}
// CreateWatchlist - Create new watchlist
func (ig *IGMarkets) CreateWatchlist(ctx context.Context, name string, epics []string) (watchlistID string, err error) {
wreq := CreateWatchlistRequest{
Name: name,
Epics: epics,
}
bodyReq, err := json.Marshal(&wreq)
if err != nil {
return "", fmt.Errorf("igmarkets: cannot marshal: %v", err)
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/gateway/deal/watchlists",
ig.APIURL), bytes.NewReader(bodyReq))
if err != nil {
return "", fmt.Errorf("igmarkets: unable to create HTTP request: %v", err)
}
igResponseInterface, err := ig.doRequest(ctx, req, 1, CreateWatchlistResponse{})
if err != nil {
return "", err
}
igResponse, _ := igResponseInterface.(*CreateWatchlistResponse)
return igResponse.WatchlistID, nil
}