-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f6894b
commit b523451
Showing
3 changed files
with
48 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from flask import request | ||
from flaskapi import FlaskAPI, status, exceptions | ||
|
||
app = FlaskAPI(__name__) | ||
|
||
|
||
notes = { | ||
0: {'text': 'do the shopping'}, | ||
1: {'text': 'build the codez'}, | ||
2: {'text': 'paint the door'}, | ||
} | ||
|
||
@app.route("/", methods=['GET', 'POST']) | ||
def notes_list(): | ||
if request.method == 'POST': | ||
global idx | ||
note = {'text': str(request.data.get('text', ''))} | ||
idx = max(list(notes.keys())) + 1 | ||
notes[idx] = note | ||
return note, status.HTTP_201_CREATED | ||
|
||
# request.method == 'GET' | ||
return notes | ||
|
||
|
||
@app.route("/<int:key>/", methods=['GET', 'PUT', 'DELETE']) | ||
def notes_detail(key): | ||
if request.method == 'PUT': | ||
note = {'text': str(request.data.get('text', ''))} | ||
notes[key] = note | ||
return note | ||
|
||
elif request.method == 'DELETE': | ||
notes.pop(key, None) | ||
return None, status.HTTP_204_NO_CONTENT | ||
|
||
# request.method == 'GET' | ||
if key not in notes: | ||
raise exceptions.NotFound() | ||
return notes[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