-
Notifications
You must be signed in to change notification settings - Fork 4
/
rate_helper.go
76 lines (61 loc) · 1.67 KB
/
rate_helper.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
package exchangerate
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
"github.com/shopspring/decimal"
"github.com/tidwall/gjson"
)
const apiAddress = "https://free.currencyconverterapi.com/api/v6/convert?q=%s_%s&compact=y&apiKey=%s"
// RateHelper struct definition
type RateHelper struct {
APIKey string
Amount float32
FromCurrency string
ToCurrency []string
Result map[string]string
mutex sync.Mutex
}
// NewRateHelper creates and return a new rate helper struct
func NewRateHelper() *RateHelper {
return &RateHelper{}
}
// SaveResult saves rate into internal map
func (helper *RateHelper) SaveResult(key, value string) {
helper.mutex.Lock()
helper.Result[key] = value
helper.mutex.Unlock()
}
// Query function, queries different exchange rates and return the results
func (helper *RateHelper) Query() {
helper.Result = map[string]string{}
var wg sync.WaitGroup
for _, to := range helper.ToCurrency {
wg.Add(1)
go func(to string) {
defer wg.Done()
resp, err := http.Get(fmt.Sprintf(apiAddress, helper.FromCurrency, to, helper.APIKey))
if err != nil {
helper.SaveResult(to, "N/A")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
helper.SaveResult(to, "N/A")
return
}
valueStr := gjson.Get(string(body), fmt.Sprintf("%s_%s.val", helper.FromCurrency, to))
if helper.Amount != 1 {
value := decimal.NewFromFloat(valueStr.Float())
result := value.Mul(decimal.NewFromFloat(float64(helper.Amount)))
helper.SaveResult(to, result.StringFixedBank(2))
} else {
helper.SaveResult(to, valueStr.String())
}
}(to)
}
wg.Wait()
}