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
/
Copy pathpricemulti.go
147 lines (116 loc) · 3.25 KB
/
pricemulti.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
package cryptocomparego
import (
"errors"
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"github.com/lucazulian/cryptocomparego/context"
)
const (
pricemultiBasePath = "data/pricemulti"
)
// Same as single API path but with multiple from symbols.
type PriceMultiService interface {
List(context.Context, *PriceMultiRequest) ([]PriceMulti, *Response, error)
}
type PriceMultiServiceOp struct {
client *Client
}
type PriceMulti struct {
Name string
Value []Price
}
var _ PriceMultiService = &PriceMultiServiceOp{}
type PriceMultiRequest struct {
Fsyms []string
Tsyms []string
E string
ExtraParams string
Sign bool
TryConversion bool
}
func NewPriceMultiRequest(fsyms []string, tsyms []string) *PriceMultiRequest {
pr := PriceMultiRequest{Fsyms: fsyms, Tsyms: tsyms}
pr.E = "CCCAGG"
pr.Sign = false
pr.TryConversion = true
return &pr
}
func (pr *PriceMultiRequest) FormattedQueryString(baseUrl string) string {
values := url.Values{}
if len(pr.Fsyms) > 0 {
values.Add("fsyms", strings.Join(pr.Fsyms, ","))
}
if len(pr.Tsyms) > 0 {
values.Add("tsyms", strings.Join(pr.Tsyms, ","))
}
if len(pr.E) > 0 {
values.Add("e", pr.E)
}
if len(pr.ExtraParams) > 0 {
values.Add("extraParams", pr.ExtraParams)
}
values.Add("sign", strconv.FormatBool(pr.Sign))
values.Add("tryConversion", strconv.FormatBool(pr.TryConversion))
return fmt.Sprintf("%s?%s", baseUrl, values.Encode())
}
//TODO try to remove Sorter duplication
type PriceMultiNameSorter []PriceMulti
func (a PriceMultiNameSorter) Len() int { return len(a) }
func (a PriceMultiNameSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a PriceMultiNameSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }
type priceMultiRoot map[string]interface{}
func (ds *priceMultiRoot) GetPrices() ([]PriceMulti, error) {
var pricesMulti []PriceMulti
for pKey, pValue := range *ds {
priceMulti := PriceMulti{}
priceMulti.Name = pKey
var prices []Price
for key, value := range pValue.(map[string]interface{}) {
price := Price{key, value.(float64)}
prices = append(prices, price)
}
sort.Sort(PriceNameSorter(prices))
priceMulti.Value = prices
pricesMulti = append(pricesMulti, priceMulti)
}
return pricesMulti, nil
}
func (ds *priceMultiRoot) HasError() error {
//TODO try to unmarshal with error struct
var priceMultiError error = nil
if val, ok := (*ds)["Response"]; ok {
if val == "Error" {
val, _ = (*ds)["Message"]
priceMultiError = errors.New(val.(string))
}
}
return priceMultiError
}
func (s *PriceMultiServiceOp) List(ctx context.Context, priceMultiRequest *PriceMultiRequest) ([]PriceMulti, *Response, error) {
path := pricemultiBasePath
if priceMultiRequest != nil {
path = priceMultiRequest.FormattedQueryString(pricemultiBasePath)
}
req, err := s.client.NewRequest(ctx, http.MethodGet, *s.client.MinURL, path, nil)
if err != nil {
return nil, nil, err
}
root := new(priceMultiRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if err := root.HasError(); err != nil {
return nil, resp, err
}
prices, err := root.GetPrices()
if err != nil {
return nil, resp, err
}
sort.Sort(PriceMultiNameSorter(prices))
return prices, resp, err
}