Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Feb 22, 2014
1 parent 0f6894b commit b523451
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
44 changes: 44 additions & 0 deletions example.py
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)
4 changes: 4 additions & 0 deletions flaskapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class PermissionDenied(APIException):
detail = 'You do not have permission to perform this action.'


class NotFound(APIException):
status_code = status.HTTP_404_NOT_FOUND
detail = 'This resource does not exist.'

# class MethodNotAllowed(APIException):
# status_code = status.HTTP_405_METHOD_NOT_ALLOWED
# detail = 'Request method "%s" not allowed.'
Expand Down
17 changes: 0 additions & 17 deletions hello.py

This file was deleted.

0 comments on commit b523451

Please sign in to comment.