-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
79 lines (57 loc) · 2.17 KB
/
api.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
package main
import (
"encoding/json"
"errors"
"io"
"math/rand"
"net/http"
)
const BASE_API_URL string = "https://openapiv1.coinstats.app"
var API_KEYS = [1]string{"vohN0pzG3mjbLsVYXFjyL+soUlp5tnqvuJQo7X74sxY="}
func getCoinData(cointId string) (Crypto, error) {
url := BASE_API_URL + "/coins/" + cointId
response := apiCall(url)
var responseMap map[string]interface{}
json.Unmarshal(response, &responseMap)
if statusCode, ok := responseMap["statusCode"]; ok && statusCode != http.StatusOK {
return Crypto{}, errors.New(responseMap["message"].(string))
}
var crypto Crypto = Crypto{
ID: responseMap["id"].(string),
Name: responseMap["name"].(string),
Symbol: responseMap["symbol"].(string),
Price: responseMap["price"].(float64),
LogoURL: responseMap["icon"].(string),
PriceChangePercentageDay: responseMap["priceChange1d"].(float64),
Website: responseMap["websiteUrl"].(string),
MarketCap: responseMap["marketCap"].(float64),
MarketCapRank: int(responseMap["rank"].(float64)),
TotalSupply: responseMap["totalSupply"].(float64),
CirculatingSupply: responseMap["availableSupply"].(float64),
Volume: responseMap["volume"].(float64),
}
return crypto, nil
}
func getChart(coinId string, period string) ([][]float64, error) {
url := BASE_API_URL + "/coins/" + coinId + "/charts?period=" + period
response := apiCall(url)
var responseMap map[string]interface{}
json.Unmarshal(response, &responseMap)
if statusCode, ok := responseMap["statusCode"]; ok && statusCode != http.StatusOK {
return nil, errors.New(responseMap["message"].(string))
}
var chartData [][]float64
if err := json.Unmarshal(response, &chartData); err != nil {
return nil, err
}
return chartData, nil
}
func apiCall(url string) []byte {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("accept", "application/json")
req.Header.Add("X-API-KEY", API_KEYS[rand.Intn(len(API_KEYS))])
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
return body
}