This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
histominute.go
146 lines (122 loc) · 3.52 KB
/
histominute.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
package cryptocomparego
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/lucazulian/cryptocomparego/context"
"github.com/pkg/errors"
)
const (
histominuteBasePath = "data/histominute"
)
// Get the history kline data of any cryptocurrency in any other currency that you need.
type HistominuteService interface {
Get(context.Context, *HistominuteRequest) (*HistominuteResponse, *Response, error)
}
type HistominuteServiceOp struct {
client *Client
}
var _ HistodayService = &HistodayServiceOp{}
type HistominuteResponse struct {
Response string `json:"Response"`
Message string `json:"Message"` // Error Message
Type int `json:"Type"`
Aggregated bool `json:"Aggregated"`
Data []Histominute `json:"Data"`
TimeTo int64 `json:"TimeTo"`
TimeFrom int64 `json:"TimeFrom"`
FirstValueInArray bool `json:"FirstValueInArray"`
ConversionType conversionType `json:"ConversionType"`
}
type Histominute struct {
Time int64 `json:"time"`
Close float64 `json:"close"`
High float64 `json:"high"`
Low float64 `json:"low"`
Open float64 `json:"open"`
VolumeFrom float64 `json:"volumefrom"`
VolumeTo float64 `json:"volumeto"`
}
type HistominuteRequest struct {
Fsym string
Tsym string
E string
ExtraParams string
Sign bool
TryConversion bool
Aggregate int // Not Used For Now
Limit int
ToTs int64
}
func NewHistominuteRequest(fsym string, tsym string, limitMinutes int, fromTime int64) *HistominuteRequest {
pr := HistominuteRequest{Fsym: fsym, Tsym: tsym}
pr.E = "CCCAGG"
pr.Sign = false
pr.TryConversion = true
pr.Aggregate = 1
if limitMinutes < 1 {
limitMinutes = 1
}
if limitMinutes > 2000 {
limitMinutes = 2000
}
pr.Limit = limitMinutes
pr.ToTs = fromTime
return &pr
}
func (hr *HistominuteRequest) FormattedQueryString(baseUrl string) string {
values := url.Values{}
if len(hr.Fsym) > 0 {
values.Add("fsym", hr.Fsym)
}
if len(hr.Tsym) > 0 {
values.Add("tsym", hr.Tsym)
}
if len(hr.E) > 0 {
values.Add("e", hr.E)
}
if len(hr.ExtraParams) > 0 {
values.Add("extraParams", hr.ExtraParams)
}
values.Add("sign", strconv.FormatBool(hr.Sign))
values.Add("tryConversion", strconv.FormatBool(hr.TryConversion))
values.Add("limit", strconv.FormatInt(int64(hr.Limit), 10))
if hr.ToTs >= 0 {
values.Add("toTs", strconv.FormatInt(int64(hr.ToTs), 10))
}
return fmt.Sprintf("%s?%s", baseUrl, values.Encode())
}
func (s *HistominuteServiceOp) Get(ctx context.Context, histominuteRequest *HistominuteRequest) (*HistominuteResponse, *Response, error) {
path := histodyBasePath
if histominuteRequest != nil {
path = histominuteRequest.FormattedQueryString(histominuteBasePath)
}
reqUrl := fmt.Sprintf("%s%s", s.client.MinURL.String(), path)
fmt.Println(reqUrl)
resp, err := http.Get(reqUrl)
res := Response{}
res.Response = resp
if err != nil {
return nil, &res, err
}
defer func() { resp.Body.Close() }()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, &res, err
}
if len(buf) <= 0 {
return nil, &res, errors.New("Empty response")
}
hr := HistominuteResponse{}
err = json.Unmarshal(buf, &hr)
if err != nil {
return nil, &res, errors.Wrap(err, fmt.Sprintf("JSON Unmarshal error, raw string is '%s'", string(buf)))
}
if hr.Response == "Error" {
return nil, &res, errors.New(hr.Message)
}
return &hr, &res, nil
}