from flask import request
from werkzeug.exceptions import Forbidden
from werkzeug.useragents import UserAgent

from skylines.api import auth


def register(app):
    """
    :param flask.Flask app: a Flask app
    """

    from .errors import register as register_error_handlers
    from .airports import airports_blueprint
    from .airspace import airspace_blueprint
    from .mapitems import mapitems_blueprint
    from .waves import waves_blueprint

    @app.before_request
    def require_user_agent():
        """
        API requests require a ``User-Agent`` header
        """
        user_agent = request.user_agent
        assert isinstance(user_agent, UserAgent)

        if not user_agent.string:
            description = 'You don\'t have the permission to access the API with a User-Agent header.'
            raise Forbidden(description)

    app.before_request(auth.check)

    register_error_handlers(app)

    app.register_blueprint(airports_blueprint, url_prefix='/airports')
    app.register_blueprint(airspace_blueprint, url_prefix='/airspace')
    app.register_blueprint(mapitems_blueprint, url_prefix='/mapitems')
    app.register_blueprint(waves_blueprint, url_prefix='/mountain_wave_project')

from flask import request
from werkzeug.exceptions import Forbidden
from werkzeug.useragents import UserAgent

from skylines.api import auth


def register(app):
    """
    :param flask.Flask app: a Flask app
    """

    from .errors import register as register_error_handlers
    from .airports import airports_blueprint
    from .airspace import airspace_blueprint
    from .mapitems import mapitems_blueprint
    from .waves import waves_blueprint

    @app.before_request
    def require_user_agent():
        """
        API requests require a ``User-Agent`` header
        """
        user_agent = request.user_agent
        assert isinstance(user_agent, UserAgent)

        if not user_agent.string:
            description = 'You don\'t have the permission to access the API with a User-Agent header.'
            raise Forbidden(description)

    app.before_request(auth.check)

    @app.after_request
    def add_cors_headers(response):
        if 'Origin' in request.headers:
            response.headers.add('Access-Control-Allow-Origin', request.headers.get('Origin'))
            response.headers.add('Access-Control-Allow-Credentials', 'true')

            if 'Access-Control-Request-Methods' in request.headers:
                response.headers.add('Access-Control-Allow-Methods', request.headers.get('Access-Control-Request-Methods'))

            if 'Access-Control-Request-Headers' in request.headers:
                response.headers.add('Access-Control-Allow-Headers', request.headers.get('Access-Control-Request-Headers'))

        return response

    register_error_handlers(app)

    app.register_blueprint(airports_blueprint, url_prefix='/airports')
    app.register_blueprint(airspace_blueprint, url_prefix='/airspace')
    app.register_blueprint(mapitems_blueprint, url_prefix='/mapitems')
    app.register_blueprint(waves_blueprint, url_prefix='/mountain_wave_project')

Add CORS headers