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 } ;
2
5
use iothub:: service:: ServiceClient ;
3
6
use std:: error:: Error ;
4
7
5
8
#[ tokio:: main]
6
- async fn main ( ) -> Result < ( ) , Box < dyn Error > > {
9
+ async fn main ( ) -> Result < ( ) , Box < dyn Error + Send + Sync > > {
7
10
let iothub_connection_string = std:: env:: var ( "IOTHUB_CONNECTION_STRING" )
8
11
. expect ( "Set env variable IOTHUB_CONNECTION_STRING first!" ) ;
9
12
@@ -12,17 +15,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
12
15
. expect ( "Please pass the device id as the first parameter" ) ;
13
16
14
17
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 ) ?;
17
21
let device = service_client
18
22
. 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
+ ) ,
23
30
)
24
- . status ( Status :: Enabled )
25
- . execute ( )
26
31
. await ?;
27
32
28
33
println ! ( "Successfully created a new device '{}'" , device. device_id) ;
@@ -33,14 +38,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
33
38
) ;
34
39
let device = service_client
35
40
. update_device_identity ( device. etag )
36
- . device_id ( device. device_id )
37
41
. 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
+ ) ,
41
49
)
42
- . status ( Status :: Disabled )
43
- . execute ( )
44
50
. await ?;
45
51
46
52
println ! ( "Getting device identity of '{}'" , device. device_id) ;
@@ -49,7 +55,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
49
55
50
56
println ! ( "Deleting device '{}'" , device. device_id) ;
51
57
service_client
52
- . delete_device_identity ( device. device_id , None )
58
+ . delete_device_identity ( device. device_id , device. etag )
59
+ . execute ( )
53
60
. await ?;
54
61
55
62
Ok ( ( ) )
0 commit comments