def base_api(title=None, cors_origin=None): """Create base_api object""" blueprint = flask.Blueprint('v1', __name__) api = restplus.Api(blueprint, version='1.0', title=title, description='Treadmill REST API Documentation') error_handlers.register(api) # load up any external error_handlers for module in plugin_manager.load_all('treadmill.rest.error_handlers'): module.init(api) @blueprint.route('/docs/', endpoint='docs') def _swagger_ui(): """Swagger documentation route""" return restplus.apidoc.ui_for(api) rest.FLASK_APP.register_blueprint(blueprint) rest.FLASK_APP.register_blueprint(restplus.apidoc.apidoc) cors = webutils.cors(origin=cors_origin, content_type='application/json', credentials=True) @rest.FLASK_APP.before_request def _before_request_user_handler(): user = flask.request.environ.get('REMOTE_USER') if user: flask.g.user = user @rest.FLASK_APP.after_request def _after_request_cors_handler(response): """Process all OPTIONS request, thus don't need to add to each app""" if flask.request.method != 'OPTIONS': return response _LOGGER.debug('This is an OPTIONS call') def _noop_options(): """No noop response handler for all OPTIONS. """ headers = flask.request.headers.get('Access-Control-Request-Headers') options_cors = webutils.cors(origin=cors_origin, credentials=True, headers=headers) response = options_cors(_noop_options)() return response return (api, cors)
def setUp(self): """Initialize the app with the corresponding logic.""" self.app = flask.Flask(__name__) self.app.testing = True api = restplus.Api(self.app) error_handlers.register(api) cors = webutils.cors(origin='*', content_type='application/json', credentials=False) self.impl = mock.Mock() instance.init(api, cors, self.impl) self.client = self.app.test_client()
def base_api(title=None, cors_origin=None): """Create base_api object""" blueprint = flask.Blueprint('v1', __name__) api = restplus.Api(blueprint, version='1.0', title=title, description='Treadmill REST API Documentation') error_handlers.register(api) # load up any external error_handlers for module in plugin_manager.load_all('treadmill.rest.error_handlers'): module.init(api) @blueprint.route('/docs/', endpoint='docs') def _swagger_ui(): """Swagger documentation route""" return restplus.apidoc.ui_for(api) # Need to create our own Apidoc, as the restplus one uses relative path to # their module to serve up the content for the Swagger UI. tmpl_dir = pkg_resources.resource_filename('flask_restplus', 'templates') static_dir = pkg_resources.resource_filename('flask_restplus', 'static') # This is a hack that overrides all templates and static folders for our # Flask app, but as it stands, only flask-restplus is using # render_template, so this is fine for now. # The main problem is that restplus.Api() internally refers to it's # restplus.apidoc.apidoc and that is created on load time, which all kinds # of Flask rule and defering going on. Ideally restplus should allow # creating your own Apidoc and sending that in the above Api() constructor. rest.FLASK_APP.template_folder = tmpl_dir rest.FLASK_APP.static_folder = static_dir rest.FLASK_APP.register_blueprint(blueprint) rest.FLASK_APP.register_blueprint(restplus.apidoc.apidoc) cors = webutils.cors(origin=cors_origin, content_type='application/json', credentials=True) @rest.FLASK_APP.before_request def _before_request_user_handler(): user = flask.request.environ.get('REMOTE_USER') if user: flask.g.user = user @rest.FLASK_APP.after_request def _after_request_cors_handler(response): """Process all OPTIONS request, thus don't need to add to each app""" if flask.request.method != 'OPTIONS': return response _LOGGER.debug('This is an OPTIONS call') def _noop_options(): """No noop response handler for all OPTIONS""" pass headers = flask.request.headers.get('Access-Control-Request-Headers') options_cors = webutils.cors(origin=cors_origin, credentials=True, headers=headers) response = options_cors(_noop_options)() return response return (api, cors)
def init(apis, title=None, cors_origin=None, authz_arg=None): """Module initialization.""" blueprint = flask.Blueprint('v1', __name__) api = restplus.Api(blueprint, version='1.0', title=title, description="Treadmill REST API Documentation") error_handlers.register(api) # load up any external error_handlers try: err_handlers_plugin = importlib.import_module( 'treadmill.plugins.rest.error_handlers') err_handlers_plugin.init(api) except ImportError as err: _LOGGER.warn('Unable to load error_handlers plugin: %s', err) @blueprint.route('/docs/', endpoint='docs') def _swagger_ui(): """Swagger documentation route""" return restplus.apidoc.ui_for(api) rest.FLASK_APP.register_blueprint(blueprint) rest.FLASK_APP.register_blueprint(restplus.apidoc.apidoc) cors = webutils.cors(origin=cors_origin, content_type='application/json', credentials=True) @rest.FLASK_APP.after_request def _after_request_cors_handler(response): """Process all OPTIONS request, thus don't need to add to each app""" if flask.request.method != 'OPTIONS': return response _LOGGER.debug('This is an OPTIONS call') def _noop_options(): """No noop response handler for all OPTIONS""" pass headers = flask.request.headers.get('Access-Control-Request-Headers') options_cors = webutils.cors(origin=cors_origin, credentials=True, headers=headers) response = options_cors(_noop_options)() return response def user_clbk(): """Get current user from the request.""" return flask.request.environ.get('REMOTE_USER') if authz_arg is None: authorizer = authz.NullAuthorizer() else: authorizer = authz.ClientAuthorizer(user_clbk, authz_arg) endpoints = [] for apiname in apis: try: apimod = apiname.replace('-', '_') _LOGGER.info('Loading api: %s', apimod) api_restmod = importlib.import_module('.'.join( ['treadmill', 'rest', 'api', apimod])) api_implmod = importlib.import_module('.'.join( ['treadmill', 'api', apimod])) api_impl = api_implmod.init(authorizer) endpoint = api_restmod.init(api, cors, api_impl) if endpoint is None: endpoint = apiname.replace('_', '-').replace('.', '/') if not endpoint.startswith('/'): endpoint = '/' + endpoint endpoints.append(endpoint) except ImportError as err: _LOGGER.warn('Unable to load %s api: %s', apimod, err) return endpoints