Skip to content
10 changes: 6 additions & 4 deletions nisystemlink/clients/file/_file_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ def __upload_file(

Args:
file: The file to upload.
metadata: JSON Dictionary with key/value pairs
metadata: Multipart part for file metadata, typically ``None`` or a
``(None, json_string, "application/json")`` tuple where
``json_string`` contains the metadata key/value pairs.
id: Specify an unique (among all file) 24-digit Hex string ID of the file once it is uploaded.
Defaults to None.
workspace: The id of the workspace the file belongs to. Defaults to None.
Expand Down Expand Up @@ -327,13 +329,13 @@ def upload_file(
ApiException: if unable to communicate with the File Service.
"""
if metadata:
metadata_str = json.dumps(metadata)
metadata_part = (None, json.dumps(metadata), "application/json")
else:
metadata_str = None
metadata_part = None
Comment thread
AMoldova-NI marked this conversation as resolved.

file_id = self.__upload_file(
file=file,
metadata=metadata_str,
metadata=metadata_part,
id=id,
Comment thread
AMoldova-NI marked this conversation as resolved.
workspace=workspace,
)
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/file/test_file_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ def test__api_info__returns(self, client: FileClient):
api_info = client.api_info()
assert len(api_info.model_dump()) != 0

def test__upload_file_with_metadata__succeeds(
self, client: FileClient, binary_file_data: BinaryIO, random_filename_extension: str
):
Comment thread
AMoldova-NI marked this conversation as resolved.
file_name = random_filename_extension
metadata = {"CustomProp": "CustomValue"}

# Upload the file with metadata
file_id = client.upload_file(file=binary_file_data, metadata=metadata)
Comment thread
AMoldova-NI marked this conversation as resolved.
Outdated
Comment thread
AMoldova-NI marked this conversation as resolved.
Outdated

# Verify the file was created with correct metadata
files = client.get_files(ids=[file_id])
assert files.total_count == 1
assert len(files.available_files) == 1
assert files.available_files[0].id == file_id
assert files.available_files[0].properties is not None
assert files.available_files[0].properties["Name"] == file_name
assert (
len(files.available_files[0].properties.keys()) == len(metadata) + 1
) # Name + 1 custom property
Comment thread
AMoldova-NI marked this conversation as resolved.

client.delete_file(id=file_id)

Comment thread
AMoldova-NI marked this conversation as resolved.
Outdated
# confirm that file was deleted
files = client.get_files(ids=[file_id])
assert files.total_count == 0

def test__upload_get_delete_files__succeeds(
self, client: FileClient, test_file, random_filename_extension
):
Expand Down
Loading