-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
520 lines (394 loc) · 15.2 KB
/
api_client.py
File metadata and controls
520 lines (394 loc) · 15.2 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import requests
class APIClient:
def __init__(self, base_url: str) -> None:
"""Initializes the APIClient.
Args:
base_url (str): The base URL for the API.
"""
self.base_url = (base_url or "").rstrip("/")
self.session = requests.Session()
# ====================
# API Client Handlers
# ====================
# === User ===
def verify(self, token: str) -> dict:
"""Verifies the user token.
Args:
token (str): The access token to verify.
Returns:
dict: The JSON response from the API containing user data.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("GET",
url=self.base_url + "/auth/user",
headers=headers
)
def get_user_data(self, token: str) -> dict:
"""Retrieves user data using the provided token.
Args:
token (str): The access token.
Returns:
dict: The JSON response containing user data.
"""
return self.verify(token)
# === Login ===
def login(self, email: str, password: str) -> dict:
"""Authenticates a user.
Args:
email (str): The user's email address.
password (str): The user's password.
Returns:
dict: The JSON response containing authentication tokens.
"""
payload = {"email": email, "password": password}
return self._request("POST",
url=self.base_url + "/auth/login",
json=payload
)
# === Signup ===
def signup(self, code: str, email: str, password: str) -> dict:
"""Completes the signup process by verifying the code.
Args:
code (str): The verification code sent to email.
email (str): The user's email address.
password (str): The desired password.
Returns:
dict: The JSON response confirming signup.
"""
payload = {"code": code, "email": email, "password": password}
return self._request("PATCH",
url=self.base_url + "/auth/signup/verify-code",
json=payload
)
def signup_send_code(self, email: str) -> dict:
"""Sends a signup verification code to the provided email.
Args:
email (str): The email address to send the code to.
Returns:
dict: The JSON response confirming the code was sent.
"""
payload = {"email": email}
return self._request("POST",
url=self.base_url + "/auth/signup/send-code",
json=payload
)
# === Token ===
def refresh(self, refresh_token: str) -> dict:
"""Refreshes the access token.
Args:
refresh_token (str): The refresh token.
Returns:
dict: The JSON response containing new tokens.
"""
payload = {"refresh_token": refresh_token}
return self._request("POST",
url=self.base_url + "/auth/token/refresh",
json=payload
)
# === Reset Password ===
def request_reset_password(self, email: str) -> dict:
"""Requests a password reset link/code.
Args:
email (str): The email address associated with the account.
Returns:
dict: The JSON response confirming the request.
"""
payload = {"email": email}
return self._request("POST",
url=self.base_url + "/auth/forgot-password/request-reset",
json=payload
)
def reset_password_confirm_email(self, email: str, code: str) -> dict:
"""Confirms the email for password reset using a code.
Args:
email (str): The user's email address.
code (str): The verification code.
Returns:
dict: The JSON response confirming email verification.
"""
payload = {"email": email, "code": code}
return self._request("POST",
url=self.base_url + "/auth/forgot-password/confirm-email",
json=payload
)
def reset_password(self, reset_token: str, password: str) -> dict:
"""Resets the password using a reset token.
Args:
reset_token (str): The token received after email confirmation.
password (str): The new password.
Returns:
dict: The JSON response confirming the password reset.
"""
payload = {"reset_token": reset_token, "password": password}
return self._request("PATCH",
url=self.base_url + "/auth/forgot-password/reset-password",
json=payload
)
# === App Data ===
def get_departments(self, token: str = None) -> dict:
"""Retrieves the list of departments (groups).
Returns:
dict: The JSON response containing the list of departments.
"""
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
return self._request("GET", url=self.base_url + "/app/groups", headers=headers)
def create_department(self, data: dict, token: str) -> dict:
"""Creates a new department.
Args:
data (dict): The department name.
Returns:
dict: The JSON response confirming the creation.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("POST",
url=self.base_url + "/app/groups",
headers=headers,
json=data
)
def edit_department(self, data: dict, token: str, department_id: int) -> dict:
"""Edits an existing department.
Args:
data (dict): The updated department name.
department_id (int): The ID of the department to edit.
Returns:
dict: The JSON response confirming the edit.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("PATCH",
url=self.base_url + f"/app/groups/{department_id}",
headers=headers,
json=data
)
def delete_department(self, token: str, department_id: int) -> dict:
"""Deletes a department.
Args:
department_id (int): The ID of the department.
Returns:
dict: The JSON response confirming the deletion.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("DELETE",
url=self.base_url + f"/app/groups/{department_id}",
headers=headers
)
def get_categories(self, token: str = None) -> dict:
"""Retrieves the list of categories.
Returns:
dict: The JSON response containing the list of categories.
"""
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
return self._request("GET", url=self.base_url + "/app/categories", headers=headers)
def create_category(self, data: dict, token: str) -> dict:
"""Creates a new category.
Returns:
dict: The JSON response confirming the creation.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("POST",
url=self.base_url + "/app/categories",
headers=headers,
json=data
)
def edit_category(self, data: dict, token: str, category_id: int) -> dict:
"""Edits an existing category.
Args:
data (dict): The updated category name.
category_id (int): The ID of the category to edit.
Returns:
dict: The JSON response confirming the edit.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("PATCH",
url=self.base_url + f"/app/categories/{category_id}",
headers=headers,
json=data
)
def delete_category(self, token: str, category_id: int) -> dict:
"""Deletes a category.
Args:
category_id (int): The ID of the category.
Returns:
dict: The JSON response confirming the deletion.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("DELETE",
url=self.base_url + f"/app/categories/{category_id}",
headers=headers
)
def get_documents(self, category_id: int = None, group_id: int = None, limit: int = 50, offset: int = 0, token: str = None) -> dict:
"""Retrieves the list of documents.
Returns:
dict: The JSON response containing the list of documents.
"""
params = {"limit": limit, "offset": offset}
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
if category_id is not None:
params["category_id"] = category_id
if group_id is not None:
params["group_id"] = group_id
return self._request("GET", url=self.base_url + "/app/documents", params=params, headers=headers)
def get_document(self, document_id: int) -> dict:
"""
Retrieves a specific document by its ID using a direct API call.
"""
url = self.base_url + f"/app/documents/{document_id}"
response = self._request("GET", url)
return response
def get_document_pages(self, document_id: int) -> dict:
"""Retrieves the list of pages for a specific document.
Args:
document_id (int): The ID of the document.
Returns:
dict: The JSON response containing the list of pages.
"""
return self._request("GET",
url=self.base_url + f"/app/documents/{document_id}/pages"
)
def search_data(
self,
query: str,
group_id: int = None,
category_id: int = None,
tags: list[str] = None,
filters: dict = None,
token: str = None
) -> dict:
"""Searches the data based on the provided query.
Args:
category_id (int): The ID of the category.
group_id (int): The ID of the department (group).
query (str): The search query.
filters (dict): Search filters (exact_match, include_pages, etc).
Returns:
dict: The JSON response containing the search results.
"""
params = {"query": query, "tags": tags}
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
# Search scope
if group_id is not None:
params["group_id"] = group_id
if category_id is not None:
params["category_id"] = category_id
# Filters
if filters:
# Search settings
if filters.get("exact_match"):
params["exact_match"] = "true"
# Saerch pages
if not filters.get("include_pages", True):
params["include_pages"] = "false"
# Search in columns
search_fields = []
if filters.get("search_by_name"):
search_fields.append("name")
if filters.get("search_by_code"):
search_fields.append("code")
if search_fields:
params["search_fields"] = search_fields
return self._request("GET",
url=self.base_url + f"/app/search",
params=params,
headers=headers
)
def create_document(self, data: dict, token: str) -> dict:
"""Creates a new document.
Args:
data (dict): The document data.
Returns:
dict: The JSON response confirming the creation.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("POST",
url=self.base_url + "/app/documents",
headers=headers,
json=data
)
def update_document(self, document_id: int, data: dict, token: str) -> dict:
"""Updates a document.
Args:
document_id (int): The ID of the document to update.
data (dict): The updated document data.
Returns:
dict: The JSON response confirming the update.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("PATCH",
url=self.base_url + f"/app/documents/{document_id}",
headers=headers,
json=data
)
def delete_document(self, document_id: int, token: str) -> dict:
"""Deletes a document.
Args:
document_id (int): The ID of the document to delete.
Returns:
dict: The JSON response confirming the deletion.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("DELETE",
url=self.base_url + f"/app/documents/{document_id}",
headers=headers
)
# ====================
# Files
# ====================
def download_file(self, file_id: int, destination_path: str, token: str) -> None:
"""Downloads a file.
Args:
file_id (int): The ID of the file.
destination_path (str): The local path to save the file.
token (str): The access token.
"""
headers = {"Authorization": f"Bearer {token}"}
url = self.base_url + f"/app/files/{file_id}/download"
with self.session.get(url, headers=headers, stream=True) as response:
response.raise_for_status()
with open(destination_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
def upload_file(self, document_id: int, file_path: str, token: str) -> dict:
"""Uploads a file to the server.
Args:
document_id (int): The ID of the document.
file_path (str): The path to the file.
token (str): The access token.
Returns:
dict: The JSON response containing the file details.
"""
headers = {"Authorization": f"Bearer {token}"}
with open(file_path, "rb") as f:
files = {"file": f}
return self._request("POST",
url=self.base_url + f"/app/documents/{document_id}/files",
headers=headers,
files=files
)
def delete_file(self, file_id: int, token: str) -> dict:
"""Deletes a file.
Args:
file_id (int): The ID of the file.
token (str): The access token.
Returns:
dict: The JSON response confirming the deletion.
"""
headers = {"Authorization": f"Bearer {token}"}
return self._request("DELETE",
url=self.base_url + f"/app/files/{file_id}",
headers=headers
)
# ====================
# API Client Methods
# ====================
def _request(self, method: str, url: str, timeout: int = 10, **kwargs) -> dict:
"""Generic method for making API requests."""
request = self.session.request(method, url, timeout=timeout, **kwargs)
request.raise_for_status()
return request.json()