Skip to content

Commit a5606d4

Browse files
committed
Improve Uber example
1 parent 749d56c commit a5606d4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

examples/uber.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,33 @@ fn main() {
1818
match uber::products::get(query_params) {
1919
Ok(result) => match result.body() {
2020
OkBody::Status200(body) => {
21-
let list = body.products.unwrap_or(vec![]);
22-
println!("First product: {:?}", list[0]);
21+
let list = body.products
22+
.unwrap_or(vec![]);
23+
24+
if list.len() > 0 {
25+
let first = &list[0];
26+
let default = String::from("Unknown");
27+
let first_id = first.product_id
28+
.as_ref()
29+
.unwrap_or(&default);
30+
31+
println!("First product: {}", first_id);
32+
} else {
33+
println!("No products!");
34+
}
2335
},
2436
OkBody::UnspecifiedCode(body) => println!(
2537
"Grr.. the server returned something not in its schema: {}",
2638
body
2739
)
2840
},
2941
Err(result) => match result.body() {
30-
ErrBody::UnspecifiedCode(body) => println!(
31-
"Error message: {:?}",
32-
body.message
33-
.unwrap_or(String::from("[No message]"))
34-
),
42+
ErrBody::UnspecifiedCode(body) => {
43+
let message = body.message
44+
.unwrap_or(String::from("[None given]"));
45+
46+
println!("Error message: {}", message);
47+
},
3548
ErrBody::NetworkFailure() => println!("Request failed!"),
3649
},
3750
}

0 commit comments

Comments
 (0)