-
Notifications
You must be signed in to change notification settings - Fork 3
/
cryptocurrency.go
128 lines (111 loc) · 2.89 KB
/
cryptocurrency.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
package goiex
import (
"net/http"
"net/url"
)
// Cryptocurrency struct to interface with / endpoints
type Cryptocurrency struct {
iex
}
// CryptoBook struct
type CryptoBook struct {
Bids []struct {
Price string `json:"price"`
Size string `json:"size"`
Timestamp int64 `json:"timestamp"`
} `json:"bids"`
Asks []struct {
Price string `json:"price"`
Size string `json:"size"`
Timestamp int64 `json:"timestamp"`
} `json:"asks"`
}
// CryptoPrice struct
type CryptoPrice struct {
Symbol string `json:"symbol"`
Price float64 `json:"price,string"`
}
// CryptoQuote struct
type CryptoQuote struct {
Symbol string `json:"symbol"`
Sector string `json:"sector"`
CalculationPrice string `json:"calculationPrice"`
LatestPrice float64 `json:"latestPrice,string"`
LatestSource string `json:"latestSource"`
LatestUpdate int64 `json:"latestUpdate"`
LatestVolume float64 `json:"latestVolume,string"`
BidPrice float64 `json:"bidPrice,string"`
BidSize float64 `json:"bidSize,string"`
AskPrice float64 `json:"askPrice,string"`
AskSize float64 `json:"askSize,string"`
High float64 `json:"high,string"`
Low float64 `json:"low,string"`
PreviousClose float64 `json:"previousClose,string"`
}
// NewCryptocurrency returns new Cryptocurrency
func NewCryptocurrency(
token, version string,
base *url.URL,
httpClient *http.Client,
options ...IEXOption,
) *Cryptocurrency {
apiurl, err := url.Parse("crypto/")
if err != nil {
panic(err)
}
crypto := &Cryptocurrency{
iex: iex{
token: token,
version: version,
url: base,
apiurl: apiurl,
client: httpClient,
},
}
for _, option := range options {
err := option(&crypto.iex)
if err != nil {
return nil
}
}
return crypto
}
// Token return token string
func (c *Cryptocurrency) Token() string {
return c.token
}
// Version return version string
func (c *Cryptocurrency) Version() string {
return c.version
}
// URL return URL base
func (c *Cryptocurrency) URL() *url.URL {
return c.url
}
// APIURL return APIURL
func (c *Cryptocurrency) APIURL() *url.URL {
return c.apiurl
}
// Client return HTTP client
func (c *Cryptocurrency) Client() *http.Client {
return c.client
}
// Retry return Retry struct that implements Retryer
func (c *Cryptocurrency) Retry() *Retry {
return c.iex.Retry
}
// CryptoBook GET /crypto/{symbol}/book
func (c *Cryptocurrency) CryptoBook(symbol string) (cb *CryptoBook, err error) {
err = get(c, &cb, symbol+"/book", nil)
return
}
// CryptoPrice GET /crypto/{symbol}/price
func (c *Cryptocurrency) CryptoPrice(symbol string) (cp *CryptoPrice, err error) {
err = get(c, &cp, symbol+"/price", nil)
return
}
// CryptoQuote GET /crypto/{symbol}/quote
func (c *Cryptocurrency) CryptoQuote(symbol string) (cq *CryptoQuote, err error) {
err = get(c, &cq, symbol+"/quote", nil)
return
}