-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
134 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package viacep | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/wiliamvj/api-users-golang/config/env" | ||
) | ||
|
||
type ViaCepResponse 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"` | ||
IBGE string `json:"ibge"` | ||
GIA string `json:"gia"` | ||
DDD string `json:"ddd"` | ||
SIAFI string `json:"siafi"` | ||
} | ||
|
||
func GetCep(cep string) (*ViaCepResponse, error) { | ||
url := fmt.Sprintf("%s/%s/json", env.Env.ViaCepURL, cep) | ||
var viaCepResponse ViaCepResponse | ||
|
||
resp, err := http.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&viaCepResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if viaCepResponse.CEP == "" { | ||
return nil, fmt.Errorf("cep not found") | ||
} | ||
return &viaCepResponse, 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
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 |
---|---|---|
|
@@ -6,7 +6,8 @@ content-type: application/json | |
{ | ||
"name": "John Doe", | ||
"email": "[email protected]", | ||
"password": "12345678@" | ||
"password": "12345678@", | ||
"cep": "77132243" | ||
} | ||
|
||
### | ||
|
@@ -17,7 +18,8 @@ content-type: application/json | |
|
||
{ | ||
"name": "John Doe", | ||
"email": "[email protected]" | ||
"email": "[email protected]", | ||
"cep": "00000000" | ||
} | ||
|
||
### | ||
|
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
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