Skip to content

Instantly share code, notes, and snippets.

@arajkumar
Created May 22, 2020 12:04
Show Gist options
  • Save arajkumar/bd491edb9f0542632e6d736c7e1666dc to your computer and use it in GitHub Desktop.
Save arajkumar/bd491edb9f0542632e6d736c7e1666dc to your computer and use it in GitHub Desktop.
Blueprint and Flask_restful
from flask import Flask
from example_blueprint import example_blueprint
app = Flask(__name__)
app.register_blueprint(example_blueprint)
app.run(host='127.0.0.1', port=5000, debug=True)
from flask import Blueprint
from flask_restful import Api, Resource
example_blueprint = Blueprint('example_blueprint', __name__)
rest_api_v2 = Api(example_blueprint)
class DBError(Exception):
pass
class GremlinError(Exception):
pass
@example_blueprint.errorhandler(GremlinError)
@example_blueprint.errorhandler(DBError)
def _errror(ex):
return "error:" + ex.args[0]
# @example_blueprint.route('/db')
# def index():
# raise DBError('db')
# return "This is an example app"
# @example_blueprint.route('/gremlin')
# def gremlin():
# raise GremlinError('gremlin')
# return "This is an example app"
class Gremlin(Resource):
@staticmethod
def get():
raise GremlinError('gremlin')
class DB(Resource):
@staticmethod
def get():
raise DBError('db')
rest_api_v2.add_resource(Gremlin, "/api/gremlin")
rest_api_v2.add_resource(DB, "/api/db")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment