Skip to content

Commit 87ff8e0

Browse files
single endpoint for tags and labels
1 parent 3d69b6c commit 87ff8e0

2 files changed

Lines changed: 34 additions & 61 deletions

File tree

src/handlers/http/datasets.rs

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -112,77 +112,56 @@ pub async fn get_datasets_by_tag(path: web::Path<String>) -> Result<HttpResponse
112112
}
113113

114114
#[derive(Debug, Deserialize)]
115-
pub struct PutTagsBody {
116-
pub tags: Vec<DatasetTag>,
115+
pub struct PutDatasetMetadataBody {
116+
pub tags: Option<Vec<DatasetTag>>,
117+
pub labels: Option<Vec<String>>,
117118
}
118119

119-
#[derive(Debug, Deserialize)]
120-
pub struct PutLabelsBody {
121-
pub labels: Vec<String>,
122-
}
123-
124-
/// PUT /api/v1/datasets/{name}/tags
125-
/// Replaces the dataset's tags with the provided list.
126-
pub async fn put_dataset_tags(
120+
/// PUT /api/v1/datasets/{name}
121+
/// Replaces the dataset's tags and/or labels.
122+
/// Only fields present in the body are updated; absent fields are left unchanged.
123+
pub async fn put_dataset_metadata(
127124
path: web::Path<String>,
128-
body: web::Json<PutTagsBody>,
125+
body: web::Json<PutDatasetMetadataBody>,
129126
) -> Result<HttpResponse, DatasetsError> {
130127
let dataset_name = path.into_inner();
131-
let new_tags: Vec<DatasetTag> = body
132-
.into_inner()
133-
.tags
134-
.into_iter()
135-
.collect::<HashSet<_>>()
136-
.into_iter()
137-
.collect();
128+
let body = body.into_inner();
138129

139130
let stream = PARSEABLE
140131
.get_stream(&dataset_name)
141132
.map_err(|_| DatasetsError::DatasetNotFound(dataset_name.clone()))?;
142133

143-
// Update storage first, then in-memory
144-
let storage = PARSEABLE.storage.get_object_store();
145-
let existing_labels = stream.get_dataset_labels();
146-
storage
147-
.update_dataset_tags_and_labels_in_stream(&dataset_name, &new_tags, &existing_labels)
148-
.await
149-
.map_err(DatasetsError::Storage)?;
150-
151-
stream.set_dataset_tags(new_tags.clone());
152-
153-
Ok(HttpResponse::Ok().json(serde_json::json!({ "tags": new_tags })))
154-
}
155-
156-
/// PUT /api/v1/datasets/{name}/labels
157-
/// Replaces the dataset's labels with the provided list.
158-
pub async fn put_dataset_labels(
159-
path: web::Path<String>,
160-
body: web::Json<PutLabelsBody>,
161-
) -> Result<HttpResponse, DatasetsError> {
162-
let dataset_name = path.into_inner();
163-
let new_labels: Vec<String> = body
164-
.into_inner()
165-
.labels
166-
.into_iter()
167-
.collect::<HashSet<_>>()
168-
.into_iter()
169-
.collect();
170-
171-
let stream = PARSEABLE
172-
.get_stream(&dataset_name)
173-
.map_err(|_| DatasetsError::DatasetNotFound(dataset_name.clone()))?;
134+
let final_tags = match body.tags {
135+
Some(tags) => tags
136+
.into_iter()
137+
.collect::<HashSet<_>>()
138+
.into_iter()
139+
.collect(),
140+
None => stream.get_dataset_tags(),
141+
};
142+
let final_labels = match body.labels {
143+
Some(labels) => labels
144+
.into_iter()
145+
.collect::<HashSet<_>>()
146+
.into_iter()
147+
.collect(),
148+
None => stream.get_dataset_labels(),
149+
};
174150

175151
// Update storage first, then in-memory
176152
let storage = PARSEABLE.storage.get_object_store();
177-
let existing_tags = stream.get_dataset_tags();
178153
storage
179-
.update_dataset_tags_and_labels_in_stream(&dataset_name, &existing_tags, &new_labels)
154+
.update_dataset_tags_and_labels_in_stream(&dataset_name, &final_tags, &final_labels)
180155
.await
181156
.map_err(DatasetsError::Storage)?;
182157

183-
stream.set_dataset_labels(new_labels.clone());
158+
stream.set_dataset_tags(final_tags.clone());
159+
stream.set_dataset_labels(final_labels.clone());
184160

185-
Ok(HttpResponse::Ok().json(serde_json::json!({ "labels": new_labels })))
161+
Ok(HttpResponse::Ok().json(serde_json::json!({
162+
"tags": final_tags,
163+
"labels": final_labels,
164+
})))
186165
}
187166

188167
#[derive(Debug, thiserror::Error)]

src/handlers/http/modal/server.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,9 @@ impl Server {
220220
.authorize_for_resource(Action::GetStreamInfo),
221221
)
222222
.route(
223-
"/{name}/tags",
223+
"/{name}",
224224
web::put()
225-
.to(http::datasets::put_dataset_tags)
226-
.authorize_for_resource(Action::CreateStream),
227-
)
228-
.route(
229-
"/{name}/labels",
230-
web::put()
231-
.to(http::datasets::put_dataset_labels)
225+
.to(http::datasets::put_dataset_metadata)
232226
.authorize_for_resource(Action::CreateStream),
233227
)
234228
}

0 commit comments

Comments
 (0)