Skip to content

Instantly share code, notes, and snippets.

@marcieltorres
marcieltorres / mapping.json
Last active January 19, 2018 11:03
API Gateway (AWS): Mapping Body Template (/url/{param})
{
"body" : $input.json('$'),
"headers": {
#foreach($header in $input.params().header.keySet())
"$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
#end
},
"method": "$context.httpMethod",
"params": {
#foreach($param in $input.params().path.keySet())
@marcieltorres
marcieltorres / git-revert.txt
Last active May 27, 2018 16:11
git revert to commit
git reset <commit id>
git reset --soft HEAD@{1}
git commit -m "Revert to <commit id>"
git reset --hard
@marcieltorres
marcieltorres / get-mx-records.js
Created August 25, 2018 01:58
nodeJS-get-mx-records-from-domain
var dns = require('dns');
function getMxRecords(domain){
return new Promise(function(resolve, reject) {
dns.resolveMx(domain, function (err, addresses) {
if (err) {
//console.log(err, err.stack)
resolve(null);
}
@marcieltorres
marcieltorres / aws-lambda-function-receitaws.py
Created August 25, 2018 02:02
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\"}")
@marcieltorres
marcieltorres / aws-lambda-function-send-mail.js
Created August 25, 2018 02:05
NodeJS: Sending mail with AWS Lambda Function
var aws = require('aws-sdk');
exports.handler = (event, context, callback) => {
var _returnMessage = "";
var _destinationAddress, _sourceAddress = '[email protected]';
var _defaultSubject = '[Subject]';
var _defaultMessageData = '[Empty Message]';
//params SES
@marcieltorres
marcieltorres / aws-lambda-s3-get-content-object.js
Created August 25, 2018 02:07
NodeJS: Getting the S3 object content with the AWS Lambda Function
var aws = require('aws-sdk');
var s3 = new aws.S3();
exports.handler = (event, context, callback) => {
//Verifica se o objeto existe (chamada através de evento no S3)
if ((event) && (event.Records) && (event.Records[0]) && (event.Records[0].s3) && (event.Records[0].s3.bucket) && (event.Records[0].s3.object)){
//Recupera informações de bucket e do objeto
var _bucket = event.Records[0].s3.bucket.name;
@marcieltorres
marcieltorres / aws-lambda-s3-validating-file.js
Created August 25, 2018 02:10
NodeJS: Validating s3 file with the AWS Lambda Function
//Using SDK
var aws = require('aws-sdk');
var s3 = new aws.S3();
//Define quais extensões serão aceitas no bucket
var _extensionsAllowed = ["txt", "csv"];
//Define tamanho máximo e mínimo que serão aceitos (em bytes)
var _minFileSize = 20; var _maxFileSize = (1024*1024*2);
@marcieltorres
marcieltorres / s3-aws-lambda-function-trigger-object.py
Created August 25, 2018 02:15
AWS Lambda function to trigger objects
import boto3
import re
s3 = boto3.client('s3')
def lambda_handler(event, context):
if 'Records' in event:
bucket_object = event['Records'][0]['s3']['bucket']['name']
@marcieltorres
marcieltorres / get_address_by_cep.py
Created August 25, 2018 02:18
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\"}")
from typing import Dict
from requests import Session
from requests.exceptions import RequestException
class BaseApiClient:
__slots__ = ['uri', 'timeout', 'headers', 'session']
uri: str
timeout: int