From 5bbb8ba5b3ef5f7f10289f69808c63c9003673a4 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Wed, 16 Feb 2022 13:36:11 +0100 Subject: [PATCH] Do not wrap the property in a json object See https://github.com/WebThingsIO/api/issues/143 --- src/server.rs | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/src/server.rs b/src/server.rs index 7d5681e..0d17138 100644 --- a/src/server.rs +++ b/src/server.rs @@ -470,9 +470,8 @@ async fn handle_get_property(req: HttpRequest, state: web::Data) -> Ht }; let thing = thing.read().unwrap(); - if thing.has_property(&property_name.to_string()) { - HttpResponse::Ok() - .json(json!({property_name: thing.get_property(&property_name.to_string()).unwrap()})) + if let Some(property) = thing.get_property(property_name) { + HttpResponse::Ok().json(json!(property)) } else { HttpResponse::NotFound().finish() } @@ -494,33 +493,14 @@ async fn handle_put_property( None => return HttpResponse::NotFound().finish(), }; - let args = match body.as_object() { - Some(args) => args, - None => { - return HttpResponse::BadRequest().json(bad_request( - "Parsing request failed", - Some(body.into_inner()), - )) - } - }; - - let arg = if let Some(arg) = args.get(property_name) { - arg - } else { - return HttpResponse::BadRequest().json(bad_request( - "Request does not contain property key", - Some(json!(args)), - )); - }; + let args = body.into_inner(); let mut thing = thing.write().unwrap(); - if thing.has_property(&property_name.to_string()) { - let set_property_result = thing.set_property(property_name.to_string(), arg.clone()); + if thing.has_property(property_name) { + let set_property_result = thing.set_property(property_name.to_string(), args.clone()); match set_property_result { - Ok(()) => HttpResponse::Ok().json( - json!({property_name: thing.get_property(&property_name.to_string()).unwrap()}), - ), + Ok(()) => HttpResponse::Ok().json(json!(thing.get_property(property_name).unwrap())), Err(err) => HttpResponse::BadRequest().json(bad_request(err, Some(json!(args)))), } } else {