/
alphavantage.go
87 lines (71 loc) · 1.9 KB
/
alphavantage.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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
alphaVantageHost = "www.alphavantage.co"
alphaVantageFunction = "TIME_SERIES_DAILY"
)
type stockMetadata struct {
Information string `json:"1. Information"`
Symbol string `json:"2. Symbol"`
LastRefreshed string `json:"3. Last Refreshed"`
OutputSize string `json:"4. Output Size"`
TimeZone string `json:"5. Time Zone"`
}
type stockTimeSeriesItem struct {
Open string `json:"1. open"`
High string `json:"2. high"`
Low string `json:"3. low"`
Close string `json:"4. close"`
Volume string `json:"5. volume"`
}
type stockTimeSeries struct {
Metadata *stockMetadata `json:"Meta Data"`
TimeSeries map[string]*stockTimeSeriesItem `json:"Time Series (Daily)"`
}
type alphaVantage struct {
apiKey string
httpClient *http.Client
}
func newAlphaVantage(apiKey string) *alphaVantage {
return &alphaVantage{
apiKey: apiKey,
httpClient: &http.Client{},
}
}
func (av *alphaVantage) timeSeriesDaily(
ctx context.Context, symbol string) (*stockTimeSeries, error) {
url := fmt.Sprintf(
"https://%s/query?function=%s&symbol=%s&apikey=%s",
alphaVantageHost,
alphaVantageFunction,
symbol,
av.apiKey,
)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed http.NewRequest: %w", err)
}
resp, err := av.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("failed av.httpClient.Do: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed io.ReadAll: %w", err)
}
if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("alphaVantage API error (%d): %s", resp.StatusCode, body)
}
ts := &stockTimeSeries{}
if err := json.Unmarshal(body, &ts); err != nil {
return nil, fmt.Errorf("json.Unmarshal: %w", err)
}
return ts, nil
}