-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
60 lines (50 loc) · 1.49 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
const urlf = "https://openexchangerates.org/api/latest.json?app_id=%s"
// requestFixture loads fixture.json and returns the response.
func requestFixture(appId string) (res response, err error) {
file, err := ioutil.ReadFile("fixture.json")
if err != nil {
return res, errors.Wrap(err, "unable to read fixture.json file")
}
err = json.Unmarshal(file, &res)
if err != nil {
return res, errors.Wrap(err, "unable to unmarshal fixture.json")
}
return res, nil
}
// request makes a http request to the api using the api key.
// and returns the data.
// currently hardcoded to return a json file.
func request(appId string) (res response, err error) {
resp, err := http.Get(fmt.Sprintf(urlf, appId))
if err != nil {
return res, errors.Wrap(err, "http error")
}
if resp.StatusCode != 200 {
return res, errors.New(fmt.Sprintf("HTTP %d", resp.StatusCode))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, errors.Wrap(err, "error reading response body")
}
err = json.Unmarshal(body, &res)
if err != nil {
return res, errors.Wrap(err, "unable to unmarshal response body")
}
return res, nil
}
type response struct {
Disclaimer string `json:"disclaimer"`
License string `json:"license"`
Timestamp int `json:"timestamp"`
Base string `json:"base"`
Rates map[string]float64 `json:"rates"`
}