<commit_before># Copyright (c) 2015 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import flask
from oslo_config import cfg
import six
from werkzeug import exceptions as werkzeug_exceptions

from sahara.api import v10 as api_v10
from sahara.api import v11 as api_v11
from sahara.api import v2 as api_v2
from sahara import context
from sahara.utils import api as api_utils


CONF = cfg.CONF


def build_app(version_response=None):
    """App builder (wsgi).

    Entry point for Sahara REST API server
    """
    app = flask.Flask('sahara.api')

    version_response = (version_response or
                        {
                            "versions": [
                                {"id": "v1.0", "status": "SUPPORTED"},
                                {"id": "v1.1", "status": "CURRENT"}
                            ]
                        })

    @app.route('/', methods=['GET'])
    def version_list():
        context.set_ctx(None)
        return api_utils.render(version_response)

    @app.teardown_request
    def teardown_request(_ex=None):
        context.set_ctx(None)

    app.register_blueprint(api_v10.rest, url_prefix='/v1.0')
    app.register_blueprint(api_v10.rest, url_prefix='/v1.1')
    app.register_blueprint(api_v11.rest, url_prefix='/v1.1')

    def make_json_error(ex):
        status_code = (ex.code
                       if isinstance(ex, werkzeug_exceptions.HTTPException)
                       else 500)
        description = (ex.description
                       if isinstance(ex, werkzeug_exceptions.HTTPException)
                       else str(ex))
        return api_utils.render({'error': status_code,
                                 'error_message': description},
                                status=status_code)

    for code in six.iterkeys(werkzeug_exceptions.default_exceptions):
        app.error_handler_spec[None][code] = make_json_error

    return app


def build_v2_app():
    """App builder (wsgi).

    Entry point for Experimental V2 Sahara REST API server<commit_msg>Fix Flask error_handler_spec<commit_after># Copyright (c) 2015 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import flask
from oslo_config import cfg
import six
from werkzeug import exceptions as werkzeug_exceptions

from sahara.api import v10 as api_v10
from sahara.api import v11 as api_v11
from sahara.api import v2 as api_v2
from sahara import context
from sahara.utils import api as api_utils


CONF = cfg.CONF


def build_app(version_response=None):
    """App builder (wsgi).

    Entry point for Sahara REST API server
    """
    app = flask.Flask('sahara.api')

    version_response = (version_response or
                        {
                            "versions": [
                                {"id": "v1.0", "status": "SUPPORTED"},
                                {"id": "v1.1", "status": "CURRENT"}
                            ]
                        })

    @app.route('/', methods=['GET'])
    def version_list():
        context.set_ctx(None)
        return api_utils.render(version_response)

    @app.teardown_request
    def teardown_request(_ex=None):
        context.set_ctx(None)

    app.register_blueprint(api_v10.rest, url_prefix='/v1.0')
    app.register_blueprint(api_v10.rest, url_prefix='/v1.1')
    app.register_blueprint(api_v11.rest, url_prefix='/v1.1')

    class _JSONErrorHandler(dict):
        def __getitem__(self, ex):
            status_code = (ex.code
                           if isinstance(ex, werkzeug_exceptions.HTTPException)
                           else 500)
            description = (ex.description
                           if isinstance(ex, werkzeug_exceptions.HTTPException)
                           else str(ex))
            return api_utils.render({'error': status_code,
                                     'error_message': description},
                                    status=status_code)

    make_json_error = _JSONErrorHandler()

    for code in six.iterkeys(werkzeug_exceptions.default_exceptions):
        app.error_handler_spec[None][code] = make_json_error

    return app


def build_v2_app():
    """App builder (wsgi).

    Entry point for Experimental V2 Sahara REST API server
{'fraction_non_alphanumeric': 0.08736379277384822, 'fraction_numerical': 0.014911106862932517, 'mean_word_length': 2.3282442748091605, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '921822', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1464, 'n_words': 436}