-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
40 lines (36 loc) · 893 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
package main
import (
"context"
"encoding/json"
"github.com/gvillela7/price/Model"
"io"
"log"
"net/http"
"os"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
defer cancel()
req, err := http.NewRequest("GET", "http://localhost:8080/cotacao", nil)
if err != nil {
log.Printf("Error: Wrong URL or server down -> %v\n", err)
}
req = req.WithContext(ctx)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error: maximum time exceeded -> %v\n", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var bid Model.Bid
if err := json.Unmarshal(body, &bid); err != nil {
log.Printf("Error: -> %v\n", err)
}
text := []byte("Dólar: " + bid.Bid)
err = os.WriteFile("cotacao.txt", text, 0644)
if err != nil {
log.Printf("Error: Could not write to file -> %v\n", err)
}
}