Last active
April 26, 2016 08:31
-
-
Save jackiect/4a6fc5ba88f1d06443c8aa587ab03bdd to your computer and use it in GitHub Desktop.
This file contains 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/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) |
This file contains 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/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SyntaxError
will be raised# encoding: utf-8
or# -*- coding: utf-8 -*-
(which is more standard) to fix it# encoding: utf-8
won't change the fact that unicode and str are different in Python 2.7__future__
packageb
before''