# -*- coding: utf-8 -*-
"""
    eve-swagger.swagger
    ~~~~~~~~~~~~~~~~~~~
    swagger.io extension for Eve-powered REST APIs.

    :copyright: (c) 2015 by Nicola Iarocci.
    :license: BSD, see LICENSE for more details.
"""
from collections import Mapping
from flask import Blueprint, jsonify

from eve_swagger import OrderedDict
from .definitions import definitions
from .objects import info, host, base_path, schemes, consumes, produces, \
    parameters, responses, security_definitions, security, tags, \
    external_docs
from .paths import paths


swagger = Blueprint('eve_swagger', __name__)
swagger.additional_documentation = OrderedDict()


def add_documentation(doc):
    _nested_update(swagger.additional_documentation, doc)


@swagger.route('/api-docs')
def index():
    def node(parent, key, value):
        if value:
            parent[key] = value

    root = OrderedDict()
    root['swagger'] = '2.0'
    node(root, 'info', info())
    node(root, 'host', host())
    node(root, 'basePath', base_path())
    node(root, 'schemes', schemes())
    node(root, 'consumes', consumes())
    node(root, 'produces', produces())
    node(root, 'paths', paths())
    node(root, 'definitions', definitions())
    node(root, 'parameters', parameters())
    node(root, 'responses', responses())
    node(root, 'securityDefinitions', security_definitions())
    node(root, 'security', security())
    node(root, 'tags', tags())
    node(root, 'externalDocs', external_docs())

    _nested_update(root, swagger.additional_documentation)

    return jsonify(root)


def _nested_update(orig_dict, new_dict):
    for key, val in new_dict.items():
        if isinstance(val, Mapping):
            tmp = _nested_update(orig_dict.get(key, {}), val)
            orig_dict[key] = tmp
        elif isinstance(val, list):
            orig_dict[key] = (orig_dict.get(key, []) + val)
        else:
            orig_dict[key] = new_dict[key]
    return orig_dict

@@ -219,16 +219,26 @@
 ls.%0A%22%22%22%0A
+import re%0A
 from col
@@ -297,16 +297,94 @@
  jsonify
+, make_response, request, %5C%0A    current_app as app%0Afrom functools import wraps
 %0A%0Afrom e
@@ -821,16 +821,1830 @@
  doc)%0A%0A%0A
+def _modify_response(f):%0A    @wraps(f)%0A    def decorated(*args, **kwargs):%0A        if request.method == 'OPTIONS':%0A            resp = app.make_default_options_response()%0A        else:%0A            resp = make_response(f(*args, **kwargs))%0A%0A        # CORS%0A        domains = app.config%5B'X_DOMAINS'%5D%0A        headers = app.config%5B'X_HEADERS'%5D%0A        max_age = app.config%5B'X_MAX_AGE'%5D%0A        allow_credentials = app.config%5B'X_ALLOW_CREDENTIALS'%5D%0A        expose_headers = app.config%5B'X_EXPOSE_HEADERS'%5D%0A        origin = request.headers.get('Origin')%0A        if origin and domains:%0A            if isinstance(domains, str):%0A                domains = %5Bdomains%5D%0A%0A            if headers is None:%0A                headers = %5B%5D%0A            elif isinstance(headers, str):%0A                headers = %5Bheaders%5D%0A%0A            if expose_headers is None:%0A                expose_headers = %5B%5D%0A            elif isinstance(expose_headers, str):%0A                expose_headers = %5Bexpose_headers%5D%0A%0A            allow_credentials = allow_credentials is True%0A            methods = app.make_default_options_response().headers.get(%0A                'allow', '')%0A%0A            h = resp.headers%0A            if '*' in domains:%0A                h%5B'Access-Control-Allow-Origin'%5D = origin%0A                h%5B'Vary'%5D = 'Origin'%0A            elif any(re.match(re.escape(domain), origin)%0A                     for domain in domains):%0A                h%5B'Access-Control-Allow-Origin'%5D = origin%0A            else:%0A                h%5B'Access-Control-Allow-Origin'%5D = ''%0A%0A            h%5B'Access-Control-Allow-Headers'%5D = ', '.join(headers)%0A            h%5B'Access-Control-Expose-Headers'%5D = ', '.join(expose_headers)%0A            h%5B'Access-Control-Allow-Methods'%5D = methods%0A            h%5B'Access-Control-Max-Age'%5D = str(max_age)%0A%0A        return resp%0A    return decorated%0A%0A%0A
 @swagger
@@ -2663,16 +2663,34 @@
 -docs')%0A
+@_modify_response%0A
 def inde

Add CORS feature