|
11 | 11 | BugoutJournalEntries, |
12 | 12 | BugoutJournalEntryContent, |
13 | 13 | BugoutJournalEntryTags, |
| 14 | + BugoutSearchFields, |
| 15 | + BugoutSearchResults, |
14 | 16 | HolderType, |
15 | 17 | Method, |
16 | 18 | ) |
@@ -253,7 +255,7 @@ def delete_entry( |
253 | 255 | result = self._call(method=Method.delete, path=entry_id_path, headers=headers) |
254 | 256 | return BugoutJournalEntry(**result) |
255 | 257 |
|
256 | | - # Tags modules |
| 258 | + # Tags module |
257 | 259 | def get_most_used_tags(self, token: uuid.UUID, journal_id: uuid.UUID) -> List[Any]: |
258 | 260 | tags_path = f"journals/{journal_id}/tags" |
259 | 261 | headers = { |
@@ -322,3 +324,33 @@ def delete_tag( |
322 | 324 | method=Method.delete, path=tags_path, headers=headers, json=json |
323 | 325 | ) |
324 | 326 | return BugoutJournalEntryTags(**result) |
| 327 | + |
| 328 | + # Search module |
| 329 | + def _search_query(self, search_path: str, **queries: Dict[str, Any]) -> str: |
| 330 | + """ |
| 331 | + Validate search arguments with pydantic model BugoutSearchFields and |
| 332 | + generate search_path with queries. |
| 333 | + """ |
| 334 | + field_queries = BugoutSearchFields(**queries) |
| 335 | + fields_list = list(BugoutSearchFields.schema().get("properties").keys()) # type: ignore |
| 336 | + |
| 337 | + search_path += f"?{fields_list[0]}={getattr(field_queries, fields_list[0])}" |
| 338 | + for field in fields_list[1:]: |
| 339 | + attr = getattr(field_queries, field) |
| 340 | + if type(attr) is list: |
| 341 | + attr = ",".join(attr) |
| 342 | + search_path += f"&{field}={attr}" |
| 343 | + |
| 344 | + return search_path |
| 345 | + |
| 346 | + def search( |
| 347 | + self, token: uuid.UUID, journal_id: uuid.UUID, **queries: Dict[str, Any] |
| 348 | + ) -> BugoutSearchResults: |
| 349 | + search_path_org = f"journals/{journal_id}/search" |
| 350 | + search_path = self._search_query(search_path=search_path_org, **queries) |
| 351 | + |
| 352 | + headers = { |
| 353 | + "Authorization": f"Bearer {token}", |
| 354 | + } |
| 355 | + result = self._call(method=Method.get, path=search_path, headers=headers) |
| 356 | + return BugoutSearchResults(**result) |
0 commit comments