-
Notifications
You must be signed in to change notification settings - Fork 0
/
viacep.go
51 lines (44 loc) · 1.07 KB
/
viacep.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
package cepgo
import (
"encoding/json"
"fmt"
)
type (
ViaCEP struct{}
ViaCepModel struct {
CEP string `json:"cep"`
Logradouro string `json:"logradouro"`
Complemento string `json:"complemento"`
Bairro string `json:"bairro"`
Localidade string `json:"localidade"`
UF string `json:"uf"`
IBGE string `json:"ibge"`
GIA string `json:"gia"`
DDD string `json:"ddd"`
SIAFI string `json:"siafi"`
}
)
func (c *ViaCEP) Execute(cep string, ch chan<- *CEP, errCh chan<- error) {
var model *ViaCepModel
body, err := requester(fmt.Sprintf("https://viacep.com.br/ws/%s/json/", cep))
if err != nil {
errCh <- ErrorUnexpectedResponse
return
}
if err := json.Unmarshal(body, &model); err != nil {
errCh <- ErrorUnexpectedResponse
return
}
cepNumber, err := ParseCEPString(model.CEP)
if err != nil {
errCh <- ErrorUnexpectedResponse
return
}
ch <- &CEP{
Cep: cepNumber,
Street: model.Logradouro,
Neighborhood: model.Bairro,
City: model.Localidade,
State: model.UF,
}
}