-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
65 lines (54 loc) · 1.03 KB
/
Copy pathclient.go
File metadata and controls
65 lines (54 loc) · 1.03 KB
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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"time"
)
type Bid struct {
Bid string `json:"bid"`
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/quotation", nil)
if err != nil {
panic(err)
}
dollar := "USD"
real := "BRL"
// query args
args := req.URL.Query()
args.Add("src", dollar)
args.Add("dst", real)
// assign encoded query string to http request
req.URL.RawQuery = args.Encode()
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
saveToFile(body)
}
func saveToFile(data []byte) {
f, err := os.Create("quotation.txt")
if err != nil {
panic(err)
}
var b Bid
err = json.Unmarshal(data, &b)
if err != nil {
panic(err)
}
_, err = f.WriteString("Dollar: " + b.Bid)
if err != nil {
panic(err)
}
f.Close()
}