Skip to content

Instantly share code, notes, and snippets.

@marcieltorres
Created August 25, 2018 02:02
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 marcieltorres/418fac468c3a5cfa2d9a07f6ee7d71c2 to your computer and use it in GitHub Desktop.
Save marcieltorres/418fac468c3a5cfa2d9a07f6ee7d71c2 to your computer and use it in GitHub Desktop.
Get data from ReceitaWS with the AWS Lambda Function
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