Skip to content

Instantly share code, notes, and snippets.

@marcieltorres
Created August 25, 2018 02:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcieltorres/e3fb0403aa2728cab8dbd9f267dd71f5 to your computer and use it in GitHub Desktop.
Save marcieltorres/e3fb0403aa2728cab8dbd9f267dd71f5 to your computer and use it in GitHub Desktop.
ViaCEP: AWS Lambda Function para buscar endereço através do CEP
import json
import urllib
import re
def lambda_handler(event, context):
if 'cep' in event and _regex(event['cep']):
return json.loads(urllib.request.urlopen(_get_url_api(event['cep'])).read())
return json.loads("{\"erro\": true, \"mensagem\": \"Formato incorreto\"}")
def _get_url_api(cep):
return ('http://www.viacep.com.br/ws/{}/json'.format(_replace(cep)))
def _replace(str):
return str.replace("-", "").replace(" ", "")
def _regex(str):
return re.match('[0-9]{8}', _replace(str))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment