Skip to content

Commit

Permalink
Merge pull request #11 from IgorHalfeld/feat/v2
Browse files Browse the repository at this point in the history
feat: v2
  • Loading branch information
IgorHalfeld authored Sep 9, 2022
2 parents 6150eaa + c952ff9 commit c98f2d4
Show file tree
Hide file tree
Showing 24 changed files with 380 additions and 195 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
run-example:
go run cmd/main.go

test:
APP_ENV=testing CONFIG_DIR=${PWD} go test -v -cover -count 1 -failfast ./...

Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,25 @@ import (
)

func main() {
address, _ := lagoinha.GetAddress("CEP_GOES_HERE")
fmt.Printf("Complete Address %v:", address)
// get amount of cep providers enabled
fmt.Println("Total amount of cep providers:", lagoinha.GetTotalAmountOfCepProviders())
chResp, chErr := lagoinha.GetAddress("04568000")

select {
case address := <-chResp:
fmt.Printf("Response: %+v\n", address)
case err := <-chErr:
fmt.Printf("Error: %+v\n", err)
}
}
```

você também pode setar uma api de preferência

```golang
chResp, chErr := lagoinha.GetAddress("04568000", &lagoinha.GetAddressOptions{
PreferenceForAPI: "ViaCEP",
})
```

logo by [@nelsonsecco](https://twitter.com/nelsonsecco)
21 changes: 21 additions & 0 deletions cmd/main.go
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)
}
}
16 changes: 0 additions & 16 deletions examples/app.go

This file was deleted.

41 changes: 41 additions & 0 deletions internal/entity/cep.go
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
}
51 changes: 51 additions & 0 deletions internal/service/apicep/apicep_impl.go
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
}
10 changes: 10 additions & 0 deletions internal/service/apicep/apicep_model.go
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"`
}
51 changes: 51 additions & 0 deletions internal/service/brasilapi/brasilapi_impl.go
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 structs/cep.go → ...rnal/service/brasilapi/brasilapi_model.go
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"`
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package services
package correios

import (
"bytes"
"encoding/xml"
"errors"
"net/http"

"github.com/igorhalfeld/lagoinha/structs"
"github.com/igorhalfeld/lagoinha/internal/entity"
)

// CorreiosService service
type CorreiosService interface {
Request(cep string) (*structs.Cep, error)
}

type correiosImpl struct{}
type CorreiosService struct{}

// NewCorreiosService creates a new instance
func NewCorreiosService() CorreiosService {
return &correiosImpl{}
func New() *CorreiosService {
return &CorreiosService{}
}

// Request - fetch data from correios api
func (cs *correiosImpl) Request(cep string) (*structs.Cep, error) {
func (cs *CorreiosService) Request(cep string) (*entity.Cep, error) {
const proxyURL = "https://proxier.now.sh/"
client := &http.Client{}

result := structs.CorreiosResponse{}
result := correiosResponse{}

url := proxyURL + "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl"
payload := `
Expand Down Expand Up @@ -62,12 +56,12 @@ func (cs *correiosImpl) Request(cep string) (*structs.Cep, error) {
return cs.formater(&result)
}

func (cs *correiosImpl) formater(r *structs.CorreiosResponse) (*structs.Cep, error) {
func (cs *CorreiosService) formater(r *correiosResponse) (*entity.Cep, error) {
if r == nil {
return nil, errors.New("Cep not found")
}

cep := &structs.Cep{
cep := &entity.Cep{
Cep: r.Body.Consult.Return.Cep,
City: r.Body.Consult.Return.City,
Neighborhood: r.Body.Consult.Return.Neighborhood,
Expand Down
17 changes: 17 additions & 0 deletions internal/service/correios/correios_model.go
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"`
}
7 changes: 7 additions & 0 deletions internal/service/service.go
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)
}
29 changes: 12 additions & 17 deletions services/viacep.go → internal/service/viacep/viacep_impl.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package services
package viacep

import (
"encoding/json"
"errors"
"net/http"

"github.com/igorhalfeld/lagoinha/structs"
"github.com/igorhalfeld/lagoinha/internal/entity"
"github.com/igorhalfeld/lagoinha/pkg/errors"
)

// ViaCepService service
type ViaCepService interface {
Request(cep string) (*structs.Cep, error)
}

type viaCepImpl struct {
type ViaCepService struct {
}

// NewViaCepService creates a new instance
func NewViaCepService() ViaCepService {
return &viaCepImpl{}
func New() *ViaCepService {
return &ViaCepService{}
}

// Request - fetch data from viacep api
func (vc *viaCepImpl) Request(cep string) (*structs.Cep, error) {
result := structs.ViaCepResponse{}
func (vc *ViaCepService) Request(cep string) (*entity.Cep, error) {
result := viaCepResponse{}

res, err := http.Get("https://viacep.com.br/ws/" + cep + "/json/")
if err != nil {
Expand All @@ -40,18 +35,18 @@ func (vc *viaCepImpl) Request(cep string) (*structs.Cep, error) {
return vc.formater(&result)
}

func (vc *viaCepImpl) formater(r *structs.ViaCepResponse) (*structs.Cep, error) {
func (vc *ViaCepService) formater(r *viaCepResponse) (*entity.Cep, error) {
if r == nil {
return nil, errors.New("Cep not found")
return nil, errors.CepNotFoundError
}

cep := &structs.Cep{
cep := &entity.Cep{
Cep: r.Cep,
City: r.City,
Neighborhood: r.Neighborhood,
State: r.State,
Street: r.Street,
Provider: "Viacep",
Provider: "ViaCEP",
}

return cep, nil
Expand Down
Loading

0 comments on commit c98f2d4

Please sign in to comment.