-
Notifications
You must be signed in to change notification settings - Fork 6
/
RestClient.go
147 lines (109 loc) · 2.95 KB
/
RestClient.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 golang_forex_quotes
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
func CreateRestClient(apiKey string) RestClient {
return RestClient{
ApiKey: apiKey,
}
}
func fetch(query string, apiKey string) ([]byte, error) {
if strings.Count(query, "") > 7683 {
// println("String count", strings.Count(query, ""))
// err := errors.New("No more than 865 pairs or 1730 curriencies")
err := errors.New("No more than 949 pairs or 1898 curriencies")
return nil, err
} else {
response, e := http.Get("https://api.1forge.com/" + query + "&api_key=" + apiKey)
if e != nil {
return nil, e
}
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
}
}
func unableToUnmarshal(response []byte) error {
apiError := RestError{}
e := json.Unmarshal(response, &apiError)
if e != nil {
return e
}
return errors.New("forex.1forge.com rejected your request: " + apiError.Message)
}
func (u UnlimitedQuota) toQuota() Quota {
return Quota{
QuotaUsed: u.QuotaUsed,
QuotaLimit: 0,
QuotaRemaining: 0,
HoursUntilReset: u.HoursUntilReset,
}
}
func (c RestClient) GetQuota() (Quota, error) {
result, e := fetch("quota?cache=false", c.ApiKey)
quota := Quota{}
if e != nil {
return quota, e
}
//Able to unmarshal to Quota
if json.Unmarshal(result, "a) == nil {
return quota, nil
}
unlimitedQuota := UnlimitedQuota{}
//Unable to unmarshal to Quota, try UnlimitedQuota
if json.Unmarshal(result, &unlimitedQuota) == nil {
return unlimitedQuota.toQuota(), nil
}
//Unable to unmarshal at all
return quota, unableToUnmarshal(result)
}
func (c RestClient) GetSymbols() ([]string, error) {
result, e := fetch("symbols?cache=false", c.ApiKey)
if e != nil {
return nil, e
}
symbolList := []string{}
if json.Unmarshal(result, &symbolList) != nil {
return symbolList, unableToUnmarshal(result)
}
return symbolList, nil
}
func (c RestClient) GetQuotes(symbols []string) ([]Quote, error) {
result, e := fetch("quotes?pairs="+strings.Join(symbols, ","), c.ApiKey)
s := string(result)
// println(s)
if e != nil {
return nil, e
}
quotes := []Quote{}
if json.Unmarshal(result, "es) != nil {
return quotes, unableToUnmarshal(result)
}
return quotes, nil
}
func (c RestClient) Convert(from string, to string, quantity int) (ConversionResult, error) {
result, e := fetch("convert?from="+from+"&to="+to+"&quantity="+strconv.Itoa(quantity), c.ApiKey)
conversion := ConversionResult{}
if e != nil {
return conversion, e
}
if json.Unmarshal(result, &conversion) != nil {
return conversion, unableToUnmarshal(result)
}
return conversion, nil
}
func (c RestClient) GetMarketStatus() (MarketStatus, error) {
result, e := fetch("market_status?cache=false", c.ApiKey)
marketStatus := MarketStatus{}
if e != nil {
return marketStatus, e
}
if json.Unmarshal(result, &marketStatus) != nil {
return marketStatus, unableToUnmarshal(result)
}
return marketStatus, nil
}