forked from psanford/finance-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
109 lines (88 loc) · 2.42 KB
/
client.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
package future
import (
"context"
"strings"
finance "github.com/TraderWithPython/finance-go"
form "github.com/TraderWithPython/finance-go/form"
"github.com/TraderWithPython/finance-go/iter"
)
// Client is used to invoke quote APIs.
type Client struct {
B finance.Backend
}
func getC() Client {
return Client{finance.GetBackend(finance.YFinBackend)}
}
// Params carries a context and symbols information.
type Params struct {
finance.Params `form:"-"`
// Symbols are the symbols for which a
// quote is requested.
Symbols []string `form:"-"`
sym string `form:"symbols"`
}
// Iter is an iterator for a list of quotes.
// The embedded Iter carries methods with it;
// see its documentation for details.
type Iter struct {
*iter.Iter
}
// Future returns the most recent future
// visited by a call to Next.
func (i *Iter) Future() *finance.Future {
return i.Current().(*finance.Future)
}
// Get returns an Future quote that matches the parameters specified.
func Get(symbol string) (*finance.Future, error) {
i := List([]string{symbol})
if !i.Next() {
return nil, i.Err()
}
return i.Future(), nil
}
// List returns several quotes.
func List(symbols []string) *Iter {
return ListP(&Params{Symbols: symbols})
}
// ListP returns a quote iterator and requires a params
// struct as an argument.
func ListP(params *Params) *Iter {
return getC().ListP(params)
}
// ListP returns a quote iterator.
func (c Client) ListP(params *Params) *Iter {
if params.Context == nil {
ctx := context.TODO()
params.Context = &ctx
}
// Validate input.
// TODO: validate symbols..
if params == nil || len(params.Symbols) == 0 {
return &Iter{iter.NewE(finance.CreateArgumentError())}
}
params.sym = strings.Join(params.Symbols, ",")
body := &form.Values{}
form.AppendTo(body, params)
return &Iter{iter.New(body, func(b *form.Values) (interface{}, []interface{}, error) {
resp := response{}
err := c.B.Call(finance.YQuotePath, body, params.Context, &resp)
if err != nil {
err = finance.CreateRemoteError(err)
}
ret := make([]interface{}, len(resp.Inner.Result))
for i, v := range resp.Inner.Result {
ret[i] = v
}
if resp.Inner.Error != nil {
err = finance.CreateRemoteError(resp.Inner.Error)
}
return nil, ret, err
})}
}
// response is a yfin quote response.
type response struct {
Inner struct {
Result []*finance.Future `json:"result"`
Error *finance.YfinError `json:"error"`
} `json:"quoteResponse"`
}