Skip to content

Commit 468babd

Browse files
authored
Add support for module identities and rewrite requests (Azure#158)
* Add support for module identities and rewrite requests * Fix formatting of invoke_method_builder.rs * Changed required fields for builders and removed execute() from getters
1 parent 606a99e commit 468babd

27 files changed

+1404
-877
lines changed

sdk/iothub/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ bytes = "1.0"
1111
chrono = "0.4"
1212
crypto-mac = { version = "0.10", features = ["std"] }
1313
hmac = "0.10"
14-
hyper = "0.14"
15-
hyper-tls = "0.5"
14+
http = "0.2"
1615
serde = { version = "1.0", features = ["derive"] }
1716
serde_json = "1.0"
1817
serde_derive = "1.0"
@@ -21,3 +20,6 @@ url = "2.2"
2120

2221
[dev-dependencies]
2322
tokio = { version = "1.0", features = ["macros"] }
23+
hyper = "0.14"
24+
hyper-rustls = "0.22"
25+
reqwest = "0.11.0"

sdk/iothub/examples/device_identity.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use iothub::service::identity::{DesiredCapability, Status};
1+
use std::sync::Arc;
2+
3+
use azure_core::HttpClient;
4+
use iothub::service::resources::{AuthenticationMechanism, DesiredCapability, Status};
25
use iothub::service::ServiceClient;
36
use std::error::Error;
47

58
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn Error>> {
9+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
710
let iothub_connection_string = std::env::var("IOTHUB_CONNECTION_STRING")
811
.expect("Set env variable IOTHUB_CONNECTION_STRING first!");
912

@@ -12,17 +15,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
1215
.expect("Please pass the device id as the first parameter");
1316

1417
println!("Getting device twin for device '{}'", device_id);
15-
16-
let service_client = ServiceClient::from_connection_string(iothub_connection_string, 3600)?;
18+
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
19+
let service_client =
20+
ServiceClient::from_connection_string(http_client, iothub_connection_string, 3600)?;
1721
let device = service_client
1822
.create_device_identity()
19-
.device_id(device_id)
20-
.authentication_using_sas(
21-
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
22-
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk=",
23+
.execute(
24+
&device_id,
25+
Status::Enabled,
26+
AuthenticationMechanism::new_using_symmetric_key(
27+
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
28+
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
29+
),
2330
)
24-
.status(Status::Enabled)
25-
.execute()
2631
.await?;
2732

2833
println!("Successfully created a new device '{}'", device.device_id);
@@ -33,14 +38,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
3338
);
3439
let device = service_client
3540
.update_device_identity(device.etag)
36-
.device_id(device.device_id)
3741
.device_capability(DesiredCapability::IotEdge)
38-
.authentication_using_sas(
39-
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
40-
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk=",
42+
.execute(
43+
&device_id,
44+
Status::Enabled,
45+
AuthenticationMechanism::new_using_symmetric_key(
46+
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
47+
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
48+
),
4149
)
42-
.status(Status::Disabled)
43-
.execute()
4450
.await?;
4551

4652
println!("Getting device identity of '{}'", device.device_id);
@@ -49,7 +55,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
4955

5056
println!("Deleting device '{}'", device.device_id);
5157
service_client
52-
.delete_device_identity(device.device_id, None)
58+
.delete_device_identity(device.device_id, device.etag)
59+
.execute()
5360
.await?;
5461

5562
Ok(())

sdk/iothub/examples/directmethod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use std::error::Error;
2+
use std::sync::Arc;
3+
4+
use azure_core::HttpClient;
15
use iothub::service::ServiceClient;
26
use serde_json;
3-
use std::error::Error;
47

58
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn Error>> {
9+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
710
let iothub_connection_string = std::env::var("IOTHUB_CONNECTION_STRING")
811
.expect("Set env variable IOTHUB_CONNECTION_STRING first!");
912

@@ -23,7 +26,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
2326
.nth(4)
2427
.expect("Please pass the payload as the fourth parameter");
2528

26-
let service_client = ServiceClient::from_connection_string(iothub_connection_string, 3600)?;
29+
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
30+
let service_client =
31+
ServiceClient::from_connection_string(http_client, iothub_connection_string, 3600)?;
2732
println!(
2833
"Sending direct method {} to {}:{} on: {}",
2934
method_name, device_id, module_id, service_client.iothub_name

sdk/iothub/examples/gettwin.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use std::sync::Arc;
2+
3+
use azure_core::HttpClient;
14
use iothub::service::ServiceClient;
25
use std::error::Error;
36

47
#[tokio::main]
5-
async fn main() -> Result<(), Box<dyn Error>> {
8+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
69
let iothub_connection_string = std::env::var("IOTHUB_CONNECTION_STRING")
710
.expect("Set env variable IOTHUB_CONNECTION_STRING first!");
811

@@ -12,7 +15,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
1215

1316
println!("Getting device twin for device: {}", device_id);
1417

15-
let service_client = ServiceClient::from_connection_string(iothub_connection_string, 3600)?;
18+
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
19+
let service_client =
20+
ServiceClient::from_connection_string(http_client, iothub_connection_string, 3600)?;
1621
let twin = service_client.get_device_twin(device_id).await?;
1722

1823
println!("Received device twin: {:?}", twin);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use std::sync::Arc;
2+
3+
use azure_core::HttpClient;
4+
use iothub::service::resources::AuthenticationMechanism;
5+
use iothub::service::ServiceClient;
6+
use std::error::Error;
7+
8+
#[tokio::main]
9+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
10+
let iothub_connection_string = std::env::var("IOTHUB_CONNECTION_STRING")
11+
.expect("Set env variable IOTHUB_CONNECTION_STRING first!");
12+
13+
let device_id = std::env::args()
14+
.nth(1)
15+
.expect("Please pass the device id as the first parameter");
16+
17+
let module_id = std::env::args()
18+
.nth(2)
19+
.expect("Please pass the module id as the second parameter");
20+
21+
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
22+
let service_client =
23+
ServiceClient::from_connection_string(http_client, iothub_connection_string, 3600)?;
24+
let module = service_client
25+
.create_module_identity()
26+
.execute(
27+
&device_id,
28+
&module_id,
29+
"IoTEdge",
30+
AuthenticationMechanism::new_using_symmetric_key(
31+
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
32+
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
33+
),
34+
)
35+
.await?;
36+
37+
println!(
38+
"Successfully created a new module '{}:{}'",
39+
module.device_id, module.module_id
40+
);
41+
42+
println!(
43+
"Setting status to disabled of module '{}:{}'",
44+
module.device_id, module.module_id
45+
);
46+
let module = service_client
47+
.update_module_identity(module.etag)
48+
.execute(
49+
&device_id,
50+
&module_id,
51+
"Docker",
52+
AuthenticationMechanism::new_using_symmetric_key(
53+
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
54+
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
55+
),
56+
)
57+
.await?;
58+
59+
println!(
60+
"Getting module identity of '{}:{}'",
61+
module.device_id, module.module_id
62+
);
63+
let module = service_client
64+
.get_module_identity(module.device_id, module.module_id)
65+
.await?;
66+
println!("Identity is: {:?}", module);
67+
68+
println!(
69+
"Deleting module '{}:{}'",
70+
module.device_id, module.module_id
71+
);
72+
service_client
73+
.delete_module_identity(module.device_id, module.module_id, module.etag)
74+
.execute()
75+
.await?;
76+
77+
Ok(())
78+
}

sdk/iothub/examples/updatetwin.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use azure_core::HttpClient;
12
use iothub::service::ServiceClient;
23
use serde_json;
34
use std::error::Error;
5+
use std::sync::Arc;
46

57
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn Error>> {
8+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
79
let iothub_connection_string = std::env::var("IOTHUB_CONNECTION_STRING")
810
.expect("Set env variable IOTHUB_CONNECTION_STRING first!");
911

@@ -16,8 +18,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
1618
.expect("Please pass the payload as the second parameter");
1719

1820
println!("Updating device twin for device: {}", device_id);
21+
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
1922

20-
let service_client = ServiceClient::from_connection_string(iothub_connection_string, 3600)?;
23+
let service_client =
24+
ServiceClient::from_connection_string(http_client, iothub_connection_string, 3600)?;
2125
let updated_twin = service_client
2226
.update_device_twin(device_id)
2327
.properties(serde_json::from_str(&payload)?)

sdk/iothub/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
#![deny(missing_docs)]
2+
//! The IoT Hub crate contains a client that can be used to manage the IoT Hub.
3+
4+
/// The service module contains the IoT Hub Service Client that can be used to manage the IoT Hub.
15
pub mod service;

0 commit comments

Comments
 (0)