-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticated.py
182 lines (131 loc) · 4.4 KB
/
authenticated.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
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
import requests
import sqlite3
import datetime
from dotenv import load_dotenv
import os
from general import resource_path
basedir = os.path.dirname(__file__)
db_path = os.path.join(basedir, 'db.db')
load_dotenv()
access_token = None
refresh_token = None
base_url = os.environ['BASE_URL']
global_headers = None
def set_creds():
global access_token
global refresh_token
global global_headers
try:
con = sqlite3.connect(resource_path('db.db'))
cursor = con.cursor()
res = cursor.execute(f"""
SELECT * FROM user
""")
data_tuple = res.fetchone()
if data_tuple is None:
return False
creds = {'access_token': data_tuple[1], 'refresh_token': data_tuple[2]}
global_headers = {
'Authorization': 'Bearer ' + creds['access_token']
}
access_token = True
refresh_token = creds['refresh_token']
con.commit()
con.close()
return True
except sqlite3.OperationalError:
return False
def get_request_response(response):
if response.status_code == 200:
data = response.json()
return data
elif response.status_code == 404:
return 404
else:
return -1
def create_list(list_data, missions_data):
if access_token is not None:
global global_headers
data = {
'container': list_data,
'tasks': missions_data
}
response = requests.post(f'{base_url}tasks/apis/new-task-list/',json=data, headers=global_headers)
if response.status_code == 201:
return 1
elif response.status_code == 400:
return 0
else: # In case status code where 500 or any other problem
return -1
def get_list(list_id):
if access_token is not None:
global global_headers
response = requests.get(f'{base_url}tasks/apis/get-tasks/{list_id}/', headers=global_headers)
return get_request_response(response)
def get_lists():
if access_token is not None:
global global_headers
response = requests.get(f'{base_url}tasks/apis/get-containers/', headers=global_headers)
keys = {
'list_id': 'id',
'title': 'title',
'date':'date',
'done': 'done'
}
return get_request_response(response), keys
def get_today_list():
if access_token is not None:
global global_headers
try:
today_date = datetime.datetime.today().strftime('%Y-%m-%d')
response = requests.get(f'{base_url}tasks/apis/today-tasks/{today_date}/', headers=global_headers)
if response.status_code == 200:
return response.json()['list']
if response.status_code == 404:
return {'title': '', 'tasks': []}
if response.status_code == 403 or response.status_code == 401:
logout()
return -1
except requests.exceptions.ConnectionError:
return -1
# return get_request_response(response)
def update_mission(mission_id, data):
if access_token is not None:
global global_headers
response = requests.patch(f'{base_url}tasks/apis/update-task/{mission_id}/', data=data, headers=global_headers)
if response.status_code == 202 or response.status_code == 200:
return 1
elif response.status_code == 400:
return 0
else:
return -1
def delete_list(list_id):
if access_token is not None:
global global_headers
response = requests.delete(f'{base_url}tasks/apis/delete-container/{list_id}/', headers=global_headers)
if response.status_code == 204:
return 1
else:
return -1
def logout():
global access_token
global refresh_token
global global_headers
try:
con = sqlite3.connect(resource_path('db.db'))
cursor = con.cursor()
res = cursor.execute(f"""
SELECT * FROM user
""")
data_tuple = res.fetchone()
cursor.execute(f"""
DELETE FROM user WHERE username="{data_tuple[0]}"
""")
con.commit()
con.close()
access_token = None
refresh_token = None
global_headers = None
return
except sqlite3.OperationalError:
return False