-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
52 lines (42 loc) · 867 Bytes
/
client.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
package main
import (
"context"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
ctx := context.Background()
buscarCotacao(ctx)
}
func buscarCotacao(ctx context.Context) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()
client := http.Client{}
request, err := http.NewRequestWithContext(ctx, "GET", "http://localhost:8080/cotacao", nil)
if err != nil {
return nil, err
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
file, err := os.Create("cotacao.txt")
if err != nil {
panic(err)
}
defer file.Close()
n, err := file.Write(body)
if err != nil {
panic(err)
}
println(n, "Cotação salva com sucesso em cotacao.txt")
file.Close()
return body, nil
}