-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_overuse.go
55 lines (44 loc) · 1017 Bytes
/
02_overuse.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
package disadvantages
import (
"encoding/json"
"io/ioutil"
"net/http"
)
const downstreamServer = "http://www.example.com"
// FetchRates rates from downstream service
type FetchRates struct{}
func (f *FetchRates) Fetch() ([]Rate, error) {
// build the URL from which to fetch the rates
url := downstreamServer + "/rates"
// build request
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
// fetch rates
response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
// read the content of the response
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
// convert JSON bytes to Go structs
out := &downstreamResponse{}
err = json.Unmarshal(data, out)
if err != nil {
return nil, err
}
return out.Rates, nil
}
// response format from the downstream service
type downstreamResponse struct {
Rates []Rate `json:"rates"`
}
type Rate struct {
Code string
Value float64
}