Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
Latest commit
231 lines (197 loc) · 6.91 KB
/
Copy pathcli.py
File metadata and controls
231 lines (197 loc) · 6.91 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
fromPyInquirerimportprompt
importrequests
importclick
importos
importcotter
fromcotterimporttokenhandler
token_file_name="cotter_token.json"
# 1️⃣ Add your Cotter API KEY ID here
api_key="YOUR API KEY ID"
@click.group()
defmain():
""" A Todo List CLI """
pass
@main.command()
deflogin():
"""Login to use the API"""
# Add a file called `cotter_login_success.html`
# Call Cotter's api to login
port=8080# Select a port
response=cotter.login_with_email_link(api_key, port)
# 1️⃣ Add your Cotter API KEY ID here
url='https://cottertodolist.herokuapp.com/login_register'
headers= {'Content-Type': 'application/json'}
data=response
resp=requests.post(url, json=data, headers=headers)
ifresp.status_code!=200:
resp.raise_for_status()
tokenhandler.store_token_to_file(response["oauth_token"], token_file_name)
click.echo(resp.json())
@main.command()
@click.option('--name', prompt='List name', help='Name for your new todo list')
defcreate(name):
"""Create a todo list"""
# Get access token
access_token=tokenhandler.get_token_from_file(
token_file_name, api_key)["access_token"]
# Construct request
url="https://cottertodolist.herokuapp.com/list/create"
headers= {'Content-Type': 'application/json',
'Authorization': 'Bearer '+access_token}
data= {"name": name}
response=requests.post(url, json=data, headers=headers)
ifresponse.status_code!=200:
response.raise_for_status()
click.echo("List "+name+" created!")
@main.command()
defadd():
"""Create a todo task for a list"""
# Get access token from file
access_token=tokenhandler.get_token_from_file(
token_file_name, api_key)["access_token"]
# Get all todo lists for the user
url="https://cottertodolist.herokuapp.com/list"
headers= {'Authorization': 'Bearer '+access_token}
response=requests.get(url, headers=headers)
lists=response.json()
# Prompt to pick list
options=map(lambdax: x["name"], lists)
questions= [
{
'type': 'list',
'name': 'list_name',
'message': 'Add task to which list?',
'choices': options,
},
{
'type': 'input',
'name': 'task_name',
'message': 'Task description',
}
]
answers=prompt(questions)
ifnotanswers:
return
# Call API to create task fot the selected list
url="https://cottertodolist.herokuapp.com/todo/create"
headers= {'Content-Type': 'application/json',
'Authorization': 'Bearer '+access_token}
data= {
"name": answers['list_name'],
"task": answers['task_name']
}
response=requests.post(url, json=data, headers=headers)
ifresponse.status_code!=200:
response.raise_for_status()
click.echo("Task "+answers['task_name'] +
" is added in list "+answers['list_name'])
@main.command()
@click.option('-a', '--all', is_flag=True) # Make a boolean flag
defls(all):
"""Show lists"""
# Get access token from file
access_token=tokenhandler.get_token_from_file(
token_file_name, api_key)["access_token"]
# Get all lists
url="https://cottertodolist.herokuapp.com/list"
headers= {'Authorization': 'Bearer '+access_token}
response=requests.get(url, headers=headers)
ifresponse.status_code!=200:
response.raise_for_status()
listsFormatted=response.json()
ifall==True:
# Show all tasks in all lists
forchosenListinlistsFormatted:
click.echo('\n'+chosenList['name'])
fortaskinchosenList['tasks']:
iftask['done'] ==True:
click.echo("[✔] "+task['task'])
else:
click.echo("[ ] "+task['task'])
else:
# Show a prompt to choose a list
questions= [
{
'type': 'list',
'name': 'list',
'message': 'Which list do you want to see?',
'choices': list(map(lambdalst: lst['name'], listsFormatted))
},
]
answers=prompt(questions)
ifnotanswers:
return
# Get the chosen list
chosenList=list(
filter(lambdalst: lst['name'] ==answers['list'], listsFormatted))
iflen(chosenList) <=0:
click.echo("Invalid choice of list")
return
chosenList=chosenList[0]
# Show tasks in the chosen list
click.echo(chosenList['name'])
fortaskinchosenList['tasks']:
iftask['done'] ==True:
click.echo("[✔] "+task['task'])
else:
click.echo("[ ] "+task['task'])
@main.command()
deftoggle():
"""Update tasks in a list"""
# Get access token from file
access_token=tokenhandler.get_token_from_file(
token_file_name, api_key)["access_token"]
# Call API to list all tasks
url="https://cottertodolist.herokuapp.com/list"
headers= {'Authorization': 'Bearer '+access_token}
response=requests.get(url, headers=headers)
ifresponse.status_code!=200:
response.raise_for_status()
listsFormatted=response.json()
# Show a prompt to choose a list
questions= [
{
'type': 'list',
'name': 'list',
'message': 'Which list do you want to update?',
'choices': list(map(lambdalst: lst['name'], listsFormatted))
},
]
answers=prompt(questions)
ifnotanswers:
return
# Get the chosen list
chosenList=list(
filter(lambdalst: lst['name'] ==answers['list'], listsFormatted))
iflen(chosenList) <=0:
click.echo("Invalid choice of list")
return
chosenList=chosenList[0]
# Show an interactive checklist for the tasks
questions= [
{
'type': 'checkbox',
'message': chosenList['name'],
'name': chosenList['name'],
'choices': list(map(lambdatask: {'name': task['task'], 'checked': task["done"]}, chosenList['tasks'])),
}
]
answers=prompt(questions)
ifnotanswers:
return
# Call our Update API for each task in the list
# set `done` as True or False based on answers
fortaskinchosenList['tasks']:
url="https://cottertodolist.herokuapp.com/todo/update/done/"+ \
task['id']
headers= {'Content-Type': 'application/json',
'Authorization': 'Bearer '+access_token}
data= {
"done": task['task'] inanswers[chosenList['name']]
}
response=requests.put(url, json=data, headers=headers)
ifresponse.status_code!=200:
response.raise_for_status()
click.echo(answers)
if__name__=="__main__":
main()