Created
May 22, 2020 12:04
-
-
Save arajkumar/bd491edb9f0542632e6d736c7e1666dc to your computer and use it in GitHub Desktop.
Blueprint and Flask_restful
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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