def main(): db_session.global_init("db/blogs.db") api.add_resource(users_resource.UsersListResource, '/api/v2/users') api.add_resource(users_resource.UsersResource, '/api/v2/users/<int:user_id>') api.add_resource(jobs_resource.jobsListResource, '/api/v2/jobs') api.add_resource(jobs_resource.jobsResource, '/api/v2/jobs/<int:job_id>') app.run(debug=True)
def create_app(): """ Create the app and setting all the road here Returns: Flask: Flask application for the api """ application = Flask(__name__) # bugsnag integration CORS(application) application.config["JSON_AS_ASCII"] = False application.config["DEBUG"] = True api = Api(application) # default index api.add_resource(Index, "/") api.add_resource(Stats, "/stats") return application
# 此模块是为了便于管理url 因此从views中引入api, blueprint,views from {{cookiecutter.app_name}}.api.views import api, blueprint from {{cookiecutter.app_name}}.api.views import ResfulHelloViews from {{cookiecutter.app_name}}.extensions import apispec from flask import current_app api.add_resource(ResfulHelloViews, '/hello/<string:name>') @blueprint.before_app_first_request def register_views(): apispec.spec.path(view=ResfulHelloViews, app=current_app)
from flask import Flask from flask_restful import Api from flask_cors import CORS from api import Card, Sms, Info, Analytics import api app = Flask(__name__) CORS(app) api = Api(app) api.add_resource(Card, '/carte') api.add_resource(Info, '/info') api.add_resource(Sms, '/sms') api.add_resource(Analytics, '/analytics') if __name__ == '__main__': app.run()
# parse the arguments into an object args = parser.parse_args() shelf = get_db() shelf[args['identifier']] = args return {'message': 'Device registered', 'data': args}, 201 class Device(Resource): def get(self, identifier): shelf = get_db() # If the key does not exist in the data store, return a 404 error if not (identifier in shelf): return {'message': 'Device not found', 'data0': {}}, 404 return {'message': 'Device found', 'data': shelf[identifier]}, 200 def delete(self, identifier): shelf = get_db() # If the key does not exist in the data store, return a 404 error if not (identifier in shelf): return {'message': 'Device not found', 'data0': {}}, 404 del shelf[identifier] return '', 204 api.add_resource(DeviceList, '/devices') api.add_resource(Device, '/device/<string:identifier>') api.add_resource(DeviceList, '/devices')
from flask_potion import ModelResource from {{cookiecutter.app_name}} import api from .models import User class UserResource(ModelResource): class Meta: model = User api.add_resource(UserResource)