def post_hello(body): if not body: return problem(status=400, title="Bad Request", detail="Body should be a json object.") print(f"body, {body.__class__}") return {"UNEXPECTED_STRING": 1} def configure_logger(log_config="logging.yaml"): """Configure the logging subsystem.""" if not isfile(log_config): return basicConfig() from logging.config import dictConfig with open(log_config) as fh: log_config = yaml_load(fh) return dictConfig(log_config) if __name__ == "__main__": configure_logger() app = FlaskApp("hello", port=8443, specification_dir=dirname(__file__), options={"swagger_ui": True}) app.add_api("simple.yaml", validate_responses=True, strict_validation=True) app.run(ssl_context="adhoc", debug=True)
import argparse import os import subprocess import sys from connexion import FlaskApp as Flask specification_dir = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, specification_dir) # Create the application instance app = Flask(__name__, specification_dir=specification_dir) # Read the open api .yml file to configure the # endpoints # TODO allow for specifying multiple swagger files app.add_api(os.path.join("openapi", "swagger.yml")) port = int(os.environ.get("SMARTNOISE_SERVICE_PORT", 5000)) os.environ["SMARTNOISE_SERVICE_PORT"] = str(port) if __name__ == "__main__": app.run(host='0.0.0.0', port=port) # flask app app = app.app
from os import environ from connexion import FlaskApp from mongoengine import connect from modules.error_handlers import configure_error_handlers app_is_debug_mode = environ['FLASK_IS_DEBUG'] == 'True' app_use_reloader = environ['FLASK_USE_RELOADER'] == 'True' app = FlaskApp(__name__, specification_dir='swagger/') app = configure_error_handlers(app=app) app.add_api('timers_api_documentation.yaml', strict_validation=True) if __name__ == '__main__': connect(environ['MONGO_DATABASE_NAME'], host=environ['MONGO_DATABASE_SERVER'], port=int(environ['MONGO_DATABASE_PORT'])) app.run(port=int(environ['FLASK_DEFAULT_PORT']), debug=app_is_debug_mode, use_reloader=app_use_reloader, threaded=False)
app = FlaskApp(__name__, options={}) app.add_api('openapi.yaml', base_path=ENV.BASE_HREF) CORS(app.app) application = app.app @application.teardown_appcontext def shutdown_session(exception: Exception = None) -> None: """ Boilerplate connexion code """ database_provider.db_session.remove() @application.errorhandler(SBNSISException) def handle_sbnsis_error(error: Exception): """Log errors.""" get_logger().exception('SBS Survey Image Service error.') return str(error), getattr(error, 'code', 500) @application.errorhandler(Exception) def handle_other_error(error: Exception): """Log errors.""" get_logger().exception('An error occurred.') return ('Unexpected error. Please report if the problem persists.', getattr(error, 'code', 500)) if __name__ == '__main__': app.run(port=ENV.API_PORT, use_reloader=False, threaded=False)
from flask import Flask from flask import request from flask.views import View from connexion import FlaskApp class SimpleTest(View): def dispatch_request(self): return 'foo' get_test = SimpleTest.as_view('get_test') if __name__ == '__main__': options = {"swagger_ui": True} app = FlaskApp(__name__, options=options) app.add_api('openapi.yaml') app.run()