Last active
April 26, 2016 08:31
Revisions
-
jackiect revised this gist
Apr 20, 2016 . 1 changed file with 61 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ #!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import unicode_literals from flask import request, url_for from flask.ext.api import FlaskAPI, status, exceptions app = FlaskAPI(__name__) notes = { 0: 'do the shopping购物', 1: 'build the codez', 2: 'paint the door', } def note_repr(key): return { 'url': request.host_url.rstrip('/') + url_for('notes_detail', key=key), 'text': notes[key] } @app.route("/", methods=['GET', 'POST']) def notes_list(): """ List or create notes. """ if request.method == 'POST': note = str(request.data.get('text', '')) idx = max(notes.keys()) + 1 notes[idx] = note return note_repr(idx), status.HTTP_201_CREATED # request.method == 'GET' return [note_repr(idx) for idx in sorted(notes.keys())] @app.route("/<int:key>/", methods=['GET', 'PUT', 'DELETE']) def notes_detail(key): """ Retrieve, update or delete note instances. """ if request.method == 'PUT': note = str(request.data.get('text', '')) notes[key] = note return note_repr(key) elif request.method == 'DELETE': notes.pop(key, None) return '', status.HTTP_204_NO_CONTENT # request.method == 'GET' if key not in notes: raise exceptions.NotFound() return note_repr(key) if __name__ == "__main__": app.run(debug=True) -
jackiect created this gist
Apr 18, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ #!/usr/bin/python # -*- coding: utf-8 -*- from flask import request, url_for from flask.ext.api import FlaskAPI, status, exceptions app = FlaskAPI(__name__) notes = { 0: '购物', 1: 'build the codez', 2: 'paint the door', } def note_repr(key): return { 'url': request.host_url.rstrip('/') + url_for('notes_detail', key=key), 'text': notes[key] } @app.route("/", methods=['GET', 'POST']) def notes_list(): """ List or create notes. """ if request.method == 'POST': note = str(request.data.get('text', '')) idx = max(notes.keys()) + 1 notes[idx] = note return note_repr(idx), status.HTTP_201_CREATED # request.method == 'GET' return [note_repr(idx) for idx in sorted(notes.keys())] @app.route("/<int:key>/", methods=['GET', 'PUT', 'DELETE']) def notes_detail(key): """ Retrieve, update or delete note instances. """ if request.method == 'PUT': note = str(request.data.get('text', '')) notes[key] = note return note_repr(key) elif request.method == 'DELETE': notes.pop(key, None) return '', status.HTTP_204_NO_CONTENT # request.method == 'GET' if key not in notes: raise exceptions.NotFound() return note_repr(key) if __name__ == "__main__": app.run(debug=True)