Created
August 25, 2018 02:02
-
-
Save marcieltorres/418fac468c3a5cfa2d9a07f6ee7d71c2 to your computer and use it in GitHub Desktop.
Get data from ReceitaWS with the AWS Lambda Function
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 'cnpj' in event and _regex(event['cnpj']): | |
return json.loads(urllib.request.urlopen(_getUrlApi(event['cnpj'])).read()) | |
return json.loads("{\"status\": \"error\", \"message\": \"Formato incorreto\"}") | |
def _getUrlApi(cnpj): | |
return ('https://www.receitaws.com.br/v1/cnpj/%s' % _replace(cnpj)) | |
def _replace(str): | |
return str.replace("-", "").replace("/", "").replace(".", "").replace(" ", "") | |
def _regex(str): | |
return re.match('[0-9]{14}', _replace(str)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment