-
Notifications
You must be signed in to change notification settings - Fork 2
/
latest.go
53 lines (44 loc) · 949 Bytes
/
latest.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
package goforex
import (
"encoding/json"
"io/ioutil"
"net/url"
"strings"
)
type Latest struct {
Base string `json:"base"`
Date string `json:"date"`
Rates map[string]float64 `json:"rates"`
}
func (c *Client) Latest(params ...map[string]string) (Latest, error) {
var latest Latest
u, err := url.Parse(BaseUrl)
if err != nil {
return latest, err
}
u.Path = "latest"
if len(params) > 0 {
q := u.Query()
if params[0]["base"] != "" {
q.Set("base", strings.ToUpper(params[0]["base"]))
}
if params[0]["symbols"] != "" {
q.Set("symbols", strings.ToUpper(params[0]["symbols"]))
}
u.RawQuery = q.Encode()
}
resp, err := c.Conn.Get(u.String())
if err != nil {
return latest, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return latest, err
}
err = json.Unmarshal(body, &latest)
if err != nil {
return latest, err
}
return latest, nil
}