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

WoT compliance #62

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 6 additions & 7 deletions src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ pub trait Property: Send + Sync {
/// Returns a JSON value describing the property.
fn as_property_description(&self) -> serde_json::Map<String, serde_json::Value> {
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
}
Expand Down
54 changes: 16 additions & 38 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,21 +340,20 @@ async fn handle_get_things(req: HttpRequest, state: web::Data<AppState>) -> 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()));
Expand Down Expand Up @@ -393,18 +392,17 @@ async fn handle_get_thing(req: HttpRequest, state: web::Data<AppState>) -> 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(
Expand Down Expand Up @@ -472,9 +470,8 @@ async fn handle_get_property(req: HttpRequest, state: web::Data<AppState>) -> 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()
}
Expand All @@ -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 {
Expand Down
40 changes: 17 additions & 23 deletions src/thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,50 +324,45 @@ impl Thing for BaseThing {
json!(self.get_property_descriptions()),
);

let mut links: Vec<serde_json::Map<String, serde_json::Value>> = Vec::new();
let mut forms: Vec<serde_json::Map<String, serde_json::Value>> = 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),
},
]),
Expand All @@ -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),
},
]),
Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ 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
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