Created
May 24, 2016 10:54
-
-
Save amorphobia/ff6704b92fd8373ea7cd844372470982 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python3 | |
from flask import Flask, jsonify | |
app = Flask(__name__) | |
tasks = [ | |
{ | |
'id': 1, | |
'title': u'Buy groceries', | |
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', | |
'done': False | |
}, | |
{ | |
'id': 2, | |
'title': u'Learn Python', | |
'description': u'Need to find a good Python tutorial on the web', | |
'done': False | |
} | |
] | |
# public | |
from flask import url_for | |
def make_public_task(task): | |
new_task = {} | |
for field in task: | |
if field == 'id': | |
new_task['url'] = url_for('get_task', task_id=task['id'], _external=True) | |
else: | |
new_task[field] = task[field] | |
return new_task | |
# authorization | |
from flask.ext.httpauth import HTTPBasicAuth | |
auth = HTTPBasicAuth() | |
@auth.get_password | |
def get_password(username): | |
if username == 'xuesong': | |
return 'piepie' | |
return None | |
@auth.error_handler | |
def unauthorized(): | |
return make_response(jsonify({'error': 'Unauthorized access'}), 401) | |
# get all tasks | |
@app.route('/todo/api/v1.0/tasks', methods=['GET']) | |
@auth.login_required | |
def get_tasks(): | |
# return jsonify({'tasks': tasks}) | |
return jsonify({'tasks': list(map(make_public_task, tasks))}) | |
from flask import abort | |
# get task by id | |
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET']) | |
@auth.login_required | |
def get_task(task_id): | |
task_filter_obj = filter(lambda t: t['id'] == task_id, tasks) | |
# task = next(task_filter_obj) | |
# if len(task) == 0: | |
# abort(404) | |
try: | |
task = next(task_filter_obj) | |
except StopIteration: | |
abort(404) | |
return jsonify({'task': task}) | |
# append a new task | |
from flask import request | |
@app.route('/todo/api/v1.0/tasks', methods=['POST']) | |
@auth.login_required | |
def create_task(): | |
if not request.json or not 'title' in request.json: | |
abort(400) | |
task = { | |
'id': tasks[-1]['id'] + 1, | |
'title': request.json['title'], | |
'description': request.json.get('description', ""), | |
'done': False | |
} | |
tasks.append(task) | |
return jsonify({'task': task}), 201 | |
# update task | |
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT']) | |
@auth.login_required | |
def update_task(task_id): | |
task_filter_obj = filter(lambda t: t['id'] == task_id, tasks) | |
try: | |
task = next(task_filter_obj) | |
except StopIteration: | |
abort(404) | |
if not request.json: | |
abort(400) | |
if 'title' in request.json and type(request.json['title']) != str: | |
abort(400) | |
if 'description' in request.json and type(request.json['description']) != str: | |
abort(400) | |
if 'done' in request.json and type(request.json['done']) is not bool: | |
abort(400) | |
task['title'] = request.json.get('title', task['title']) | |
task['description'] = request.json.get('description', task['description']) | |
task['done'] = request.json.get('done', task['done']) | |
return jsonify({'task': task}) | |
# delete task | |
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['DELETE']) | |
@auth.login_required | |
def delete_task(task_id): | |
task_filter_obj = filter(lambda t: t['id'] == task_id, tasks) | |
try: | |
task = next(task_filter_obj) | |
except StopIteration: | |
abort(404) | |
tasks.remove(task) | |
return jsonify({'result': True}) | |
# Errors | |
from flask import make_response | |
@app.errorhandler(404) | |
def not_found(error): | |
return make_response(jsonify({'error': 'Not found'}), 404) | |
@app.errorhandler(400) | |
def bad_request(error): | |
return make_response(jsonify({'error': 'Bad request'}), 400) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment