Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATA-3862 Update Go SDK to use new Binary Data ID #4841

Merged
merged 4 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 38 additions & 47 deletions app/data_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type BinaryData struct {
// BinaryMetadata is the metadata associated with binary data.
type BinaryMetadata struct {
ID string
BinaryDataID string
CaptureMetadata CaptureMetadata
TimeRequested time.Time
TimeReceived time.Time
Expand Down Expand Up @@ -572,10 +573,10 @@ func (d *DataClient) BinaryDataByFilter(
}

// BinaryDataByIDs queries binary data and metadata based on given IDs.
func (d *DataClient) BinaryDataByIDs(ctx context.Context, binaryIDs []*BinaryID) ([]*BinaryData, error) {
func (d *DataClient) BinaryDataByIDs(ctx context.Context, binaryDataIDs []string) ([]*BinaryData, error) {
resp, err := d.dataClient.BinaryDataByIDs(ctx, &pb.BinaryDataByIDsRequest{
IncludeBinary: true,
BinaryIds: binaryIDsToProto(binaryIDs),
BinaryDataIds: binaryDataIDs,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -619,9 +620,9 @@ func (d *DataClient) DeleteBinaryDataByFilter(ctx context.Context, filter *Filte

// DeleteBinaryDataByIDs deletes binary data based on given IDs.
// It returns the number of binary datapoints deleted.
func (d *DataClient) DeleteBinaryDataByIDs(ctx context.Context, binaryIDs []*BinaryID) (int, error) {
func (d *DataClient) DeleteBinaryDataByIDs(ctx context.Context, binaryDataIDs []string) (int, error) {
resp, err := d.dataClient.DeleteBinaryDataByIDs(ctx, &pb.DeleteBinaryDataByIDsRequest{
BinaryIds: binaryIDsToProto(binaryIDs),
BinaryDataIds: binaryDataIDs,
})
if err != nil {
return 0, err
Expand All @@ -630,10 +631,10 @@ func (d *DataClient) DeleteBinaryDataByIDs(ctx context.Context, binaryIDs []*Bin
}

// AddTagsToBinaryDataByIDs adds string tags, unless the tags are already present, to binary data based on given IDs.
func (d *DataClient) AddTagsToBinaryDataByIDs(ctx context.Context, tags []string, binaryIDs []*BinaryID) error {
func (d *DataClient) AddTagsToBinaryDataByIDs(ctx context.Context, tags, binaryDataIDs []string) error {
_, err := d.dataClient.AddTagsToBinaryDataByIDs(ctx, &pb.AddTagsToBinaryDataByIDsRequest{
BinaryIds: binaryIDsToProto(binaryIDs),
Tags: tags,
BinaryDataIds: binaryDataIDs,
Tags: tags,
})
return err
}
Expand All @@ -651,11 +652,11 @@ func (d *DataClient) AddTagsToBinaryDataByFilter(ctx context.Context, tags []str
// RemoveTagsFromBinaryDataByIDs removes string tags from binary data based on given IDs.
// It returns the number of binary files which had tags removed.
func (d *DataClient) RemoveTagsFromBinaryDataByIDs(ctx context.Context,
tags []string, binaryIDs []*BinaryID,
tags, binaryDataIDs []string,
) (int, error) {
resp, err := d.dataClient.RemoveTagsFromBinaryDataByIDs(ctx, &pb.RemoveTagsFromBinaryDataByIDsRequest{
BinaryIds: binaryIDsToProto(binaryIDs),
Tags: tags,
BinaryDataIds: binaryDataIDs,
Tags: tags,
})
if err != nil {
return 0, err
Expand Down Expand Up @@ -684,15 +685,15 @@ func (d *DataClient) RemoveTagsFromBinaryDataByFilter(ctx context.Context,
// All normalized coordinates (xMin, yMin, xMax, yMax) must be float values in the range [0, 1].
func (d *DataClient) AddBoundingBoxToImageByID(
ctx context.Context,
binaryID *BinaryID,
binaryDataID string,
label string,
xMinNormalized float64,
yMinNormalized float64,
xMaxNormalized float64,
yMaxNormalized float64,
) (string, error) {
resp, err := d.dataClient.AddBoundingBoxToImageByID(ctx, &pb.AddBoundingBoxToImageByIDRequest{
BinaryId: binaryIDToProto(binaryID),
BinaryDataId: binaryDataID,
Label: label,
XMinNormalized: xMinNormalized,
YMinNormalized: yMinNormalized,
Expand All @@ -709,11 +710,11 @@ func (d *DataClient) AddBoundingBoxToImageByID(
func (d *DataClient) RemoveBoundingBoxFromImageByID(
ctx context.Context,
bboxID string,
binaryID *BinaryID,
binaryDataID string,
) error {
_, err := d.dataClient.RemoveBoundingBoxFromImageByID(ctx, &pb.RemoveBoundingBoxFromImageByIDRequest{
BinaryId: binaryIDToProto(binaryID),
BboxId: bboxID,
BinaryDataId: binaryDataID,
BboxId: bboxID,
})
return err
}
Expand All @@ -731,7 +732,7 @@ func (d *DataClient) BoundingBoxLabelsByFilter(ctx context.Context, filter *Filt
}

// UpdateBoundingBox updates the bounding box for a given bbox ID for the file represented by the binary ID.
func (d *DataClient) UpdateBoundingBox(ctx context.Context, binaryID *BinaryID, bboxID string, opts *UpdateBoundingBoxOptions) error {
func (d *DataClient) UpdateBoundingBox(ctx context.Context, binaryDataID, bboxID string, opts *UpdateBoundingBoxOptions) error {
var label *string
var xMinNormalized, yMinNormalized, xMaxNormalized, yMaxNormalized *float64
if opts != nil {
Expand All @@ -743,7 +744,7 @@ func (d *DataClient) UpdateBoundingBox(ctx context.Context, binaryID *BinaryID,
}

_, err := d.dataClient.UpdateBoundingBox(ctx, &pb.UpdateBoundingBoxRequest{
BinaryId: binaryIDToProto(binaryID),
BinaryDataId: binaryDataID,
BboxId: bboxID,
Label: label,
XMinNormalized: xMinNormalized,
Expand Down Expand Up @@ -787,25 +788,25 @@ func (d *DataClient) ConfigureDatabaseUser(
// AddBinaryDataToDatasetByIDs adds the binary data with the given binary IDs to the dataset.
func (d *DataClient) AddBinaryDataToDatasetByIDs(
ctx context.Context,
binaryIDs []*BinaryID,
binaryDataIDs []string,
datasetID string,
) error {
_, err := d.dataClient.AddBinaryDataToDatasetByIDs(ctx, &pb.AddBinaryDataToDatasetByIDsRequest{
BinaryIds: binaryIDsToProto(binaryIDs),
DatasetId: datasetID,
BinaryDataIds: binaryDataIDs,
DatasetId: datasetID,
})
return err
}

// RemoveBinaryDataFromDatasetByIDs removes the binary data with the given binary IDs from the dataset.
func (d *DataClient) RemoveBinaryDataFromDatasetByIDs(
ctx context.Context,
binaryIDs []*BinaryID,
binaryDataIDs []string,
datasetID string,
) error {
_, err := d.dataClient.RemoveBinaryDataFromDatasetByIDs(ctx, &pb.RemoveBinaryDataFromDatasetByIDsRequest{
BinaryIds: binaryIDsToProto(binaryIDs),
DatasetId: datasetID,
BinaryDataIds: binaryDataIDs,
DatasetId: datasetID,
})
return err
}
Expand Down Expand Up @@ -853,7 +854,7 @@ func (d *DataClient) BinaryDataCaptureUpload(
SDBinary: binaryData,
}

response, err := d.dataCaptureUpload(ctx, metadata, []SensorData{sensorData})
response, err := d.dataCaptureUpload(ctx, metadata, []SensorData{sensorData}, true)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -911,7 +912,7 @@ func (d *DataClient) TabularDataCaptureUpload(
metadata.Tags = options.Tags
}
}
response, err := d.dataCaptureUpload(ctx, metadata, sensorContents)
response, err := d.dataCaptureUpload(ctx, metadata, sensorContents, false)
if err != nil {
return "", err
}
Expand All @@ -920,7 +921,9 @@ func (d *DataClient) TabularDataCaptureUpload(

// dataCaptureUpload uploads the metadata and contents for either tabular or binary data,
// and returns the file ID associated with the uploaded data and metadata.
func (d *DataClient) dataCaptureUpload(ctx context.Context, metadata UploadMetadata, sensorContents []SensorData) (string, error) {
func (d *DataClient) dataCaptureUpload(ctx context.Context, metadata UploadMetadata, sensorContents []SensorData,
isBinaryUpload bool,
) (string, error) {
sensorContentsPb, err := sensorContentsToProto(sensorContents)
if err != nil {
return "", err
Expand All @@ -932,6 +935,12 @@ func (d *DataClient) dataCaptureUpload(ctx context.Context, metadata UploadMetad
if err != nil {
return "", err
}
// Both tabular and binary data can be uploaded via this endpoint.
// If binary data is uploaded, the binary data id will be returned.
// If tabular data is uploaded, the file id will be returned.
if isBinaryUpload {
return resp.BinaryDataId, nil
}
return resp.FileId, nil
}

Expand Down Expand Up @@ -1016,7 +1025,7 @@ func (d *DataClient) StreamingDataCaptureUpload(
if err != nil {
return "", err
}
return resp.FileId, nil
return resp.BinaryDataId, nil
}

// FileUploadFromBytes uploads the contents and metadata for binary data such as encoded images or other data represented by bytes
Expand Down Expand Up @@ -1154,7 +1163,7 @@ func (d *DataClient) fileUploadStreamResp(metadata *syncPb.UploadMetadata, data
if err != nil {
return "", err
}
return resp.FileId, nil
return resp.BinaryDataId, nil
}

// CreateDataset makes a new dataset.
Expand Down Expand Up @@ -1324,6 +1333,7 @@ func binaryMetadataFromProto(proto *pb.BinaryMetadata) (*BinaryMetadata, error)
}
return &BinaryMetadata{
ID: proto.Id,
BinaryDataID: proto.BinaryDataId,
CaptureMetadata: *captureMetadata,
TimeRequested: proto.TimeRequested.AsTime(),
TimeReceived: proto.TimeReceived.AsTime(),
Expand Down Expand Up @@ -1353,25 +1363,6 @@ func tabularDataFromProto(proto *pb.TabularData, metadata *pb.CaptureMetadata) (
}, nil
}

func binaryIDToProto(binaryID *BinaryID) *pb.BinaryID {
if binaryID == nil {
return nil
}
return &pb.BinaryID{
FileId: binaryID.FileID,
OrganizationId: binaryID.OrganizationID,
LocationId: binaryID.LocationID,
}
}

func binaryIDsToProto(binaryIDs []*BinaryID) []*pb.BinaryID {
var protoBinaryIDs []*pb.BinaryID
for _, binaryID := range binaryIDs {
protoBinaryIDs = append(protoBinaryIDs, binaryIDToProto(binaryID))
}
return protoBinaryIDs
}

func filterToProto(filter *Filter) *pb.Filter {
if filter == nil {
return nil
Expand Down
Loading
Loading