Last active
August 29, 2015 14:02
-
-
Save seaders/5087cd2287d2b58d595f to your computer and use it in GitHub Desktop.
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
from . import app, db | |
def sendEmail(email_subject, email_from, email_to, email_reply_to, email_msg): | |
try: | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from boto.ses.connection import SESConnection | |
m = MIMEMultipart() | |
m['Subject'] = email_subject | |
m['From'] = email_from | |
m['To'] = email_to | |
m['Reply-To'] = email_reply_to | |
m.attach(MIMEText(email_msg.encode('utf-8'))) | |
r = SESConnection(app.config['AWS_KEY'], | |
app.config['AWS_SECRET']).\ | |
send_raw_email(source=m['From'], raw_message=m.as_string(), | |
destinations=m['To']) | |
return '[EMAIL SUCCESS]' + str(r) | |
except Exception as e: | |
app.logger.error(str(e)) | |
return '[EMAIL ERROR] ' + str(e) | |
import sys | |
import traceback | |
import json | |
from flask.views import MethodView | |
from flask import request | |
SUCCESS_MESSAGE = '{"message": "Success"}' | |
EMPTY_DICT = '{}' | |
def errorMessage(msg, extra=None): | |
return dict(error_message=msg, extra=str(extra)) | |
class MethodViewWrapper(MethodView): | |
def dispatch_request(self, *args, **kwargs): | |
try: | |
retval = MethodView.dispatch_request(self, *args, **kwargs) | |
except Exception as e: | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
l = [{'url': (request.url + | |
' (' + request.url_rule.endpoint + | |
' | ' + request.url_rule.rule + ')')}, | |
{'view_args': request.view_args}, | |
{'method': request.method}, | |
{'data': request.data}, | |
{'args': request.args}] | |
try: | |
l.append({'json': request.json}) | |
except Exception: | |
pass | |
l.extend([{'stacktrace': ''.join(traceback.format_exception | |
(exc_type, exc_value, | |
exc_traceback))}, | |
{'form': request.form}, | |
{'cookies': request.cookies}, | |
{'headers': dict([(key, str(val)) for key, val | |
in request.headers.environ.items()])}]) | |
msg = '' | |
for o in l: | |
k, v = o.items()[0] | |
s = str(v) | |
msg += '\n' + ('-' * 50) + '\n' + k.upper() + ' ' +\ | |
('""' if '' == s else s) | |
sendEmail('Backend {} Service Error'. | |
format(app.config['SM_SERVICE_NAME']), | |
app.config['ERROR_ADDRESS'], app.config['ERROR_ADDRESS'], | |
app.config['ERROR_ADDRESS'], msg) | |
raise e | |
istuple = type(retval) is tuple | |
if istuple: | |
retval = list(retval) | |
data = retval[0] | |
else: | |
data = retval | |
if not isinstance(data, basestring): | |
if data is None: | |
data = SUCCESS_MESSAGE | |
else: | |
data = json.dumps(data) | |
if istuple: | |
retval[0] = data | |
else: | |
retval = [data, 200, | |
{str('Content-Type'): str('application/json')}] | |
return tuple(retval) | |
class UserGameAPI(MethodViewWrapper): | |
'''User Game Resource | |
<game_id>/<user_id> | |
This resource represents a user's state within a game''' | |
def get(self, game_id, user_id): | |
return db.get_user_game(game_id, user_id) | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment