Skip to content

Commit

Permalink
Merge pull request #1 from WalterPaes/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
WalterPaes authored Sep 28, 2022
2 parents 8f2d652 + 2c41425 commit e651490
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 79 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# Go ViaCep

##### This lib was developed for get an adress data by zipcode using [ViaCep](https://viacep.com.br/) API

## Import and Usage

```
go get github.com/WalterPaes/go-viacep
```

### Create a New Integration
```go
// Parse zipcode to CEP type
zipcode := cep.CEP("01001-000")

// Create a new integration
integration := viacep.NewIntegration(zipcode)
```go
viaCep, err := NewViaCEP("01001-000")
```

### Get Address

```go
integration.GetAddress()
address, err := viaCep.GetAddress()
```

## :rocket: Technologies

This project was developed with the following technologies:

- [Go](https://golang.org/)
- [GoLand](https://www.jetbrains.com/go/?gclid=EAIaIQobChMI5-ug_OvG6gIVBgiRCh0GGARZEAAYASAAEgKOSPD_BwE)
- [ViaCep](https://viacep.com.br/)
- [Go](https://golang.org/)
- [GoLand](https://www.jetbrains.com/go/?gclid=EAIaIQobChMI5-ug_OvG6gIVBgiRCh0GGARZEAAYASAAEgKOSPD_BwE)
- [ViaCep](https://viacep.com.br/)

Made by [Walter Junior](https://www.linkedin.com/in/walter-paes/)
15 changes: 0 additions & 15 deletions cep/address.go

This file was deleted.

40 changes: 0 additions & 40 deletions tests/viacep_integration_test.go

This file was deleted.

66 changes: 51 additions & 15 deletions viacep/viacep.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,86 @@ package viacep

import (
"encoding/json"
"errors"
"fmt"
"github.com/WalterPaes/go-viacep/cep"
"io/ioutil"
"log"
"net/http"
"regexp"
)

// Integration represents viacep integration
type Integration struct {
cep cep.CEP
const (
cepNotFoundError = "CEP '%s' was not found"
invalidCepError = "'%s' is an invalid CEP"
)

type Address 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"`
Unidade string `json:"unidade,omitempty"`
IBGE string `json:"ibge,omitempty"`
Gia string `json:"gia,omitempty"`
}

type ViaCep struct {
cep string
}

func NewIntegration(cep cep.CEP) *Integration {
return &Integration{cep: cep}
func NewViaCEP(cep string) (*ViaCep, error) {
if err := validate(cep); err != nil {
log.Printf("[VIACEP] VALIDATION ERROR: %s \n", err.Error())
return nil, err
}

return &ViaCep{
cep: cep,
}, nil
}

func (vcs Integration) GetAddress() (*cep.Address, error) {
url := fmt.Sprintf("https://viacep.com.br/ws/%v/json/", vcs.cep)
func (vc ViaCep) GetAddress() (*Address, error) {
url := fmt.Sprintf("https://viacep.com.br/ws/%v/json/", vc.cep)
resp, err := http.Get(url)
if err != nil {
log.Printf("[VIACEP] ERROR: %s \n", err.Error())
log.Printf("[VIACEP] REQUEST ERROR: %s \n", err.Error())
return nil, err
}

if resp.StatusCode != http.StatusOK {
log.Printf("[VIACEP] ERROR: " + resp.Status)
return nil, errors.New(resp.Status)
notFoundError := fmt.Errorf(cepNotFoundError, vc.cep)
log.Printf("[VIACEP] NOT FOUND ERROR: " + notFoundError.Error())
return nil, notFoundError
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("[VIACEP] ERROR: %s \n", err.Error())
log.Printf("[VIACEP] BODY PARSE ERROR: %s \n", err.Error())
return nil, err
}

var address *cep.Address
var address *Address
err = json.Unmarshal(body, &address)
if err != nil {
log.Printf("[VIACEP] ERROR: %s \n", err.Error())
log.Printf("[VIACEP] JSON PARSE ERROR: %s \n", err.Error())
return nil, err
}

return address, err
}

func validate(cep string) error {
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
return err
}

cep = reg.ReplaceAllString(cep, "")
if len(cep) < 7 || len(cep) > 8 {
return fmt.Errorf(invalidCepError, cep)
}
return nil
}
77 changes: 77 additions & 0 deletions viacep/viacep_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package viacep

import (
"fmt"
"testing"
)

func TestViaCepIntegration(t *testing.T) {
t.Run("SUCESS: Get a address by cep", func(t *testing.T) {
cep := "01001-000"
viaCep, err := NewViaCEP(cep)
if err != nil {
t.Fatal("Errors was not expected!", err.Error())
}

address, err := viaCep.GetAddress()
if err != nil {
t.Fatal("Errors was not expected!", err.Error())
}

if address.CEP != cep {
t.Errorf("Was expected '%s', but got '%s'", cep, address.CEP)
}

expectedAddress := Address{
Logradouro: "Praça da Sé",
Complemento: "lado ímpar",
Bairro: "Sé",
Localidade: "São Paulo",
UF: "SP",
}

if address.Logradouro != expectedAddress.Logradouro {
t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Logradouro, address.Logradouro)
}

if address.Complemento != expectedAddress.Complemento {
t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Complemento, address.Complemento)
}

if address.Bairro != expectedAddress.Bairro {
t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Bairro, address.Bairro)
}

if address.Localidade != expectedAddress.Localidade {
t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Localidade, address.Localidade)
}

if address.UF != expectedAddress.UF {
t.Errorf("Was expected '%s', but got '%s'", expectedAddress.UF, address.UF)
}
})

t.Run("ERROR: Not found zipcode", func(t *testing.T) {
cep := "1234567"
viaCep, err := NewViaCEP(cep)
if err != nil {
t.Fatal("Errors was not expected!", err.Error())
}

_, err = viaCep.GetAddress()
notFoundError := fmt.Errorf(cepNotFoundError, cep)
if err.Error() != notFoundError.Error() {
t.Errorf("Was expected '%s', but got '%s'", notFoundError.Error(), err.Error())
}
})

t.Run("ERROR: Invalid zipcode", func(t *testing.T) {
cep := "12345"
_, err := NewViaCEP(cep)

invalidError := fmt.Errorf(invalidCepError, cep)
if err.Error() != invalidError.Error() {
t.Errorf("Was expected '%s', but got '%s'", invalidError.Error(), err.Error())
}
})
}

0 comments on commit e651490

Please sign in to comment.