Created
August 25, 2018 02:18
-
-
Save marcieltorres/e3fb0403aa2728cab8dbd9f267dd71f5 to your computer and use it in GitHub Desktop.
ViaCEP: AWS Lambda Function para buscar endereço através do CEP
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
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