Created
March 3, 2017 09:15
-
-
Save DmitryBe/b93a78b69f8296a9b60f0bca953c2a52 to your computer and use it in GitHub Desktop.
Restful API with python flask
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
# https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask | |
from flask import Flask, jsonify │ response = self.full_dispatch_request() | |
from flask import request, abort, make_response │ File "/home/dmitry/workspace/flask-api/env/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request | |
│ rv = self.handle_user_exception(e) | |
app = Flask(__name__) │ File "/home/dmitry/workspace/flask-api/env/lib/python2.7/site-packages/flask/app.py", line 1512, in handle_user_exception | |
│ return self.handle_http_exception(e) | |
@app.errorhandler(404) │ File "/home/dmitry/workspace/flask-api/env/lib/python2.7/site-packages/flask/app.py", line 1471, in handle_http_exception | |
def not_found(error): │ return handler(e) | |
return make_response(jsonify({'error': 'Not found'}), 404) │ File "/home/dmitry/workspace/flask-api/app.py", line 8, in not_found | |
│ return make_response(jsonify({'error': 'Not found'}), 404) | |
tasks = [ │NameError: global name 'make_response' is not defined | |
{ │ * Detected change in '/home/dmitry/workspace/flask-api/app.py', reloading | |
'id': 1, │ * Restarting with stat | |
'title': u'Buy groceries', │ * Debugger is active! | |
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', │ * Debugger pin code: 166-987-990 | |
'done': False │127.0.0.1 - - [03/Mar/2017 16:57:08] "DELETE /api/tasks/4 HTTP/1.1" 404 - | |
}, │127.0.0.1 - - [03/Mar/2017 16:57:10] "DELETE /api/tasks/4 HTTP/1.1" 404 - | |
{ │127.0.0.1 - - [03/Mar/2017 16:57:16] "DELETE /api/tasks/4 HTTP/1.1" 404 - | |
'id': 2, │ | |
'title': u'Learn Python', ├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | |
'description': u'Need to find a good Python tutorial on the web', │ File "/home/dmitry/workspace/flask-api/env/lib/python2.7/site-packages/flask/app.py", line 1512, in handle_user_exception | |
'done': False │ return self.handle_http_exception(e) | |
} │ File "/home/dmitry/workspace/flask-api/env/lib/python2.7/site-packages/flask/app.py", line 1471, in handle_http_exception | |
] │ return handler(e) | |
│ File "/home/dmitry/workspace/flask-api/app.py", line 8, in not_found | |
@app.route('/api/tasks',methods=['GET']) │ return make_response(jsonify({'error': 'Not found'}), 404) | |
def get_tasks(): │NameError: global name 'make_response' is not defined | |
return jsonify({'tasks': tasks}) │ | |
│--> | |
@app.route('/api/tasks', methods=['POST']) │dmitry@ubuntu-dmitry:~$ | |
def create_task(): │dmitry@ubuntu-dmitry:~$ | |
if not request.json or not 'title' in request.json: │dmitry@ubuntu-dmitry:~$ curl -X DELETE http://localhost:5000/api/tasks/4 | |
abort(400) │{ | |
task = { │ "error": "Not found" | |
'id': tasks[-1]['id'] + 1, │} | |
'title': request.json['title'], │dmitry@ubuntu-dmitry:~$ | |
'description': request.json.get('description', ""), │dmitry@ubuntu-dmitry:~$ | |
'done': False │dmitry@ubuntu-dmitry:~$ | |
} │dmitry@ubuntu-dmitry:~$ | |
tasks.append(task) │dmitry@ubuntu-dmitry:~$ curl -X DELETE http://localhost:5000/api/tasks/4 | |
return jsonify(task), 201 │{ | |
│ "error": "Not found" | |
@app.route('/api/tasks/<int:task_id>', methods=['DELETE']) │} | |
def delete_task(task_id): │dmitry@ubuntu-dmitry:~$ | |
task = [task for task in tasks if task['id'] == task_id] │dmitry@ubuntu-dmitry:~$ | |
if len(task) == 0: │dmitry@ubuntu-dmitry:~$ | |
abort(404) │dmitry@ubuntu-dmitry:~$ | |
tasks.remove(task[0]) │dmitry@ubuntu-dmitry:~$ | |
return jsonify({'result': True}) │dmitry@ubuntu-dmitry:~$ curl -X DELETE http://localhost:5000/api/tasks/4 | |
│{ | |
if __name__ == '__main__': │ "error": "Not found" | |
app.run(host='0.0.0.0', 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
appdirs==1.4.2 │ * Restarting with stat | |
click==6.7 │ * Debugger is active! | |
Flask==0.12 │ * Debugger pin code: 166-987-990 | |
itsdangerous==0.24 │/home/dmitry/workspace/flask-api/env/local/lib/python2.7/site-packages/werkzeug/filesystem.py:63: BrokenFilesystemWarning: Detected a m | |
Jinja2==2.9.5 │isconfigured UNIX filesystem: Will use UTF-8 as filesystem encoding instead of 'ANSI_X3.4-1968' | |
MarkupSafe==0.23 │ BrokenFilesystemWarning) | |
packaging==16.8 │127.0.0.1 - - [03/Mar/2017 16:56:37] "DELETE /api/tasks/4 HTTP/1.1" 500 - | |
pyparsing==2.1.10 │Traceback (most recent call last): | |
six==1.10.0 │ File "/home/dmitry/workspace/flask-api/env/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__ | |
Werkzeug==0.11.15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment