-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
91 lines (78 loc) · 2.04 KB
/
request.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package dollar
import (
"context"
"encoding/json"
"io"
"log"
"net/http"
"github.com/google/uuid"
"github.com/kenesparta/fullcycle-client-server-api/server/constants"
"github.com/kenesparta/fullcycle-client-server-api/server/utils"
)
func RequestDollarPrice(ctx context.Context) (*Conversion, error) {
ctx, cancel := context.WithTimeout(ctx, constants.MaxRequestTimeout)
defer cancel()
client := http.Client{Timeout: constants.MaxRequestTimeout}
request, reqErr := http.NewRequestWithContext(
ctx,
http.MethodGet,
constants.URLCotacao,
nil)
if reqErr != nil {
return nil, reqErr
}
request.Header.Set("Accept", "application/json")
response, respErr := client.Do(request)
if respErr != nil {
log.Printf("timeout error %v", respErr)
return nil, respErr
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Printf("close stream error %v", err)
return
}
}(response.Body)
readBody, readErr := io.ReadAll(response.Body)
if readErr != nil {
log.Printf("read error %v", readErr)
return nil, readErr
}
var conv Conversion
unmErr := json.Unmarshal(readBody, &conv)
if unmErr != nil {
return nil, unmErr
}
return &conv, nil
}
func SaveDollarPrice(ctx context.Context) ([]byte, error) {
conv, reqErr := RequestDollarPrice(ctx)
if reqErr != nil {
return nil, reqErr
}
conv.Id = uuid.New().String()
cotacao := Cotacao{
Id: conv.Id,
Code: conv.Data.Codein,
Codein: conv.Data.Codein,
Name: conv.Data.Name,
High: utils.StrToFloat(conv.Data.High),
Low: utils.StrToFloat(conv.Data.Low),
VarBid: utils.StrToFloat(conv.Data.VarBid),
PctChange: utils.StrToFloat(conv.Data.PctChange),
Bid: utils.StrToFloat(conv.Data.Bid),
Ask: utils.StrToFloat(conv.Data.Ask),
Timestamp: conv.Data.Timestamp,
CreateDate: conv.Data.CreateDate,
}
saveErr := Save(ctx, cotacao)
if saveErr != nil {
return nil, saveErr
}
convBytes, marshErr := json.Marshal(cotacao)
if marshErr != nil {
return nil, marshErr
}
return convBytes, nil
}