diff --git a/python_nemesis/api/__init__.py b/python_nemesis/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python_nemesis/api/v1/__init__.py b/python_nemesis/api/v1/__init__.py new file mode 100644 index 0000000..4ba0d2a --- /dev/null +++ b/python_nemesis/api/v1/__init__.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +# 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. + +from flask import Blueprint +from python_nemesis.exceptions import general_handler +from python_nemesis.exceptions import NemesisException + + +V1_API = Blueprint('v1_api', __name__) + + +@V1_API.errorhandler(NemesisException) +def handle_exception(error): + return general_handler(error) + + +@V1_API.route('/v1') +def api_definition(): + return "" diff --git a/python_nemesis/base_app.py b/python_nemesis/base_app.py index e9cf39a..3d7a567 100644 --- a/python_nemesis/base_app.py +++ b/python_nemesis/base_app.py @@ -16,7 +16,7 @@ from oslo_config import cfg from python_nemesis.config import collect_sqlalchemy_opts from python_nemesis.config import register_opts from python_nemesis.extensions import db -# from python_nemesis.extensions import log +from python_nemesis.extensions import log def configure_blueprints(app, blueprints): @@ -63,7 +63,7 @@ def configure_extensions(app): :type app: :py:class:`flask.Flask` """ db.init_app(app) - # log.init_app(app) + log.init_app(app) def create_app(app_name=None, blueprints=None): @@ -83,6 +83,11 @@ def create_app(app_name=None, blueprints=None): configure_app(app) configure_extensions(app) + # Here we register the application blueprints. + from python_nemesis.api.v1 import V1_API + blueprints = [V1_API] + configure_blueprints(app, blueprints) + return app diff --git a/python_nemesis/exceptions.py b/python_nemesis/exceptions.py new file mode 100644 index 0000000..929ae53 --- /dev/null +++ b/python_nemesis/exceptions.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- + +# 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. + +from flask import request +import json +from python_nemesis.extensions import log + + +def general_handler(error): + log.logger.exception("Hit exception during %s" % request) + + try: + status_code = error.status_code + ret_data = json.dumps(error.to_dict()) + except Exception: + status_code = 500 + ret_data = {"title": "Internal Server Error", + "code": status_code, + "message": ""} + ret_data = json.dumps(ret_data) + + return ret_data, status_code + + +class NemesisException(Exception): + status_code = 500 + title = "Internal Server Error" + message = "" + + def __init__(self, title, message, status_code=None, payload=None): + Exception.__init__(self) + self.title = title + self.message = message + if status_code is not None: + self.status_code = status_code + + self.payload = payload + + def to_dict(self): + rv = dict(self.payload or ()) + rv['code'] = self.status_code + rv['title'] = self.title + rv['message'] = self.message + return rv diff --git a/python_nemesis/extensions.py b/python_nemesis/extensions.py index 96f65ff..5cb5858 100644 --- a/python_nemesis/extensions.py +++ b/python_nemesis/extensions.py @@ -1,6 +1,6 @@ -# from flask_oslolog import OsloLog +from flask_oslolog import OsloLog from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() -# log = OsloLog() +log = OsloLog() diff --git a/requirements.txt b/requirements.txt index 26a59c8..0b06308 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ pbr>=2.0.0 # Apache-2.0 alembic>=0.8.10 # MIT Flask!=0.11,<1.0,>=0.10 # BSD Flask-SQLAlchemy>=2.0 # BSD +flask-oslolog # Apache-2.0 oslo.config>=3.22.0 # Apache-2.0 oslo.messaging>=5.19.0 # Apache-2.0 oslo.log>=3.11.0 # Apache-2.0