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

Move IoT Hub to operation builder pattern #960

Merged
merged 14 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
87 changes: 48 additions & 39 deletions sdk/core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! setters {
(@single $name:ident : $typ:ty => $transform:expr) => {
#[allow(clippy::redundant_field_names)]
#[allow(clippy::needless_update)]
// TODO: Declare using idiomatic with_$name when https://github.com/Azure/azure-sdk-for-rust/issues/292 is resolved.
#[allow(missing_docs)]
pub fn $name<P: ::std::convert::Into<$typ>>(self, $name: P) -> Self {
let $name: $typ = $name.into();
Self {
Expand Down Expand Up @@ -123,6 +123,7 @@ macro_rules! setters {
macro_rules! operation {
// Construct the builder.
(@builder
$(#[$outer:meta])*
// The name of the operation and any generic params along with their constraints
$name:ident<$($generic:ident: $first_constraint:ident $(+ $constraint:ident)* ),* $(+ $lt:lifetime)?>,
// The client
Expand All @@ -139,6 +140,7 @@ macro_rules! operation {
) => {
azure_core::__private::paste! {
#[derive(Debug, Clone)]
$(#[$outer])*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pub struct [<$name Builder>]<$($generic)*> {
client: $client,
$($required: $rtype,)*
Expand Down Expand Up @@ -169,8 +171,45 @@ macro_rules! operation {
}
}
};
// `operation! { #[stream] ListUsers, client: UserClient, ?consistency_level: ConsistencyLevel }`
(#[stream] $(#[$outer:meta])* $name:ident,
client: $client:ty,
$($required:ident: $rtype:ty,)*
$(?$optional:ident: $otype:ty),*) => {
$crate::operation!{
@builder
$(#[$outer])*
$name<>,
client: $client,
@required
$($required: $rtype,)*
@optional
$($optional: $otype,)*
@nosetter
}
};
(#[stream] $(#[$outer:meta])*
$name:ident,
client: $client:ty,
$($required:ident: $rtype:ty,)*
$(?$optional:ident: $otype:ty,)*
$(#[skip]$nosetter:ident: $nstype:ty),*
) => {
$crate::operation!{
@builder
$(#[$outer])*
$name<>,
client: $client,
@required
$($required: $rtype,)*
@optional
$($optional: $otype,)*
@nosetter
$($nosetter: $nstype),*
}
};
// Construct a builder and the `Future` related code
($name:ident<$($generic:ident: $first_constraint:ident $(+ $constraint:ident)* ),* $(+ $lt:lifetime)?>,
($(#[$outer:meta])* $name:ident<$($generic:ident: $first_constraint:ident $(+ $constraint:ident)* ),* $(+ $lt:lifetime)?>,
client: $client:ty,
@required
$($required:ident: $rtype:ty,)*
Expand All @@ -180,7 +219,9 @@ macro_rules! operation {
$($nosetter:ident: $nstype:ty),*
) => {
$crate::operation! {
@builder $name<$($generic: $first_constraint $(+ $constraint)*),* $(+ $lt)*>,
@builder
$(#[$outer])*
$name<$($generic: $first_constraint $(+ $constraint)*),* $(+ $lt)*>,
client: $client,
@required
$($required: $rtype,)*
Expand All @@ -202,11 +243,12 @@ macro_rules! operation {
}
};
// `operation! { CreateUser, client: UserClient, ?consistency_level: ConsistencyLevel }`
($name:ident,
($(#[$outer:meta])* $name:ident,
client: $client:ty,
$($required:ident: $rtype:ty,)*
$(?$optional:ident: $otype:ty),*) => {
$crate::operation!{
$(#[$outer])*
$name<>,
client: $client,
@required
Expand All @@ -216,47 +258,14 @@ macro_rules! operation {
@nosetter
}
};
// `operation! { #[stream] ListUsers, client: UserClient, ?consistency_level: ConsistencyLevel }`
(#[stream] $name:ident,
client: $client:ty,
$($required:ident: $rtype:ty,)*
$(?$optional:ident: $otype:ty),*) => {
$crate::operation!{
@builder
$name<>,
client: $client,
@required
$($required: $rtype,)*
@optional
$($optional: $otype,)*
@nosetter
}
};
(#[stream] $name:ident,
client: $client:ty,
$($required:ident: $rtype:ty,)*
$(?$optional:ident: $otype:ty,)*
$(#[skip]$nosetter:ident: $nstype:ty),*
) => {
$crate::operation!{
@builder
$name<>,
client: $client,
@required
$($required: $rtype,)*
@optional
$($optional: $otype,)*
@nosetter
$($nosetter: $nstype),*
}
};
// `operation! { CreateDocument<D: Serialize>, client: UserClient, ?consistency_level: ConsistencyLevel, ??other_field: bool }`
($name:ident<$($generic:ident: $first_constraint:ident $(+ $constraint:ident)*),* $(+ $lt:lifetime)?>,
($(#[$outer:meta])* $name:ident<$($generic:ident: $first_constraint:ident $(+ $constraint:ident)*),* $(+ $lt:lifetime)?>,
client: $client:ty,
$($required:ident: $rtype:ty,)*
$(?$optional:ident: $otype:ty,)*
$(#[skip] $nosetter:ident: $nstype:ty),*) => {
$crate::operation!{
$(#[$outer])*
$name<$($generic: $first_constraint $(+ $constraint)*),* $(+ $lt)*>,
client: $client,
@required
Expand Down
1 change: 1 addition & 0 deletions sdk/iot_hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde_derive = "1.0"
sha2 = "0.10"
url = "2.2"
thiserror = "1.0"
futures = "0.3"

[dev-dependencies]
env_logger = "0.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
service_client
.apply_on_edge_device(device_id)
.modules_content(modules_content)
.execute()
.into_future()
.await?;

println!("Successfully applied the configuration");
Expand Down
16 changes: 10 additions & 6 deletions sdk/iot_hub/examples/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use azure_iot_hub::service::ServiceClient;
use azure_iot_hub::service::{resources::Configuration, ServiceClient};
use std::error::Error;

#[tokio::main]
Expand Down Expand Up @@ -29,7 +29,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
"metric1",
"SELECT deviceId FROM devices WHERE properties.reported.lastDesiredStatus.code = 200",
)
.execute()
.into_future()
.await?;

println!(
Expand All @@ -38,7 +38,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
);

println!("Getting configuration: {}", configuration_id);
let configuration = service_client.get_configuration(configuration_id).await?;
let configuration = service_client
.get_configuration(configuration_id)
.into_future()
.await?;
let configuration: Configuration = configuration.try_into()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a shame that we are now returning an unstructured type.


println!(
"Successfully retrieved the new configuration '{:?}'",
Expand All @@ -65,10 +69,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
}))
.labels(configuration.labels)
.metrics(configuration.metrics.queries)
.execute()
.into_future()
.await?;

let multiple_configurations = service_client.get_configurations().await?;
let multiple_configurations = service_client.get_configurations().into_future().await?;
println!(
"Successfully retrieved all configurations '{:?}'",
multiple_configurations
Expand All @@ -86,7 +90,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {

service_client
.delete_configuration(&configuration.id, configuration.etag)
.execute()
.into_future()
.await?;

println!(
Expand Down
20 changes: 13 additions & 7 deletions sdk/iot_hub/examples/device_identity.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use azure_iot_hub::service::resources::{AuthenticationMechanism, DesiredCapability, Status};
use azure_iot_hub::service::responses::DeviceIdentityResponse;
use azure_iot_hub::service::ServiceClient;
use std::error::Error;

Expand All @@ -16,15 +17,15 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let device = service_client
.create_device_identity()
.execute(
.create_device_identity(
&device_id,
Status::Enabled,
AuthenticationMechanism::new_using_symmetric_key(
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
),
)
.into_future()
.await?;

println!("Successfully created a new device '{}'", device.device_id);
Expand All @@ -34,26 +35,31 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
device.device_id
);
let device = service_client
.update_device_identity(device.etag)
.device_capability(DesiredCapability::IotEdge)
.execute(
.update_device_identity(
&device_id,
Status::Enabled,
AuthenticationMechanism::new_using_symmetric_key(
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
),
device.etag,
)
.device_capability(DesiredCapability::IotEdge)
.into_future()
.await?;

println!("Getting device identity of '{}'", device.device_id);
let device = service_client.get_device_identity(device.device_id).await?;
let device = service_client
.get_device_identity(device.device_id)
.into_future()
.await?;
let device: DeviceIdentityResponse = device.try_into()?;
println!("Identity is: {:?}", device);

println!("Deleting device '{}'", device.device_id);
service_client
.delete_device_identity(device.device_id, device.etag)
.execute()
.into_future()
.await?;

Ok(())
Expand Down
14 changes: 9 additions & 5 deletions sdk/iot_hub/examples/directmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
method_name, device_id, module_id, service_client.iot_hub_name
);

let direct_method =
service_client.create_module_method(device_id, module_id, method_name, 30, 30);
let direct_method = service_client.create_module_method(
serde_json::from_str(&payload)?,
device_id,
module_id,
method_name,
30,
30,
);

let response = direct_method
.execute(serde_json::from_str(&payload)?)
.await?;
let response = direct_method.into_future().await?;

println!(
"Received a response from the direct method with status code {} and payload {:?}",
Expand Down
5 changes: 4 additions & 1 deletion sdk/iot_hub/examples/gettwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let http_client = azure_core::new_http_client();
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let twin = service_client.get_device_twin(device_id).await?;
let twin = service_client
.get_device_twin(device_id)
.into_future()
.await?;

println!("Received device twin: {:?}", twin);

Expand Down
14 changes: 9 additions & 5 deletions sdk/iot_hub/examples/module_identity.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use azure_iot_hub::service::resources::AuthenticationMechanism;
use azure_iot_hub::service::responses::ModuleIdentityResponse;
use azure_iot_hub::service::ServiceClient;
use std::error::Error;

Expand All @@ -19,8 +20,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let service_client =
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;
let module = service_client
.create_module_identity()
.execute(
.create_module_identity(
&device_id,
&module_id,
"IoTEdge",
Expand All @@ -29,6 +29,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
),
)
.into_future()
.await?;

println!(
Expand All @@ -41,16 +42,17 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
module.device_id, module.module_id
);
let module = service_client
.update_module_identity(module.etag)
.execute(
.update_module_identity(
&device_id,
&module_id,
"Docker",
module.etag,
AuthenticationMechanism::new_using_symmetric_key(
"QhgevIUBSWe37q1MP+M/vtktjOcrE74BVbpcxlLQw58=",
"6YS6w5wqkpdfkEW7iOP1NvituehFlFRfPko2n7KY4Gk",
),
)
.into_future()
.await?;

println!(
Expand All @@ -59,7 +61,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
);
let module = service_client
.get_module_identity(module.device_id, module.module_id)
.into_future()
.await?;
let module: ModuleIdentityResponse = module.try_into()?;
println!("Identity is: {:?}", module);

println!(
Expand All @@ -68,7 +72,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
);
service_client
.delete_module_identity(module.device_id, module.module_id, module.etag)
.execute()
.into_future()
.await?;

Ok(())
Expand Down
8 changes: 4 additions & 4 deletions sdk/iot_hub/examples/query_iothub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
ServiceClient::from_connection_string(http_client, iot_hub_connection_string, 3600)?;

let response = service_client
.query()
.query(query)
.max_item_count(1)
.execute(query)
.into_future()
.await?;

println!(
Expand All @@ -32,10 +32,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
};

let response = service_client
.query()
.query(query)
.max_item_count(1)
.continuation(token)
.execute(query)
.into_future()
.await?;

println!(
Expand Down
Loading