Skip to content

Commit

Permalink
Implement file searching
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
JBorrow committed Jan 24, 2024
1 parent 99e551b commit 2955ee8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
61 changes: 59 additions & 2 deletions hera_librarian/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
The public-facing LibrarianClient object.
"""

from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING, Optional

Expand All @@ -11,6 +12,8 @@
from .deletion import DeletionPolicy
from .exceptions import LibrarianError, LibrarianHTTPError
from .models.ping import PingRequest, PingResponse
from .models.search import (FileSearchRequest, FileSearchResponse,
FileSearchResponses)
from .models.uploads import (UploadCompletionRequest, UploadInitiationRequest,
UploadInitiationResponse)
from .settings import ClientInfo
Expand Down Expand Up @@ -206,9 +209,11 @@ def upload(
local_path : Path
Path of the file or directory to upload.
dest_path : Path
The destination 'path' on the librarian store (often the same as your filename, but may be under some root directory).
The destination 'path' on the librarian store (often the same as your
filename, but may be under some root directory).
deletion_policy : DeletionPolicy | str, optional
Whether or not this file may be deleted, by default DeletionPolicy.DISALLOWED
Whether or not this file may be deleted, by default
DeletionPolicy.DISALLOWED
Returns
-------
Expand Down Expand Up @@ -295,3 +300,55 @@ def upload(
)

return

def search_files(
self,
name: Optional[str] = None,
create_time_window: Optional[tuple[datetime, ...]] = None,
uploader: Optional[str] = None,
source: Optional[str] = None,
max_results: int = 64,
) -> list[FileSearchResponse]:
"""
Search for files on this librarain.
Parameters
----------
name : Optional[str], optional
The name o files to search for, by default None
create_time_window : Optional[tuple[datetime, ...]], optional
A time window to search files within (make sure these are UTC
times), by default None
uploader : Optional[str], optional
The person who uploaded this file, by default None
source : Optional[str], optional
The source of this file, could be another librarian, by default None
max_results : int, optional
The maximal number of results., by default 64. Note that this can be
lower as it is also set by the server.
Returns
-------
list[FileSearchResponse]
A list of files that match the query.
"""

try:
response: FileSearchResponses = self.post(
endpoint="search/file",
request=FileSearchRequest(
name=name,
create_time_window=create_time_window,
uploader=uploader,
source=source,
max_results=max_results,
),
response=FileSearchResponses,
)
except LibrarianHTTPError as e:
if e.status_code == 404 and e.reason == "No files found.":
return []
else:
raise e

return response.root
2 changes: 1 addition & 1 deletion librarian_background/send_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from schedule import CancelJob
from pathlib import Path

from librarian_server.database import get_session()
from librarian_server.database import get_session
from librarian_server.orm import (
StoreMetadata,
Instance,
Expand Down
15 changes: 15 additions & 0 deletions tests/integration_test/test_upload_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@


def test_upload_simple(librarian_client, garbage_file, server):
"""
Also tests file searching.
"""
# Perform the upload
librarian_client.upload(garbage_file, Path("test_file"))

Expand All @@ -29,6 +32,18 @@ def test_upload_simple(librarian_client, garbage_file, server):

assert real_file_contents == garbage_file_contents

search_result = librarian_client.search_files(name="test_file")

assert len(search_result) == 1

assert search_result[0].name == "test_file"
assert search_result[0].size == 1024

assert search_result[0].instances[0].path == real_file_path





def test_upload_file_to_unique_directory(librarian_client, garbage_file, server):
librarian_client.upload(
Expand Down

0 comments on commit 2955ee8

Please sign in to comment.