105 lines
2.6 KiB
Python
105 lines
2.6 KiB
Python
|
|
# Copyright (c) 2013 Mirantis, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
from flask import Flask
|
|
from flask.ext.restful import reqparse, abort, Api, Resource
|
|
|
|
app = Flask(__name__)
|
|
api = Api(app)
|
|
|
|
TODOS = {
|
|
'todo1': {'task': 'build an API'},
|
|
'todo2': {'task': '?????'},
|
|
'todo3': {'task': 'profit!'},
|
|
}
|
|
|
|
|
|
def abort_if_todo_doesnt_exist(todo_id):
|
|
if todo_id not in TODOS:
|
|
abort(404, message="Todo {} doesn't exist".format(todo_id))
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument('task', type=str)
|
|
|
|
|
|
# Todo
|
|
# show a single todo item and lets you delete them
|
|
class Todo(Resource):
|
|
def get(self, todo_id):
|
|
abort_if_todo_doesnt_exist(todo_id)
|
|
return TODOS[todo_id]
|
|
|
|
def delete(self, todo_id):
|
|
abort_if_todo_doesnt_exist(todo_id)
|
|
del TODOS[todo_id]
|
|
return '', 204
|
|
|
|
def put(self, todo_id):
|
|
args = parser.parse_args()
|
|
task = {'task': args['task']}
|
|
TODOS[todo_id] = task
|
|
return task, 201
|
|
|
|
|
|
# TodoList
|
|
# shows a list of all todos, and lets you POST to add new tasks
|
|
class TodoList(Resource):
|
|
def get(self):
|
|
return TODOS
|
|
|
|
def post(self):
|
|
args = parser.parse_args()
|
|
todo_id = 'todo%d' % (len(TODOS) + 1)
|
|
TODOS[todo_id] = {'task': args['task']}
|
|
return TODOS[todo_id], 201
|
|
|
|
##
|
|
## Actually setup the Api resource routing here
|
|
##
|
|
api.add_resource(TodoList, '/todos')
|
|
api.add_resource(Todo, '/todos/<string:todo_id>')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|
|
#
|
|
# @app.route('/client/ui')
|
|
# def ui_data():
|
|
# #type - application/z-gzip
|
|
# pass
|
|
#
|
|
# @app.route('/client/ui')
|
|
# def conductor_data():
|
|
# #type - application/z-gzip
|
|
# pass
|
|
#
|
|
# @app.route('/client/ui')
|
|
# def conductor_data():
|
|
# #type - application/json
|
|
# pass
|
|
#
|
|
# @app.route('/client/<data_type>/<path>')
|
|
# def conductor_data():
|
|
# #type - application/json
|
|
# pass
|
|
#
|
|
# @app.route('/client/<data_type>/<file_name>')
|
|
# def conductor_data():
|
|
# #type - application/json
|
|
# pass
|
|
#
|
|
#
|
|
# if __name__ == '__main__':
|
|
# app.run() |