Skip to content

Commit

Permalink
Merge pull request #13 from MarceloPetrucio/add-general-improvements
Browse files Browse the repository at this point in the history
General Improvements
  • Loading branch information
IgorHalfeld authored May 21, 2024
2 parents c98f2d4 + 2e4b126 commit e2458e7
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 25 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
run-example:
go run cmd/main.go
run-example-async:
go run cmd/example_async/main.go

run-example-sync:
go run cmd/example_sync/main.go

test:
APP_ENV=testing CONFIG_DIR=${PWD} go test -v -cover -count 1 -failfast ./...
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions cmd/example_sync/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"log"

"github.com/igorhalfeld/lagoinha"
)

func main() {
fmt.Println("Total amount of cep providers:", lagoinha.GetTotalAmountOfCepProviders())
address, err := lagoinha.GetAddressSync("15809240", &lagoinha.GetAddressOptions{
PreferenceForAPI: "Apicep",
})

if err != nil {
log.Fatalf("Error: %+v\n", err)
}

fmt.Printf("Response: %+v\n", address)
}
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
module github.com/igorhalfeld/lagoinha

go 1.12

require (
github.com/fatih/color v1.10.0 // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jpoles1/gopherbadger v2.4.0+incompatible // indirect
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
)
29 changes: 28 additions & 1 deletion internal/service/apicep/apicep_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apicep
import (
"encoding/json"
"net/http"
"time"

"github.com/igorhalfeld/lagoinha/internal/entity"
"github.com/igorhalfeld/lagoinha/pkg/errors"
Expand All @@ -18,18 +19,44 @@ func New() *ApicepService {
func (wn *ApicepService) Request(cep string) (*entity.Cep, error) {
result := apicepResponse{}

res, err := http.Get("https://ws.apicep.com/cep/" + cep + ".json")
client := &http.Client{
Timeout: time.Second * 2,
}

res, err := client.Get("https://ws.apicep.com/cep/" + cep + ".json")
if err != nil {
return nil, err
}

defer res.Body.Close()

if res.StatusCode != 200 {
switch res.StatusCode {
case http.StatusTooManyRequests:
return nil, errors.TooManyRequestsError
case http.StatusInternalServerError:
return nil, errors.InternalServerError
default:
return nil, errors.CepNotFoundError
}
}

err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return nil, err
}

if result.Status != 200 {
switch result.Status {
case http.StatusTooManyRequests:
return nil, errors.TooManyRequestsError
case http.StatusInternalServerError:
return nil, errors.InternalServerError
default:
return nil, errors.CepNotFoundError
}
}

return wn.formater(&result)
}

Expand Down
18 changes: 17 additions & 1 deletion internal/service/brasilapi/brasilapi_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package brasilapi
import (
"encoding/json"
"net/http"
"time"

"github.com/igorhalfeld/lagoinha/internal/entity"
"github.com/igorhalfeld/lagoinha/pkg/errors"
Expand All @@ -18,13 +19,28 @@ func New() *BrasilAPIService {
func (ba *BrasilAPIService) Request(cep string) (*entity.Cep, error) {
result := brasilAPIResponse{}

res, err := http.Get("https://brasilapi.com.br/api/cep/v1/" + cep)
client := &http.Client{
Timeout: time.Second * 2,
}

res, err := client.Get("https://brasilapi.com.br/api/cep/v1/" + cep)
if err != nil {
return nil, err
}

defer res.Body.Close()

if res.StatusCode != 200 {
switch res.StatusCode {
case http.StatusTooManyRequests:
return nil, errors.TooManyRequestsError
case http.StatusInternalServerError:
return nil, errors.InternalServerError
default:
return nil, errors.CepNotFoundError
}
}

err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return nil, err
Expand Down
21 changes: 18 additions & 3 deletions internal/service/correios/correios_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package correios
import (
"bytes"
"encoding/xml"
"errors"

"net/http"
"time"

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

type CorreiosService struct{}
Expand All @@ -18,7 +20,9 @@ func New() *CorreiosService {
// Request - fetch data from correios api
func (cs *CorreiosService) Request(cep string) (*entity.Cep, error) {
const proxyURL = "https://proxier.now.sh/"
client := &http.Client{}
client := &http.Client{
Timeout: time.Second * 2,
}

result := correiosResponse{}

Expand Down Expand Up @@ -48,6 +52,17 @@ func (cs *CorreiosService) Request(cep string) (*entity.Cep, error) {

defer res.Body.Close()

if res.StatusCode != 200 {
switch res.StatusCode {
case http.StatusTooManyRequests:
return nil, errors.TooManyRequestsError
case http.StatusInternalServerError:
return nil, errors.InternalServerError
default:
return nil, errors.CepNotFoundError
}
}

err = xml.NewDecoder(res.Body).Decode(&result)
if err != nil {
return nil, err
Expand All @@ -58,7 +73,7 @@ func (cs *CorreiosService) Request(cep string) (*entity.Cep, error) {

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

cep := &entity.Cep{
Expand Down
18 changes: 17 additions & 1 deletion internal/service/viacep/viacep_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package viacep
import (
"encoding/json"
"net/http"
"time"

"github.com/igorhalfeld/lagoinha/internal/entity"
"github.com/igorhalfeld/lagoinha/pkg/errors"
Expand All @@ -20,13 +21,28 @@ func New() *ViaCepService {
func (vc *ViaCepService) Request(cep string) (*entity.Cep, error) {
result := viaCepResponse{}

res, err := http.Get("https://viacep.com.br/ws/" + cep + "/json/")
client := &http.Client{
Timeout: time.Second * 2,
}

res, err := client.Get("https://viacep.com.br/ws/" + cep + "/json/")
if err != nil {
return nil, err
}

defer res.Body.Close()

if res.StatusCode != 200 {
switch res.StatusCode {
case http.StatusTooManyRequests:
return nil, errors.TooManyRequestsError
case http.StatusInternalServerError:
return nil, errors.InternalServerError
default:
return nil, errors.CepNotFoundError
}
}

err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package widenet

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

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

type WidenetService struct{}
Expand All @@ -18,13 +19,28 @@ func New() *WidenetService {
func (wn *WidenetService) Request(cep string) (*entity.Cep, error) {
result := widenetResponse{}

res, err := http.Get("http://apps.widenet.com.br/busca-cep/api/cep/" + cep + ".json")
client := &http.Client{
Timeout: time.Second * 2,
}

res, err := client.Get("http://apps.widenet.com.br/busca-cep/api/cep/" + cep + ".json")
if err != nil {
return nil, err
}

defer res.Body.Close()

if res.StatusCode != 200 {
switch res.StatusCode {
case http.StatusTooManyRequests:
return nil, errors.TooManyRequestsError
case http.StatusInternalServerError:
return nil, errors.InternalServerError
default:
return nil, errors.CepNotFoundError
}
}

err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return nil, err
Expand All @@ -35,7 +51,7 @@ func (wn *WidenetService) Request(cep string) (*entity.Cep, error) {

func (wn *WidenetService) formater(r *widenetResponse) (*entity.Cep, error) {
if r == nil {
return nil, errors.New("Cep not found")
return nil, errors.CepNotFoundError
}

cep := &entity.Cep{
Expand Down
Loading

0 comments on commit e2458e7

Please sign in to comment.