-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_cep.go
45 lines (41 loc) · 1.05 KB
/
api_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
package cep
import (
"encoding/json"
"fmt"
"github.com/guionardo/go-gstools/tools"
)
type (
CEPAPICep struct {
}
ApiCepModel struct {
Code string `json:"code"`
Address string `json:"address"`
District string `json:"district"`
City string `json:"city"`
State string `json:"state"`
StatusText string `json:"statusText"`
Status int `json:"status"`
Ok bool `json:"ok"`
Message string `json:"message"`
}
)
func (c *CEPAPICep) GetCEP(cep string) (*CEP, error) {
var model *ApiCepModel
body, err := tools.HttpGet(fmt.Sprintf("https://ws.apicep.com/cep/%s.json", tools.JustNumbers(cep)))
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &model); err != nil {
return nil, err
}
if model.Ok && model.Status == 200 {
return &CEP{
CEP: tools.JustNumbers(cep),
Logradouro: model.Address,
Bairro: model.District,
Municipio: model.City,
UF: model.State,
}, nil
}
return nil, fmt.Errorf("CEP %s não encontrado (%d %v)", cep, model.Status, model.Message)
}