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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
| #!/usr/bin/python3
import json
import os
import pymysql
DB_USER = os.getenv("DB_USER") or "tasksapi"
DB_PSWD = os.getenv("DB_PSWD") or "tasksapi"
DB_NAME = os.getenv("DB_NAME") or "tasksapi"
DB_HOST = os.getenv("DB_HOST") or "mariadb"
db = pymysql.connect(
host=DB_HOST,
user=DB_USER,
password=DB_PSWD,
database=DB_NAME,
cursorclass=pymysql.cursors.DictCursor,
)
cursor = db.cursor()
# Create a new task
def create_task(title):
try:
cursor.execute("INSERT INTO tasks (title) VALUES (%s)", title)
db.commit()
cursor.execute("SELECT MAX(id) AS id FROM tasks")
row = cursor.fetchone()
resp = get_task(row["id"])
return (resp[0], 201)
except Exception as e:
return (str(e), 500)
# Get all tasks
def get_tasks():
try:
cursor.execute(
"SELECT id, title, date_format(created, '%Y-%m-%d %H:%i') as created FROM tasks"
)
return (cursor.fetchall(), 200)
except Exception as e:
return (str(e), 500)
# Get an individual task
def get_task(id):
try:
cursor.execute(
"SELECT id, title, date_format(created, '%Y-%m-%d %H:%i') as created \
FROM tasks WHERE id="
+ str(id)
)
row = cursor.fetchone()
return (row if row is not None else "", 200 if row is not None else 404)
except Exception as e:
return ("", 404)
# Update an existing task
def update_task(id, title):
try:
cursor.execute("UPDATE tasks SET title=%s WHERE id=%s", (title, id))
db.commit()
return get_task(id)
except Exception as e:
return (str(e), 500)
# Delete an existing task
def delete_task(id):
try:
resp = get_task(id)
if resp[1] == 200:
cursor.execute("DELETE FROM tasks WHERE id=%s", id)
db.commit()
return ("", 200)
else:
return resp
except Exception as e:
return (str(e), 500)
# Returns the HTTP request method
def get_method():
return os.getenv("REQUEST_METHOD") or "GET"
# Returns the query string
def get_query_string():
query_string = os.getenv("QUERY_STRING") or ""
return query_string.replace("%20", " ").replace("%2F", "/").replace("+", " ")
# Returns the task ID if set in the request query string
def get_task_id():
query_string = get_query_string()
qs_parts = query_string.split("/")
return qs_parts[0] if qs_parts[0].isnumeric() else None
# Returns the task title from the query string if set
def get_task_title():
title = None
query_string = get_query_string()
if query_string != "":
qs_parts = query_string.split("/")
title = qs_parts[1] if len(qs_parts) > 1 else qs_parts[0]
title = None if title.isnumeric() else title
return title
# Returns True if title is valid, False otherwise
def title_is_valid(title):
return (
True
if isinstance(title, str) and len(title) >= 6 and len(title) <= 255
else False
)
# Returns a status code method
def get_status_msg(code):
msg = "OK"
msg = "Created" if code == 201 else msg
msg = "Not Found" if code == 404 else msg
msg = "Bad Request" if code == 400 else msg
msg = "Internal Server Error" if code == 500 else msg
return msg
method = get_method()
id = get_task_id()
title = get_task_title()
if method == "GET" and not id is None:
resp = get_task(id)
elif method == "GET":
resp = get_tasks()
elif method == "DELETE":
resp = delete_task(id)
elif not title_is_valid(title):
resp = ("", 400)
elif method == "POST":
resp = create_task(title)
elif method == "PUT":
resp = update_task(id, title)
print("Status: %d %s" % (resp[1], get_status_msg(resp[1])))
print("Content-type: application/json\n")
print(json.dumps(resp[0]))
|