Skip to content

Commit 57ec5eb

Browse files
authored
[DATA-2034] Add file extensions to binary data capture upload (#466)
1 parent ebc64b9 commit 57ec5eb

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/viam/app/data_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ async def binary_data_capture_upload(
437437
method_parameters: Optional[Mapping[str, Any]] = None,
438438
tags: Optional[List[str]] = None,
439439
data_request_times: Optional[Tuple[datetime, datetime]] = None,
440+
file_extension: Optional[str] = None,
440441
) -> str:
441442
"""Upload binary sensor data.
442443
@@ -453,6 +454,8 @@ async def binary_data_capture_upload(
453454
tags (Optional[List[str]]): Optional list of tags to allow for tag-based data filtering when retrieving data.
454455
data_request_times (Optional[Tuple[datetime.datetime, datetime.datetime]]): Optional tuple containing `datetime`s objects
455456
denoting the times this data was requested[0] by the robot and received[1] from the appropriate sensor.
457+
file_extension (str): The file extension of binary data including the period, e.g. .jpg, .png, .pcd.
458+
The backend will route the binary to its corresponding mime type based on this extension.
456459
457460
Raises:
458461
GRPCError: If an invalid part ID is passed.
@@ -478,6 +481,8 @@ async def binary_data_capture_upload(
478481
method_parameters=method_parameters,
479482
tags=tags,
480483
)
484+
if file_extension:
485+
metadata.file_extension = file_extension if file_extension[0] == "." else f".{file_extension}"
481486
response = await self._data_capture_upload(metadata=metadata, sensor_contents=[sensor_contents])
482487
return response.file_id
483488

tests/test_data_sync_client.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from datetime import datetime
3-
from typing import List, cast, Mapping, Any
3+
from typing import List, Tuple, cast, Mapping, Any
4+
from google.protobuf.timestamp_pb2 import Timestamp
45

56
from grpclib.testing import ChannelFor
67

@@ -21,7 +22,7 @@
2122
BINARY_DATA = b"binary_data"
2223
METHOD_NAME = "method_name"
2324
DATETIMES = (datetime.now(), datetime.now())
24-
TIMESTAMPS = (datetime_to_timestamp(DATETIMES[0]), datetime_to_timestamp(DATETIMES[1]))
25+
TIMESTAMPS = cast(Tuple[Timestamp, Timestamp], (datetime_to_timestamp(DATETIMES[0]), datetime_to_timestamp(DATETIMES[1])))
2526
METHOD_PARAMETERS = {}
2627
TABULAR_DATA = [{"key": "value"}]
2728
FILE_NAME = "file_name"
@@ -51,11 +52,27 @@ async def test_binary_data_capture_upload(self, service: MockDataSync):
5152
tags=TAGS,
5253
data_request_times=DATETIMES,
5354
binary_data=BINARY_DATA,
55+
file_extension=".txt",
5456
)
5557
self.assert_sensor_contents(sensor_contents=list(service.sensor_contents), is_binary=True)
5658
self.assert_metadata(metadata=service.metadata)
59+
assert service.metadata.file_extension == ".txt"
5760
assert file_id == FILE_UPLOAD_RESPONSE
5861

62+
# Test extension dot prepend
63+
file_id = await client.binary_data_capture_upload(
64+
part_id=PART_ID,
65+
component_type=COMPONENT_TYPE,
66+
component_name=COMPONENT_NAME,
67+
method_name=METHOD_NAME,
68+
method_parameters=METHOD_PARAMETERS,
69+
tags=TAGS,
70+
data_request_times=DATETIMES,
71+
binary_data=BINARY_DATA,
72+
file_extension="txt",
73+
)
74+
assert service.metadata.file_extension == ".txt"
75+
5976
@pytest.mark.asyncio
6077
async def test_tabular_data_capture_upload(self, service: MockDataSync):
6178
async with ChannelFor([service]) as channel:

0 commit comments

Comments
 (0)