-
Notifications
You must be signed in to change notification settings - Fork 0
/
apicep.go
55 lines (46 loc) · 1.11 KB
/
apicep.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
package cepgo
import (
"encoding/json"
"fmt"
)
type ServiceApiCEP struct {
}
type 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 *ServiceApiCEP) Execute(cep string, ch chan<- *CEP, errCh chan<- error) {
var model *ApiCepModel
body, err := requester(fmt.Sprintf("https://ws.apicep.com/cep/%s.json", cep))
if err != nil {
errCh <- ErrorUnexpectedResponse
return
}
if err := json.Unmarshal(body, &model); err != nil {
errCh <- ErrorUnexpectedResponse
return
}
if !model.Ok && model.Status != 200 {
errCh <- ErrorUnexpectedResponse
return
}
cepNumber, err := ParseCEPString(model.Code)
if err != nil {
errCh <- ErrorUnexpectedResponse
return
}
ch <- &CEP{
Cep: cepNumber,
Street: model.Address,
Neighborhood: model.District,
City: model.City,
State: model.State,
}
}