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
|
// Package cryptocompare fetches the latest USD price of a given crpytocurrency from CryptoCompare
package cryptocompare
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"within.website/x/web"
_ "within.website/x/web/useragent"
)
func Get(symbol string, currencies []string) (map[string]float64, error) {
u, err := url.Parse("https://min-api.cryptocompare.com/data/price")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("fsym", symbol)
q.Set("tsyms", strings.Join(currencies, ","))
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, fmt.Errorf("cryptocompare: can't fetch result: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
defer resp.Body.Close()
result := make(map[string]float64)
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("cryptocompare: can't decode result: %w", err)
}
return result, nil
}
|