-
Notifications
You must be signed in to change notification settings - Fork 0
/
movers.go
115 lines (98 loc) · 3 KB
/
movers.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
package gotd
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/pkg/errors"
)
// NewMoversService https://developer.tdameritrade.com/movers/apis
// Retrieve mover information by index symbol, direction type and change
func NewMoversService(s *Service) *MoversService {
rs := &MoversService{s: s}
return rs
}
// MoversService https://developer.tdameritrade.com/movers/apis
// Retrieve mover information by index symbol, direction type and change
type MoversService struct {
s *Service
}
// GetMovers https://developer.tdameritrade.com/movers/apis/get/marketdata/%7Bindex%7D/movers
// Top 10 (up or down) movers by value or percent for a particular market
func (r *MoversService) GetMovers(index string) *MoversGetMoversCall {
c := &MoversGetMoversCall{
DefaultCall: DefaultCall{
s: r.s,
urlParams: url.Values{},
},
index: index,
}
return c
}
// MoversGetMoversCall https://developer.tdameritrade.com/movers/apis/get/marketdata/%7Bindex%7D/movers
// Top 10 (up or down) movers by value or percent for a particular market
type MoversGetMoversCall struct {
DefaultCall
index string
}
// Direction https://developer.tdameritrade.com/movers/apis/get/marketdata/%7Bindex%7D/movers
// To return movers with the specified directions of up or down
func (c *MoversGetMoversCall) Direction(direction string) *MoversGetMoversCall {
c.urlParams.Set("direction", direction)
return c
}
// Change https://developer.tdameritrade.com/movers/apis/get/marketdata/%7Bindex%7D/movers
// To return movers with the specified change types of percent or value
func (c *MoversGetMoversCall) Change(change string) *MoversGetMoversCall {
c.urlParams.Set("change", change)
return c
}
func (c *MoversGetMoversCall) doRequest() (*http.Response, error) {
reqHeaders := make(http.Header)
for k, v := range c.header {
reqHeaders[k] = v
}
reqHeaders.Set("User-Agent", c.s.userAgent())
var body io.Reader = nil
urls := ResolveRelative(c.s.BasePath, "marketdata", c.index, "movers")
urls += "?" + c.urlParams.Encode()
req, err := http.NewRequest("GET", urls, body)
if err != nil {
return nil, errors.Wrapf(err, "http.NewRequest")
}
req.Header = reqHeaders
return SendRequest(c.ctx, c.s.client, req)
}
// Do send request
func (c *MoversGetMoversCall) Do() (*Mover, error) {
res, err := c.doRequest()
if res != nil && res.StatusCode == http.StatusNotModified {
if res.Body != nil {
res.Body.Close()
}
return nil, &Error{
Code: res.StatusCode,
Header: res.Header,
}
}
if err != nil {
return nil, errors.Wrapf(err, "doRequest")
}
defer res.Body.Close()
if err := CheckResponse(res); err != nil {
return nil, errors.Wrapf(err, "CheckResponse")
}
target := new(map[string]*Mover)
if err := DecodeResponse(target, res); err != nil {
return nil, errors.Wrapf(err, "DecodeResponse")
}
ret, ok := (*target)[c.index]
if !ok {
return nil, fmt.Errorf("%s could not be found", c.index)
}
ret.ServerResponse = ServerResponse{
Header: res.Header,
HTTPStatusCode: res.StatusCode,
}
return ret, nil
}