-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
54 lines (41 loc) · 1.33 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# python3 -m venv venv
# . venv/bin/activate activate
# pip install mariadb flask
import sys
import flask
from flask import request
import json
import mariadb
app = flask.Flask(__name__)
app.config["DEBUG"] = True
config = {
'host': '127.0.0.1',
'user': 'app_user',
'password': 'Password123!',
'database': 'todo'
}
@app.route('/api/tasks', methods=['GET','POST','PUT','DELETE'])
def index():
conn = mariadb.connect(**config)
cur = conn.cursor()
json_data = []
if request.method == 'GET':
cur.execute("select * from tasks order by id desc")
row_headers=[x[0] for x in cur.description]
for result in cur:
json_data.append(dict(zip(row_headers,result)))
if request.method == 'POST':
description = request.json['description']
cur.execute("insert into tasks (description) values (?)",[description])
json_data = { 'success': True }
if request.method == 'PUT':
id = request.json['id']
completed = request.json['completed']
cur.execute("update tasks set completed = ? where id = ?", [completed,id])
json_data = { 'success': True }
if request.method == 'DELETE':
id = request.args.get('id')
cur.execute("delete from tasks where id = ?",[id])
json_data = { 'success': True }
return json.dumps(json_data)
app.run(port=8080)