/
fixer.go
51 lines (39 loc) · 886 Bytes
/
fixer.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
package fixer
import (
"io/ioutil"
"net/http"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
const (
API = "https://api.fixer.io/latest"
)
// Fixer API data
type Fixer struct {
}
// New create new Fixer API data
func New() *Fixer {
return &Fixer{}
}
func (f *Fixer) GetRate(base string, quote string) (float64, error) {
url := API + "?base=" + strings.ToUpper(base) + "&symbols=" + strings.ToUpper(quote)
log.Debugf("Request url: %v", url)
resp, err := http.Get(url)
if err != nil {
return 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
log.Debugf("Response body: %v", string(body))
rateRes := gjson.GetBytes(body, "rates."+strings.ToUpper(quote)).String()
rate, err := strconv.ParseFloat(rateRes, 64)
if err != nil {
return 0, err
}
return rate, nil
}