def configure_api_behavior(app): """ Tell to Flask what to return in case of error and make sure that only JSON is returned. """ errors = { 'ProgrammingError': { 'message': 'Server error occured. Make sure that you init the ' 'database.', }, 'IntegrityError': { 'message': 'Wrong data, make sure that evey fields are present.', 'status': 400 }, 'ValueError': { 'message': 'Wrong data format in your filter.', 'status': 400 }, 'WrongDataSentException': { 'message': 'Wrong data format sent as body of your request.', 'status': 400 } } api = Api(app, catch_all_404s=True, errors=errors) api.representations = { 'application/json; charset=utf-8': output_json, 'application/json': output_json, } return api
def register_resources(app): api = Api(app) api.representations = {'application/json': output_json} products.register_resources(api) users.register_resources(api) cart.register_resources(api) pages.register_resources(api) orders.register_resources(api) reviews.register_resources(api) images.register_resources(api) mailing_list.register_resources(api)
def register_resources(admin): admin.before_request(requires_admin) admin_api = Api(admin) admin_api.representations = {'application/json': output_json} products.register_resources(admin_api) users.register_resources(admin_api) orders.register_resources(admin_api) shipments.register_resources(admin_api) pages.register_resources(admin_api) images.register_resources(admin_api) appearance.register_resources(admin_api) mailing_list.register_resources(admin_api)
def create_api(blueprint): api = Api(blueprint) api.representations = { 'application/json; charset=utf-8': output_json, } api.add_resource(Google, '/google', endpoint='google') api.add_resource(WordstatWords, '/wordstat/words', endpoint='wordstat_words') api.add_resource(WordstatHistory, '/wordstat/history', endpoint='wordstat_history') return api
def configure_api_from_blueprint(blueprint, route_tuples): """ Creates a Flask Restful api object based on information from given blueprint. API is configured to return JSON objects. Each blueprint is describe by a list of tuple. Each tuple is composed of a route and the related resource (controller). """ api = Api(blueprint, catch_all_404s=True) api.representations = { 'application/json; charset=utf-8': output_json, 'application/json': output_json, } for route_tuple in route_tuples: (path, resource) = route_tuple api.add_resource(resource, path) return api
DEFAULT_REPRESENTATIONS = {'application/json': output_json} app = Flask(__name__) app.secret_key = 'heroku_gfp8zr4k:mu22sv8pm9q3b5o286vfjjq870@ds015335' login_manager = LoginManager() login_manager.init_app(app) MONGO_URI = os.environ.get('MONGO_URL') if not MONGO_URI: MONGO_URI = "mongodb://*****:*****@ds015335.mlab.com:15335/heroku_gfp8zr4k" app.config['MONGO_URI'] = MONGO_URI mongo = PyMongo(app) api = Api(app) api.representations = DEFAULT_REPRESENTATIONS @login_manager.user_loader def load_user(user_id): return User.get(user_id) class index(Resource): def get(self): return {"Hello": "World"} class parseAllPage(Resource): def get(self): page = 1 if not 'page' in request.headers else int(request.headers['page']) location = 'halifax' if not 'location' in request.headers else request.headers['location'] headlines = mongo.db.headlines.find({'$or': [{'location': location}, {'source': 'chronicle'}]}) if page == 1 else None normal = mongo.db.normal.find({'location': location}).sort([('tag', 1)]).limit(15).skip((page - 1) * 15)
from Malcom.web.webserver import Model, UserManager from flask.ext.login import current_user from webserver import app malcom_api = Blueprint('malcom_api', __name__) def output_json(obj, code, headers=None): resp = make_response(dumps(obj), code) resp.headers.extend(headers or {}) return resp api = Api(app) DEFAULT_REPRESENTATIONS = {'application/json': output_json} api.representations = DEFAULT_REPRESENTATIONS class FileStorageArgument(reqparse.Argument): def convert(self, value, op): if self.type is FileStorage: return value # API PUBLIC for FEEDS=========================================== class FeedsAPI(Resource): decorators = [login_required] def get(self, action, feed_name=None): if action == 'list': return pickle.loads(g.messenger.send_recieve('feedList', 'feeds'))
from flask import Flask, make_response from flask_restful import Api from bson.json_util import dumps import Apps app = Flask(__name__) api = Api(app) def output_json(obj, code, headers = None): resp = make_response(dumps(obj), code) resp.headers.extend(headers or {}) return resp api.representations = {'application/json': output_json} api.add_resource(Apps.imageCollector, '/') if __name__ == '__main__': app.run(host = '127.0.0.1', port = 8000, debug = True)