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..0d17138 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( @@ -472,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() } @@ -496,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 { diff --git a/src/thing.rs b/src/thing.rs index 755d7ec..087a83c 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("type".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), }, ]), 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