forked from piquette/finance-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finance.go
236 lines (197 loc) · 5.55 KB
/
finance.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
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
package finance
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/piquette/finance-go/form"
)
// 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)
}
const (
// YFinBackend is a constant representing the yahoo service backend.
YFinBackend SupportedBackend = "yahoo"
// YFinURL is the URL of the yahoo service backend.
YFinURL string = "https://query2.finance.yahoo.com"
// 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{Timeout: defaultHTTPTimeout}
backends Backends
)
// 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
}
// 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()
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 CreateRemoteErrorS("error response recieved from upstream api")
}
if LogLevel > 2 {
Logger.Printf("API response: %q\n", resBody)
}
if v != nil {
return json.Unmarshal(resBody, v)
}
return nil
}