From 60c55993560f7a705c35cb47c00dc6aec7c1b197 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Wed, 16 Feb 2022 13:24:15 +0100 Subject: [PATCH 1/4] Change links to forms --- src/property.rs | 13 ++++++------- src/server.rs | 22 ++++++++++------------ src/thing.rs | 40 +++++++++++++++++----------------------- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/property.rs b/src/property.rs index dbf188c..e3dfee1 100644 --- a/src/property.rs +++ b/src/property.rs @@ -46,20 +46,19 @@ pub trait Property: Send + Sync { /// Returns a JSON value describing the property. fn as_property_description(&self) -> serde_json::Map { let mut description = self.get_metadata(); - let link = json!( + let form = json!( { - "rel": "property", "href": self.get_href(), } ); - if let Some(links) = description - .get_mut("links") - .map(|links| links.as_array_mut().unwrap()) + if let Some(forms) = description + .get_mut("forms") + .map(|forms| forms.as_array_mut().unwrap()) { - links.push(link); + forms.push(form); } else { - description.insert("links".to_string(), json!([link])); + description.insert("forms".to_string(), json!([form])); } description } diff --git a/src/server.rs b/src/server.rs index 41cb6c7..7d5681e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -340,21 +340,20 @@ async fn handle_get_things(req: HttpRequest, state: web::Data) -> Http for thing in things.iter() { let thing = thing.read().unwrap(); - let mut link = serde_json::Map::new(); - link.insert("rel".to_owned(), json!("alternate")); - link.insert( + let mut form = serde_json::Map::new(); + form.insert( "href".to_owned(), json!(format!("{}{}", ws_href, thing.get_href())), ); let mut description = thing.as_thing_description().clone(); { - let links = description - .get_mut("links") + let forms = description + .get_mut("forms") .unwrap() .as_array_mut() .unwrap(); - links.push(json!(link)); + forms.push(json!(form)); } description.insert("href".to_owned(), json!(thing.get_href())); @@ -393,18 +392,17 @@ async fn handle_get_thing(req: HttpRequest, state: web::Data) -> HttpR thing.get_href() ); - let mut link = serde_json::Map::new(); - link.insert("rel".to_owned(), json!("alternate")); - link.insert("href".to_owned(), json!(ws_href)); + let mut form = serde_json::Map::new(); + form.insert("href".to_owned(), json!(ws_href)); let mut description = thing.as_thing_description(); { - let links = description - .get_mut("links") + let forms = description + .get_mut("forms") .unwrap() .as_array_mut() .unwrap(); - links.push(json!(link)); + forms.push(json!(form)); } description.insert( diff --git a/src/thing.rs b/src/thing.rs index 755d7ec..e7da527 100644 --- a/src/thing.rs +++ b/src/thing.rs @@ -324,50 +324,45 @@ impl Thing for BaseThing { json!(self.get_property_descriptions()), ); - let mut links: Vec> = Vec::new(); + let mut forms: Vec> = Vec::new(); - let mut properties_link = serde_json::Map::new(); - properties_link.insert("rel".to_owned(), json!("properties")); - properties_link.insert( + let mut properties_form = serde_json::Map::new(); + properties_form.insert( "href".to_owned(), json!(format!("{}/properties", self.get_href_prefix())), ); - links.push(properties_link); + forms.push(properties_form); - let mut actions_link = serde_json::Map::new(); - actions_link.insert("rel".to_owned(), json!("actions")); - actions_link.insert( + let mut actions_form = serde_json::Map::new(); + actions_form.insert( "href".to_owned(), json!(format!("{}/actions", self.get_href_prefix())), ); - links.push(actions_link); + forms.push(actions_form); - let mut events_link = serde_json::Map::new(); - events_link.insert("rel".to_owned(), json!("events")); - events_link.insert( + let mut events_form = serde_json::Map::new(); + events_form.insert( "href".to_owned(), json!(format!("{}/events", self.get_href_prefix())), ); - links.push(events_link); + forms.push(events_form); if let Some(ui_href) = self.get_ui_href() { - let mut ui_link = serde_json::Map::new(); - ui_link.insert("rel".to_owned(), json!("alternate")); - ui_link.insert("mediaType".to_owned(), json!("text/html")); - ui_link.insert("href".to_owned(), json!(ui_href)); - links.push(ui_link); + let mut ui_form = serde_json::Map::new(); + ui_form.insert("mediaType".to_owned(), json!("text/html")); + ui_form.insert("href".to_owned(), json!(ui_href)); + forms.push(ui_form); } - description.insert("links".to_owned(), json!(links)); + description.insert("forms".to_owned(), json!(forms)); let mut actions = serde_json::Map::new(); for (name, action) in self.available_actions.iter() { let mut metadata = action.get_metadata().clone(); metadata.insert( - "links".to_string(), + "forms".to_string(), json!([ { - "rel": "action", "href": format!("{}/actions/{}", self.get_href_prefix(), name), }, ]), @@ -381,10 +376,9 @@ impl Thing for BaseThing { for (name, event) in self.available_events.iter() { let mut metadata = event.get_metadata().clone(); metadata.insert( - "links".to_string(), + "forms".to_string(), json!([ { - "rel": "event", "href": format!("{}/events/{}", self.get_href_prefix(), name), }, ]), From 5bbb8ba5b3ef5f7f10289f69808c63c9003673a4 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Wed, 16 Feb 2022 13:36:11 +0100 Subject: [PATCH 2/4] 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 { From 5965c74591c8bc8b13faff3c904b3fa9545721d7 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 7 Mar 2022 13:53:12 +0100 Subject: [PATCH 3/4] Update the tester to check for the WoT-conforming things Requires https://github.com/WebThingsIO/webthing-tester/pull/5 --- test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.sh b/test.sh index 6f6eb0c..8809384 100755 --- a/test.sh +++ b/test.sh @@ -15,7 +15,7 @@ cargo build --example single-thing cargo run --example single-thing & EXAMPLE_PID=$! sleep 5 -./webthing-tester/test-client.py +./webthing-tester/test-client.py --flavor WoT kill -15 $EXAMPLE_PID # build and test the multiple-things example @@ -23,5 +23,5 @@ cargo build --example multiple-things cargo run --example multiple-things & EXAMPLE_PID=$! sleep 5 -./webthing-tester/test-client.py --path-prefix "/0" +./webthing-tester/test-client.py --path-prefix "/0" --flavor WoT kill -15 $EXAMPLE_PID From e713924ca81d344b6d66d2d99a9d64388178cb6b Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Tue, 8 Mar 2022 15:02:30 +0100 Subject: [PATCH 4/4] Replace mediaType with type See https://github.com/WebThingsIO/gateway/issues/2808 --- src/thing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/thing.rs b/src/thing.rs index e7da527..087a83c 100644 --- a/src/thing.rs +++ b/src/thing.rs @@ -349,7 +349,7 @@ impl Thing for BaseThing { if let Some(ui_href) = self.get_ui_href() { let mut ui_form = serde_json::Map::new(); - ui_form.insert("mediaType".to_owned(), json!("text/html")); + ui_form.insert("type".to_owned(), json!("text/html")); ui_form.insert("href".to_owned(), json!(ui_href)); forms.push(ui_form); }