-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask_list.py
79 lines (63 loc) · 2.54 KB
/
task_list.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
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
import requests
import datetime
class TaskList:
def __init__(self, url="http://localhost:5000", selected_task=None):
self.url = url
self.selected_task = selected_task
def create_task(self,title="Default Task",description="Default Description",completed_at=None):
query_params = {
"title": title,
"description": description,
"completed_at": completed_at
}
response = requests.post(self.url+"/tasks",json=query_params)
return response.json()
def list_tasks(self):
response = requests.get(self.url+"/tasks")
return response.json()
def get_task(self, title=None, id=None):
for task in self.list_tasks():
if title:
if task["title"]==title:
id = task["id"]
self.selected_task = task
elif id == task["id"]:
self.selected_task = task
if self.selected_task == None:
return "Could not find task by that name or id"
response = requests.get(self.url+f"/tasks/{id}")
return response.json()
def update_task(self,title=None,description=None):
if not title:
title = self.selected_task["title"]
if not description:
description = self.selected_task["description"]
query_params = {
"title": title,
"description": description
#"completed_at": self.selected_task["is_complete"]
}
response = requests.put(
self.url+f"/tasks/{self.selected_task['id']}",
json=query_params
)
print("response:", response)
self.selected_task = response.json()["task"]
return response.json()
def delete_task(self):
response = requests.delete(self.url+f"/tasks/{self.selected_task['id']}")
self.selected_task = None
return response.json()
def mark_complete(self):
response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_complete")
self.selected_task = response.json()["task"]
return response.json()
def mark_incomplete(self):
response = requests.patch(self.url+f"/tasks/{self.selected_task['id']}/mark_incomplete")
self.selected_task = response.json()["task"]
return response.json()
def print_selected(self):
if self.selected_task:
print(f"Task with id {self.selected_task['id']} is currently selected")
else:
print("There is no selected task.")