From 93f526f1d12933204315cbdd2b85c0a40ee78c22 Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Apr 23 2018 11:03:43 +0000 Subject: Add CORS headers on one place Removes the need to call to insert_headers() after handling requests. --- diff --git a/waiverdb/api_v1.py b/waiverdb/api_v1.py index 41d7155..fce492e 100644 --- a/waiverdb/api_v1.py +++ b/waiverdb/api_v1.py @@ -10,7 +10,7 @@ from sqlalchemy.sql.expression import func, cast from waiverdb import __version__ from waiverdb.models import db, Waiver -from waiverdb.utils import reqparse_since, json_collection, jsonp, insert_headers +from waiverdb.utils import reqparse_since, json_collection, jsonp from waiverdb.fields import waiver_fields import waiverdb.auth @@ -155,8 +155,7 @@ class WaiversResource(Resource): Waiver.testcase) query = query.filter(Waiver.id.in_(subquery)) query = query.order_by(Waiver.timestamp.desc()) - return insert_headers( - json_collection(query, args['page'], args['limit'])) + return json_collection(query, args['page'], args['limit']) @jsonp @marshal_with(waiver_fields) @@ -280,7 +279,7 @@ class WaiverResource(Resource): :statuscode 404: No waiver exists with that ID. """ try: - return insert_headers(Waiver.query.get_or_404(waiver_id)) + return Waiver.query.get_or_404(waiver_id) except Exception as NotFound: raise type(NotFound)('Waiver not found') @@ -403,7 +402,7 @@ class GetWaiversBySubjectsAndTestcases(Resource): query = query.filter(Waiver.id.in_(subquery)) query = query.order_by(Waiver.timestamp.desc()) - return insert_headers({'data': marshal(query.all(), waiver_fields)}) + return {'data': marshal(query.all(), waiver_fields)} class AboutResource(Resource): @@ -430,8 +429,7 @@ class AboutResource(Resource): :statuscode 200: Currently running waiverdb software version and authentication are returned. """ - return insert_headers( - {'version': __version__, 'auth_method': current_app.config['AUTH_METHOD']}) + return {'version': __version__, 'auth_method': current_app.config['AUTH_METHOD']} # set up the Api resource routing here diff --git a/waiverdb/app.py b/waiverdb/app.py index b7f8405..736f18c 100644 --- a/waiverdb/app.py +++ b/waiverdb/app.py @@ -7,7 +7,7 @@ try: except ImportError: from urlparse import urlparse, urlunsplit -from flask import Flask +from flask import Flask, current_app from flask_migrate import Migrate from sqlalchemy import event import requests @@ -58,6 +58,18 @@ def populate_db_config(app): app.config['SQLALCHEMY_DATABASE_URI'] = dburi +def insert_headers(response): + """ Insert the CORS headers for the give response if there are any + configured for the application. + """ + cors_url = current_app.config.get('CORS_URL') + if cors_url: + response.headers['Access-Control-Allow-Origin'] = cors_url + response.headers['Access-Control-Allow-Headers'] = 'Content-Type' + response.headers['Access-Control-Allow-Method'] = 'POST, OPTIONS' + return response + + # applicaiton factory http://flask.pocoo.org/docs/0.12/patterns/appfactories/ def create_app(config_obj=None): app = Flask(__name__) @@ -89,6 +101,9 @@ def create_app(config_obj=None): app.register_blueprint(api_v1, url_prefix="/api/v1.0") app.add_url_rule('/healthcheck', view_func=healthcheck) register_event_handlers(app) + + app.after_request(insert_headers) + return app diff --git a/waiverdb/utils.py b/waiverdb/utils.py index fbd5e8c..b760503 100644 --- a/waiverdb/utils.py +++ b/waiverdb/utils.py @@ -75,7 +75,7 @@ def json_error(error): response = jsonify(message=str(error)) response.status_code = 500 - return insert_headers(response) + return response def jsonp(func): @@ -117,17 +117,3 @@ def stomp_connection(): else: raise RuntimeError('stomp was configured to publish messages, ' 'but STOMP_CONFIGS is not configured') - - -def insert_headers(response): - """ Insert the CORS headers for the give reponse if there are any - configured for the application. - """ - if isinstance(response, dict): - response = jsonify(response) - if current_app.config.get('CORS_URL'): - response.headers['Access-Control-Allow-Origin'] = \ - current_app.config['CORS_URL'] - response.headers['Access-Control-Allow-Headers'] = 'Content-Type' - response.headers['Access-Control-Allow-Method'] = 'POST, OPTIONS' - return response