-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from IgorHalfeld/feat/v2
feat: v2
- Loading branch information
Showing
24 changed files
with
380 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/igorhalfeld/lagoinha" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Total amount of cep providers:", lagoinha.GetTotalAmountOfCepProviders()) | ||
chResp, chErr := lagoinha.GetAddress("04568000", &lagoinha.GetAddressOptions{ | ||
PreferenceForAPI: "Apicep", | ||
}) | ||
|
||
select { | ||
case address := <-chResp: | ||
fmt.Printf("Response: %+v\n", address) | ||
case err := <-chErr: | ||
fmt.Printf("Error: %+v\n", err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package entity | ||
|
||
import ( | ||
"github.com/igorhalfeld/lagoinha/pkg/formater" | ||
"github.com/igorhalfeld/lagoinha/pkg/validator" | ||
) | ||
|
||
// Cep standard cep struct | ||
type Cep struct { | ||
Cep string `json:"cep"` | ||
Street string `json:"street"` | ||
Neighborhood string `json:"neighborhood"` | ||
City string `json:"city"` | ||
State string `json:"state"` | ||
Provider string `json:"provider"` | ||
} | ||
|
||
func (c *Cep) IsValid() bool { | ||
c.Cep = formater.RemoveSpecialCharacters(c.Cep) | ||
|
||
if c.Cep == "" || validator.ExceedsCepMaximumSize(c.Cep) { | ||
return false | ||
} | ||
|
||
c.Cep = formater.LeftPadWithZeros(c.Cep) | ||
|
||
return true | ||
} | ||
|
||
func (c *Cep) HasAllAddressInfo() bool { | ||
if c.Cep != "" && | ||
c.Street != "" && | ||
c.Neighborhood != "" && | ||
c.City != "" && | ||
c.State != "" && | ||
c.Provider != "" { | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package apicep | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/igorhalfeld/lagoinha/internal/entity" | ||
"github.com/igorhalfeld/lagoinha/pkg/errors" | ||
) | ||
|
||
type ApicepService struct{} | ||
|
||
func New() *ApicepService { | ||
return &ApicepService{} | ||
} | ||
|
||
// Request - fetch data from viacep api | ||
func (wn *ApicepService) Request(cep string) (*entity.Cep, error) { | ||
result := apicepResponse{} | ||
|
||
res, err := http.Get("https://ws.apicep.com/cep/" + cep + ".json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
err = json.NewDecoder(res.Body).Decode(&result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return wn.formater(&result) | ||
} | ||
|
||
func (wn *ApicepService) formater(r *apicepResponse) (*entity.Cep, error) { | ||
if r == nil { | ||
return nil, errors.CepNotFoundError | ||
} | ||
|
||
cep := &entity.Cep{ | ||
Cep: r.Code, | ||
City: r.City, | ||
Neighborhood: r.District, | ||
State: r.State, | ||
Street: r.Address, | ||
Provider: "Apicep", | ||
} | ||
|
||
return cep, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package apicep | ||
|
||
type apicepResponse struct { | ||
Status int `json:"status"` | ||
Code string `json:"code"` | ||
State string `json:"state"` | ||
City string `json:"city"` | ||
District string `json:"district"` | ||
Address string `json:"address"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package brasilapi | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/igorhalfeld/lagoinha/internal/entity" | ||
"github.com/igorhalfeld/lagoinha/pkg/errors" | ||
) | ||
|
||
type BrasilAPIService struct { | ||
} | ||
|
||
func New() *BrasilAPIService { | ||
return &BrasilAPIService{} | ||
} | ||
|
||
func (ba *BrasilAPIService) Request(cep string) (*entity.Cep, error) { | ||
result := brasilAPIResponse{} | ||
|
||
res, err := http.Get("https://brasilapi.com.br/api/cep/v1/" + cep) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
err = json.NewDecoder(res.Body).Decode(&result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ba.formater(&result) | ||
} | ||
|
||
func (ba *BrasilAPIService) formater(r *brasilAPIResponse) (*entity.Cep, error) { | ||
if r == nil { | ||
return nil, errors.CepNotFoundError | ||
} | ||
|
||
cep := &entity.Cep{ | ||
Cep: r.Cep, | ||
City: r.City, | ||
Neighborhood: r.Neighborhood, | ||
State: r.State, | ||
Street: r.Street, | ||
Provider: "BrasilAPI", | ||
} | ||
|
||
return cep, nil | ||
} |
13 changes: 6 additions & 7 deletions
13
structs/cep.go → ...rnal/service/brasilapi/brasilapi_model.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
package structs | ||
package brasilapi | ||
|
||
// Cep standard cep struct | ||
type Cep struct { | ||
type brasilAPIResponse struct { | ||
Cep string `json:"cep"` | ||
Street string `json:"street"` | ||
Neighborhood string `json:"neighborhood"` | ||
City string `json:"city"` | ||
State string `json:"state"` | ||
Provider string `json:"provider"` | ||
City string `json:"city"` | ||
Neighborhood string `json:"neighborhood"` | ||
Street string `json:"street"` | ||
Service string `json:"service"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package correios | ||
|
||
type correiosBody struct { | ||
Return struct { | ||
Cep string `xml:"cep"` | ||
State string `xml:"uf"` | ||
City string `xml:"cidade"` | ||
Neighborhood string `xml:"bairro"` | ||
Street string `xml:"end"` | ||
} `xml:"return"` | ||
} | ||
|
||
type correiosResponse struct { | ||
Body struct { | ||
Consult correiosBody `xml:"consultaCEPResponse"` | ||
} `xml:"Body"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package service | ||
|
||
import "github.com/igorhalfeld/lagoinha/internal/entity" | ||
|
||
type Provider interface { | ||
Request(cep string) (*entity.Cep, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.