forked from psanford/finance-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinance.go
More file actions
324 lines (270 loc) · 7.35 KB
/
finance.go
File metadata and controls
324 lines (270 loc) · 7.35 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package finance
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"os"
"strings"
"sync"
"time"
"github.com/TraderWithPython/finance-go/form"
"golang.org/x/net/publicsuffix"
)
// Printfer is an interface to be implemented by Logger.
type Printfer interface {
Printf(format string, v ...interface{})
}
// init sets inital logger defaults.
func init() {
Logger = log.New(os.Stderr, "", log.LstdFlags)
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
panic(err)
}
httpClient = &http.Client{
Jar: jar,
Timeout: defaultHTTPTimeout,
}
}
var (
// YFinURL is the URL of the yahoo service backend.
YFinURL = "https://query2.finance.yahoo.com"
YQuotePath = "/v7/finance/quote"
YOptionsPrefix = "/v7/finance/options/"
)
const (
// YFinBackend is a constant representing the yahoo service backend.
YFinBackend SupportedBackend = "yahoo"
// BATSBackend is a constant representing the uploads service backend.
BATSBackend SupportedBackend = "bats"
// BATSURL is the URL of the uploads service backend.
BATSURL string = ""
// Private constants.
// ------------------
defaultHTTPTimeout = 80 * time.Second
yFinURL = "https://query2.finance.yahoo.com"
batsURL = ""
)
var (
// LogLevel is the logging level for this library.
// 0: no logging
// 1: errors only
// 2: errors + informational (default)
// 3: errors + informational + debug
LogLevel = 0
// Logger controls how this library performs logging at a package level. It is useful
// to customise if you need it prefixed for your application to meet other
// requirements
Logger Printfer
// Private vars.
// -------------
httpClient *http.Client
backends Backends
yCrumb string
)
// SupportedBackend is an enumeration of supported api endpoints.
type SupportedBackend string
// Backends are the currently supported endpoints.
type Backends struct {
YFin, Bats Backend
mu sync.RWMutex
}
// BackendConfiguration is the internal implementation for making HTTP calls.
type BackendConfiguration struct {
Type SupportedBackend
URL string
HTTPClient *http.Client
}
// Backend is an interface for making calls against an api service.
// This interface exists to enable mocking for during testing if needed.
type Backend interface {
Call(path string, body *form.Values, ctx *context.Context, v interface{}) error
}
// SetHTTPClient overrides the default HTTP client.
// This is useful if you're running in a Google AppEngine environment
// where the http.DefaultClient is not available.
func SetHTTPClient(client *http.Client) {
httpClient = client
}
// NewBackends creates a new set of backends with the given HTTP client. You
// should only need to use this for testing purposes or on App Engine.
func NewBackends(httpClient *http.Client) *Backends {
return &Backends{
YFin: &BackendConfiguration{
YFinBackend, YFinURL, httpClient,
},
Bats: &BackendConfiguration{
BATSBackend, BATSURL, httpClient,
},
}
}
// GetBackend returns the currently used backend in the binding.
func GetBackend(backend SupportedBackend) Backend {
switch backend {
case YFinBackend:
backends.mu.RLock()
ret := backends.YFin
backends.mu.RUnlock()
if ret != nil {
return ret
}
backends.mu.Lock()
defer backends.mu.Unlock()
backends.YFin = &BackendConfiguration{backend, yFinURL, httpClient}
return backends.YFin
case BATSBackend:
backends.mu.RLock()
ret := backends.Bats
backends.mu.RUnlock()
if ret != nil {
return ret
}
backends.mu.Lock()
defer backends.mu.Unlock()
backends.Bats = &BackendConfiguration{backend, batsURL, httpClient}
return backends.Bats
}
return nil
}
// SetBackend sets the backend used in the binding.
func SetBackend(backend SupportedBackend, b Backend) {
switch backend {
case YFinBackend:
backends.YFin = b
case BATSBackend:
backends.Bats = b
}
}
// Call is the Backend.Call implementation for invoking market data APIs.
func (s *BackendConfiguration) Call(path string, form *form.Values, ctx *context.Context, v interface{}) error {
if form != nil && !form.Empty() {
path += "?" + form.Encode()
}
req, err := s.NewRequest("GET", path, ctx)
if err != nil {
return err
}
if err := s.Do(req, v); err != nil {
return err
}
return nil
}
// NewRequest is used by Call to generate an http.Request.
func (s *BackendConfiguration) NewRequest(method, path string, ctx *context.Context) (*http.Request, error) {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
path = s.URL + path
req, err := http.NewRequest(method, path, nil)
if err != nil {
if LogLevel > 0 {
Logger.Printf("Cannot create api request: %v\n", err)
}
return nil, err
}
if ctx != nil {
req = req.WithContext(*ctx)
}
return req, nil
}
func setBrowserHeaders(req *http.Request) {
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
}
func getYahooCrumb(client *http.Client) (string, error) {
req, err := http.NewRequest("GET", "https://finance.yahoo.com/", nil)
if err != nil {
return "", err
}
setBrowserHeaders(req)
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
req, err = http.NewRequest("GET", "https://query2.finance.yahoo.com/v1/test/getcrumb", nil)
if err != nil {
return "", err
}
setBrowserHeaders(req)
resp, err = client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}
// Do is used by Call to execute an API request and parse the response. It uses
// the backend's HTTP client to execute the request and unmarshals the response
// into v. It also handles unmarshaling errors returned by the API.
func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
if LogLevel > 1 {
Logger.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path)
}
start := time.Now()
if s.Type == YFinBackend {
if yCrumb == "" {
crumb, err := getYahooCrumb(s.HTTPClient)
if err != nil {
return fmt.Errorf("get yahoo crumb err: %w", err)
}
yCrumb = crumb
}
query := req.URL.Query()
query.Add("crumb", yCrumb)
req.URL.RawQuery = query.Encode()
}
res, err := s.HTTPClient.Do(req)
if LogLevel > 2 {
Logger.Printf("Completed in %v\n", time.Since(start))
}
if err != nil {
if LogLevel > 0 {
Logger.Printf("Request to api failed: %v\n", err)
}
return err
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
if LogLevel > 0 {
Logger.Printf("Cannot parse response: %v\n", err)
}
return err
}
if res.StatusCode >= 400 {
if LogLevel > 0 {
Logger.Printf("API error: %q\n", resBody)
}
return &RemoteError{
Msg: "error response recieved from upstream api",
StatusCode: res.StatusCode,
Body: string(resBody),
}
}
if LogLevel > 2 {
Logger.Printf("API response: %q\n", resBody)
}
if v != nil {
return json.Unmarshal(resBody, v)
}
return nil
}
type RemoteError struct {
Msg string
StatusCode int
Body string
}
func (e *RemoteError) Error() string {
return fmt.Sprintf("status: %d, detail: %s", e.StatusCode, e.Msg)
}