-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
70 lines (57 loc) · 1.53 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
package main
import (
"net/url"
"net/http"
"fmt"
"encoding/json"
"time"
)
func APIGetAllPairs(url url.URL) ([]string, error) {
resp, err := http.Get(url.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if http.StatusOK != resp.StatusCode {
return nil, fmt.Errorf("invalid response status code, expected %v got $v", http.StatusOK, resp.Status)
}
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, fmt.Errorf("error decoding response body, error: %v, body: %v", err, resp.Body)
}
pairsData, ok := data["pairs"]
if !ok {
return nil, fmt.Errorf("no pairs returned")
}
var pairs []string
for k := range pairsData.(map[string]interface{}) {
pairs = append(pairs, k)
}
return pairs, nil
}
func APIGetStatsForPair(c1 chan RateTime, pair string, url url.URL) (error) {
resp, err := http.Get(url.String()+pair)
if err != nil {
return err
}
defer resp.Body.Close()
if http.StatusOK != resp.StatusCode {
return fmt.Errorf("invalid response status code, expected %v got $v", http.StatusOK, resp.Status)
}
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return fmt.Errorf("error decoding response body, error: %v, body: %v", err, resp.Body)
}
res, ok := data[pair]
if !ok {
return fmt.Errorf("no rate returned")
}
lastRate, ok := res.(map[string]interface{})["last"]
if !ok {
return fmt.Errorf("no last returned")
}
c1 <- RateTime{Rate{pair, lastRate.(float64)}, time.Now()}
return nil
}