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
/
histoday.go
163 lines (136 loc) · 3.81 KB
/
histoday.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
package cryptocomparego
import (
"fmt"
"github.com/pkg/errors"
"net/http"
"net/url"
"strconv"
"bytes"
"encoding/json"
"github.com/lucazulian/cryptocomparego/context"
"io"
"io/ioutil"
"time"
)
const (
histodyBasePath = "data/histoday"
)
// Get the history kline data of any cryptocurrency in any other currency that you need.
type HistodayService interface {
Get(context.Context, *HistodayRequest) ([]Histoday, *Response, error)
}
type HistodayServiceOp struct {
client *Client
}
var _ HistodayService = &HistodayServiceOp{}
type conversionType struct {
Type string `json:"type"`
ConversionSymbol string `json:"conversionSymbol"`
}
type histodayResp struct {
Response string `json:"Response"`
Message string `json:"Message"` // Error Message
Type int `json:"Type"`
Aggregated bool `json:"Aggregated"`
Data []Histoday `json:"Data"`
TimeTo int64 `json:"TimeTo"`
TimeFrom int64 `json:"TimeFrom"`
FirstValueInArray bool `json:"FirstValueInArray"`
ConversionType conversionType `json:"ConversionType"`
}
type Histoday 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 HistodayRequest struct {
Fsym string
Tsym string
E string
ExtraParams string
Sign bool
TryConversion bool
Aggregate int // Not Used For Now
Limit int
ToTs time.Time // Not Used For Now
AllData bool
}
func NewHistodayRequest(fsym string, tsym string, limitDays int, allData bool) *HistodayRequest {
pr := HistodayRequest{Fsym: fsym, Tsym: tsym}
pr.E = "CCCAGG"
pr.Sign = false
pr.TryConversion = true
pr.Aggregate = 1
if limitDays < 1 {
limitDays = 1
}
if limitDays > 2000 {
limitDays = 2000
}
pr.Limit = limitDays
pr.AllData = allData
return &pr
}
func (hr *HistodayRequest) 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))
values.Add("allData", strconv.FormatBool(hr.AllData))
return fmt.Sprintf("%s?%s", baseUrl, values.Encode())
}
func ReadAndAssignResponseBody(res *http.Response) (io.Reader, error) {
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
res.Body = ioutil.NopCloser(bytes.NewReader(buf))
return bytes.NewReader(buf), nil
}
func (s *HistodayServiceOp) Get(ctx context.Context, histodayRequest *HistodayRequest) ([]Histoday, *Response, error) {
path := histodyBasePath
if histodayRequest != nil {
path = histodayRequest.FormattedQueryString(histodyBasePath)
}
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 := histodayResp{}
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.Data, &res, nil
}