Last active
November 29, 2020 11:48
-
-
Save sumanentc/d41717294e2a8bed16ae0eb2a239886c to your computer and use it in GitHub Desktop.
This file contains 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, Blueprint, jsonify | |
from flask_restplus import Api | |
from ma import ma | |
from db import db | |
from resources.store import Store, StoreList, store_ns, stores_ns | |
from resources.item import Item, ItemList, items_ns, item_ns | |
from marshmallow import ValidationError | |
app = Flask(__name__) | |
bluePrint = Blueprint('api', __name__, url_prefix='/api') | |
api = Api(bluePrint, doc='/doc', title='Sample Flask-RestPlus Application') | |
app.register_blueprint(bluePrint) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' | |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | |
app.config['PROPAGATE_EXCEPTIONS'] = True | |
api.add_namespace(item_ns) | |
api.add_namespace(items_ns) | |
api.add_namespace(store_ns) | |
api.add_namespace(stores_ns) | |
@app.before_first_request | |
def create_tables(): | |
db.create_all() | |
@api.errorhandler(ValidationError) | |
def handle_validation_error(error): | |
return jsonify(error.messages), 400 | |
item_ns.add_resource(Item, '/<int:id>') | |
items_ns.add_resource(ItemList, "") | |
store_ns.add_resource(Store, '/<int:id>') | |
stores_ns.add_resource(StoreList, "") | |
if __name__ == '__main__': | |
db.init_app(app) | |
ma.init_app(app) | |
app.run(port=5000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment