Skip to content

Instantly share code, notes, and snippets.

@haskellcamargo
Created February 2, 2018 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haskellcamargo/701605c7f62d3b421453e7766311bd2f to your computer and use it in GitHub Desktop.
Save haskellcamargo/701605c7f62d3b421453e7766311bd2f to your computer and use it in GitHub Desktop.
#include 'protheus.ch'
Class Endereco
Data CEP As Character
Data Estado As Character
Data Cidade As Character
Data Bairro As Character
Data TipoLogradouro As Character
Data Logradouro As Character
Method New( cCEP ) Constructor
EndClass
Method New( cCEP ) Class Endereco
::CEP := cCEP
Return Self
Function ConsultarCEP( cCEP )
Local oEndereco
Local cJSON
Local oResult
Local cWebService := 'http://cep.republicavirtual.com.br/web_cep.php'
cJSON := HttpGet( cWebService, 'cep=' + Escape( cCEP ) + '&formato=json' )
If FWJsonDeserialize( cJSON, @oResult ) .And. oResult:Resultado != '0'
oEndereco := Endereco():New( cCEP )
oEndereco:Estado := oResult:UF
oEndereco:Cidade := oResult:Cidade
oEndereco:Bairro := oResult:Bairro
oEndereco:TipoLogradouro := oResult:Tipo_Logradouro
oEndereco:Logradouro := oResult:Logradouro
EndIf
Return oEndereco
User Function TestCEP
Local aCEPs := { '89218-180', '8859000', '89223-002', 'EITA' }
Local oEndereco
Local nIndex
For nIndex := 1 To Len( aCEPs )
oEndereco := ConsultarCEP( aCEPs[ nIndex ] )
If oEndereco != Nil
ConOut( '--------------------------' )
ConOut( 'Estado: ' + oEndereco:Estado )
ConOut( 'Cidade: ' + oEndereco:Cidade )
ConOut( 'Bairro: ' + oEndereco:Bairro )
ConOut( 'Tipo de Logradouro: ' + oEndereco:TipoLogradouro )
ConOut( 'Logradouro: ' + oEndereco:Logradouro )
EndIf
Next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment