Skip to content

Commit 6eb71b4

Browse files
committed
Added search handler
1 parent 255273d commit 6eb71b4

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

bugout/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
__email__ = "engineering@bugout.dev"
99
__license__ = "MIT"
10-
__version__ = "0.0.5"
10+
__version__ = "0.0.6"
1111

1212
__all__ = (
1313
"__author__",

bugout/app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,9 @@ def delete_tag(
301301
tag: str,
302302
) -> data.BugoutJournalEntryTags:
303303
return self.journal.delete_tag(token, journal_id, entry_id, tag)
304+
305+
# Search
306+
def search(
307+
self, token: uuid.UUID, journal_id: uuid.UUID, **queries: Dict[str, Any]
308+
) -> data.BugoutSearchResults:
309+
return self.journal.search(token, journal_id, **queries)

bugout/data.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,30 @@ class BugoutJournalEntryTags(BaseModel):
151151
journal_id: uuid.UUID
152152
entry_id: uuid.UUID
153153
tags: List[str]
154+
155+
156+
class BugoutSearchFields(BaseModel):
157+
query: str = ""
158+
filters: Optional[List[str]] = None
159+
limit: int = 10
160+
offset: int = 0
161+
content: Optional[bool] = True
162+
163+
164+
class BugoutSearchResult(BaseModel):
165+
entry_url: str
166+
content_url: str
167+
title: str
168+
content: Optional[str]
169+
tags: List[str]
170+
created_at: str
171+
updated_at: str
172+
score: float
173+
174+
175+
class BugoutSearchResults(BaseModel):
176+
total_results: int
177+
offset: int
178+
next_offset: Optional[int]
179+
max_score: float
180+
results: List[BugoutSearchResult]

bugout/journal.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
BugoutJournalEntries,
1212
BugoutJournalEntryContent,
1313
BugoutJournalEntryTags,
14+
BugoutSearchFields,
15+
BugoutSearchResults,
1416
HolderType,
1517
Method,
1618
)
@@ -253,7 +255,7 @@ def delete_entry(
253255
result = self._call(method=Method.delete, path=entry_id_path, headers=headers)
254256
return BugoutJournalEntry(**result)
255257

256-
# Tags modules
258+
# Tags module
257259
def get_most_used_tags(self, token: uuid.UUID, journal_id: uuid.UUID) -> List[Any]:
258260
tags_path = f"journals/{journal_id}/tags"
259261
headers = {
@@ -322,3 +324,33 @@ def delete_tag(
322324
method=Method.delete, path=tags_path, headers=headers, json=json
323325
)
324326
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

Comments
 (0)