-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruments.go
175 lines (152 loc) · 4.4 KB
/
instruments.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gotd
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/pkg/errors"
)
// NewInstrumentsService https://developer.tdameritrade.com/instruments/apis
// Search for instrument and fundamental data
func NewInstrumentsService(s *Service) *InstrumentsService {
rs := &InstrumentsService{s: s}
return rs
}
// InstrumentsService https://developer.tdameritrade.com/instruments/apis
// Search for instrument and fundamental data
type InstrumentsService struct {
s *Service
}
// SearchInstruments https://developer.tdameritrade.com/instruments/apis/get/instruments
// Search or retrieve instrument data, including fundamental data.
func (r *InstrumentsService) SearchInstruments(symbol, projection string) *InstrumentsSearchInstrumentsCall {
c := &InstrumentsSearchInstrumentsCall{
DefaultCall: DefaultCall{
s: r.s,
urlParams: url.Values{},
},
}
c.urlParams.Set("symbol", symbol)
c.urlParams.Set("projection", projection)
return c
}
// InstrumentsSearchInstrumentsCall https://developer.tdameritrade.com/instruments/apis/get/instruments
// Search or retrieve instrument data, including fundamental data.
type InstrumentsSearchInstrumentsCall struct {
DefaultCall
}
func (c *InstrumentsSearchInstrumentsCall) 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, "instruments")
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 *InstrumentsSearchInstrumentsCall) Do() (*InstrumentMap, 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")
}
ret := &InstrumentMap{
ServerResponse: ServerResponse{
Header: res.Header,
HTTPStatusCode: res.StatusCode,
},
}
target := new(map[string]*Instrument)
if err := DecodeResponse(target, res); err != nil {
return nil, errors.Wrapf(err, "DecodeResponse")
}
ret.Instruments = *target
return ret, nil
}
// GetInstrument https://developer.tdameritrade.com/instruments/apis/get/instruments/%7Bcusip%7D
// Get an instrument by CUSIP
func (r *InstrumentsService) GetInstrument(cusip string) *InstrumentsGetInstrumentCall {
c := &InstrumentsGetInstrumentCall{
DefaultCall: DefaultCall{
s: r.s,
urlParams: url.Values{},
},
cusip: cusip,
}
return c
}
// InstrumentsGetInstrumentCall https://developer.tdameritrade.com/instruments/apis/get/instruments/%7Bcusip%7D
// Get an instrument by CUSIP
type InstrumentsGetInstrumentCall struct {
DefaultCall
cusip string
}
func (c *InstrumentsGetInstrumentCall) 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, "instruments", c.cusip)
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 *InstrumentsGetInstrumentCall) Do() (*Instrument, 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([]*Instrument)
if err := DecodeResponse(target, res); err != nil {
return nil, errors.Wrapf(err, "DecodeResponse")
}
if len(*target) == 0 {
return nil, fmt.Errorf("%s could not be found", c.cusip)
}
ret := (*target)[0]
ret.ServerResponse = ServerResponse{
Header: res.Header,
HTTPStatusCode: res.StatusCode,
}
return ret, nil
}