Skip to content

Instantly share code, notes, and snippets.

@slb350
Created July 15, 2017 06:26
Show Gist options
  • Select an option

  • Save slb350/ff3ef25b561ce358165573f207c33728 to your computer and use it in GitHub Desktop.

Select an option

Save slb350/ff3ef25b561ce358165573f207c33728 to your computer and use it in GitHub Desktop.
from flask import Flask
from flask import json
from flask import request
from boto3.session import Session
import json
import logging
app = Flask(__name__)
session = Session(
# aws_access_key_id='',
# aws_secret_access_key='',
region_name='us-east-1',
)
client = session.client('sqs')
response = client.get_queue_url(
QueueName='cmb-analytics'
)
url = response['QueueUrl']
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
log = logging.getLogger(__name__)
@app.route("/", methods = ['POST'])
def events():
import random
import string
uid = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5))
log.info("Request started for %s", uid)
try:
if request.headers['Content-Type'] == 'application/json':
# Retain the deserialized payload from the JSON
payload = request.json
# Fetch and add the profile ID to the payload
try:
profile_id = request.headers['profile_id']
payload['profile_id'] = profile_id
except (ValueError, KeyError):
pass
log.error('Client %s payload %s', request.headers.get('client'), payload)
# serialized payload to json
message = json.dumps(payload)
response = client.send_message(
QueueUrl=url,
MessageBody=message,
DelaySeconds=0,
)
return json.dumps({'success':True}), 200, {'Content-Type':'application/json'}
else:
log.error('received malform json')
return json.dumps({'success':False}), 415, {'Content-Type':'application/json'}
except:
log.exception('shits fucked')
finally:
log.info("Request ended for %s", uid)
if __name__ == "__main__":
app.run(host='0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment