-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_viacep.go
49 lines (44 loc) · 1.12 KB
/
api_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
package cep
// https://viacep.com.br/
import (
"encoding/json"
"fmt"
"github.com/guionardo/go-gstools/tools"
)
type (
CEPAPIViaCep 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"`
ERRO string `json:"erro"`
}
)
func (c *CEPAPIViaCep) GetCEP(cep string) (*CEP, error) {
var model *ViaCepModel
body, err := tools.HttpGet(fmt.Sprintf("https://viacep.com.br/ws/%s/json/", tools.JustNumbers(cep)))
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &model); err != nil {
return nil, err
}
if len(model.ERRO) > 0 {
return nil, fmt.Errorf("CEP %s não encontrado", cep)
}
return &CEP{
CEP: tools.JustNumbers(model.CEP),
Logradouro: model.Logradouro,
Bairro: model.Bairro,
Municipio: model.Localidade,
UF: model.UF,
}, nil
}