importConfig(app) db = SQLAlchemy(app) Resource.method_decorators.append(authDecorator(app)) app.after_request(CORS.allowALLTheThings) # setup custom error messages errors = { 'NotAcceptable': { 'message': "Your client indicated that it does not accept any of the representations we support.", 'status': 406, }, } # configure flask restful api = Api(app, default_mediatype=None, catch_all_404s=True, errors=errors) api.representations = {} home_api = Api(app, default_mediatype=None, catch_all_404s=True, errors=errors) home_api.representations = {} # def abort_if_project_doesnt_exist(name): # names = [p.name for p in db.session.query(m.Project).filter(m.Project.name == name)] # if name not in names: # abort(404, message="Project {0} doesn't exist.".format(name)) # # def abort_if_user_doesnt_exist(username): # usernames = [p.username for p in db.session.query(m.User).filter(m.User.username == username)] # print "USERNAMES RETURNED from abort_if_user_not_exist: " + str(usernames) # if username not in usernames: # abort(404, message="User {0} doesn't exist.".format(username)) # parser = reqparse.RequestParser()
from . import index, users, auth app = Flask(__name__) app.abort = abort env = os.environ.get('ENV', 'dev') app.config.from_object('website.settings.%sConfig' % env.capitalize()) app.config['ERROR_404_HELP'] = False auth.init(app) db.init_app(app) mail = Mail(app) api = Api(app, catch_all_404s=True, default_mediatype='application/hal+json') api.representations = {'application/hal+json': output_json} @app.before_request def validate_mimetype(): message = "This endpoint only supports Content-Type: %s requests, not %s." json = 'application/json' if request.method in ['POST', 'PUT']: if request.mimetype != json: app.abort(415, message=message % (json, request.mimetype)) api.add_resource(index.Index, '/', '/protected', endpoint='index') api.add_resource(users.Users, '/users', endpoint='users') api.add_resource(users.User, '/users/<int:id>', endpoint='user')
import os from flask import Flask from flask import make_response from flask.ext.restful import Api from flask.ext.pymongo import PyMongo from bson.json_util import dumps MONGO_URL = os.environ.get('MONGO_URL') if not MONGO_URL: MONGO_URL = "mongodb://localhost:27017/getpostapp"; app = Flask(__name__) app.config['MONGO_URI'] = MONGO_URL mongo = PyMongo(app) def output_json(obj, code, headers=None): resp = make_response(dumps(obj), code) resp.headers.extend(headers or {}) return resp DEFAULT_REPRESENTATIONS = {'application/json': output_json} api = Api(app) api.representations = DEFAULT_REPRESENTATIONS import getpostapp.resources
client = pymongo.MongoClient() db = client.pokemon collection = db.pokedex types_collection = db.types def output_json(obj, code, headers=None): resp = make_response(dumps(obj), code) resp.headers.extend(headers or {}) return resp DEFAULT_REPRESENTATIONS = {'application/json': output_json} api = Api(app) api.representations = DEFAULT_REPRESENTATIONS # Web Service class NationalPokedexAPI(Resource): def __init__(self): self.reqparse = reqparse.RequestParser() super(NationalPokedexAPI, self).__init__() def get(self): """ GET the complete national Pokedex :return: Complete national Pokedex """ return [
from flask import Flask, make_response from flask.ext.restful import Api from bson.json_util import dumps #Blueprint & Restfull Config app = Flask(__name__) app.register_blueprint(halalbodies_api) app.register_blueprint(places_api) app.register_blueprint(nearbies_api) api = Api(app) def toJson(obj, code, headers=None): resp = make_response(dumps(obj), code) resp.headers.extend(headers or {}) return resp #API Mapping api.representations = {'application/json' : toJson} api.add_resource(PlacesAPI, '/sharee/api/v1/places') api.add_resource(NearbyPlacesAPI, '/sharee/api/v1/places/nearby') api.add_resource(PlaceAPI, '/sharee/api/v1/places/<string:place_id>') api.add_resource(CategoriesAPI, '/sharee/api/v1/places/categories') api.add_resource(CategoryAPI, '/sharee/api/v1/places/categories/<string:category_id>') api.add_resource(HalalBodiesAPI, '/sharee/api/v1/halal/bodies') api.add_resource(HalalBodyAPI, '/sharee/api/v1/halal/bodies/<string:bodies_id>') if __name__ == '__main__': app.run(debug=True)
importConfig(app) db = SQLAlchemy(app) Resource.method_decorators.append(authDecorator(app)) app.after_request(CORS.allowALLTheThings) # setup custom error messages errors = { "NotAcceptable": { "message": "Your client indicated that it does not accept any of the representations we support.", "status": 406, } } # configure flask restful api = Api(app, default_mediatype=None, catch_all_404s=True, errors=errors) api.representations = {} home_api = Api(app, default_mediatype=None, catch_all_404s=True, errors=errors) home_api.representations = {} # def abort_if_project_doesnt_exist(name): # names = [p.name for p in db.session.query(m.Project).filter(m.Project.name == name)] # if name not in names: # abort(404, message="Project {0} doesn't exist.".format(name)) # # def abort_if_user_doesnt_exist(username): # usernames = [p.username for p in db.session.query(m.User).filter(m.User.username == username)] # print "USERNAMES RETURNED from abort_if_user_not_exist: " + str(usernames) # if username not in usernames: # abort(404, message="User {0} doesn't exist.".format(username)) # parser = reqparse.RequestParser()