/
get_cep.go
83 lines (71 loc) · 1.84 KB
/
get_cep.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
package api
import (
"context"
"crypto/tls"
"encoding/json"
"github.com/kenesparta/fullcycle-distr-trace-span/config"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"io"
"net/http"
"strings"
"time"
"github.com/kenesparta/fullcycle-distr-trace-span/internal/temperature/dto"
"github.com/kenesparta/fullcycle-distr-trace-span/internal/temperature/entity"
)
var createCepEndpoint = func(baseUrl, cep string) string {
return strings.Join([]string{baseUrl, "ws", cep, "json"}, "/")
}
type CEPFromAPI struct {
cnf *config.Config
}
func NewCEPFromAPI(cnf *config.Config) *CEPFromAPI {
return &CEPFromAPI{
cnf: cnf,
}
}
func (cap *CEPFromAPI) Get(ctx context.Context, cep string) (entity.Location, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
hCtx := otel.GetTextMapPropagator().Extract(ctx, propagation.HeaderCarrier{})
tracer := otel.Tracer("serviceBGetCEP")
_, span := tracer.Start(hCtx, "service_b:get_CEP")
defer span.End()
defer cancel()
req, reqErr := http.NewRequestWithContext(
ctx,
http.MethodGet,
createCepEndpoint(cap.cnf.CEP.URL, cep),
nil,
)
if reqErr != nil {
return entity.Location{}, reqErr
}
client := &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
resp, doErr := client.Do(req)
if doErr != nil {
return entity.Location{}, doErr
}
defer resp.Body.Close()
bodyBytes, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return entity.Location{}, readErr
}
var location dto.LocationOut
if unmErr := json.Unmarshal(bodyBytes, &location); unmErr != nil {
return entity.Location{}, unmErr
}
if location.CEP == "" {
return entity.Location{}, entity.ErrCEPNotFound
}
return entity.Location{
Cep: location.CEP,
Localidade: location.Localidade,
}, nil
}