async fn update_twin<S, T, U, V>(
    service_client: &ServiceClient,
    method: Method,
    device_id: S,
    module_id: Option<T>,
    if_match: Option<U>,
    desired_twin: DesiredTwin,
) -> Result<V, AzureError>
where
    S: Into<String>,
    T: Into<String>,
    U: Into<String>,
    for<'de> V: Deserialize<'de>,
{
    let uri = match module_id {
        Some(val) => format!(
            "https://{}.azure-devices.net/twins/{}/modules/{}?api-version={}",
            service_client.iothub_name,
            device_id.into(),
            val.into(),
            API_VERSION
        ),
        None => format!(
            "https://{}.azure-devices.net/twins/{}?api-version={}",
            service_client.iothub_name,
            device_id.into(),
            API_VERSION
        ),
    };

    let https = HttpsConnector::new();
    let client = Client::builder().build::<_, hyper::Body>(https);
    let mut request_builder = Request::builder()
        .uri(uri)
        .method(method)
        .header("Authorization", &service_client.sas_token)
        .header("Content-Type", "application/json");

    if let Some(if_match) = if_match {
        request_builder = request_builder.header("If-Match", if_match.into());
    }

    let request =
        request_builder.body(Body::from(serde_json::to_string(&desired_twin.contents)?))?;
    let (status_code, _, whole_body) =
        extract_status_headers_and_body(client.request(request)).await?;

    if !status_code.is_success() {
        return Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult::new(
            StatusCode::OK,
            status_code,
            std::str::from_utf8(&whole_body)?,
        )));
    }

    Ok(serde_json::from_slice(&whole_body)?)
}
/// Helper function for updating the device/module twin
Helper function for updating the device/module twin
Helper function for updating the device / module twin
async fn update_twin<S, T, U, V>(
    service_client: &ServiceClient,
    method: Method,
    device_id: S,
    module_id: Option<T>,
    if_match: Option<U>,
    desired_twin: DesiredTwin,
) -> Result<V, AzureError>
where
    S: Into<String>,
    T: Into<String>,
    U: Into<String>,
    for<'de> V: Deserialize<'de>,
{
    let uri = match module_id {
        Some(val) => format!(
            "https://{}.azure-devices.net/twins/{}/modules/{}?api-version={}",
            service_client.iothub_name,
            device_id.into(),
            val.into(),
            API_VERSION
        ),
        None => format!(
            "https://{}.azure-devices.net/twins/{}?api-version={}",
            service_client.iothub_name,
            device_id.into(),
            API_VERSION
        ),
    };
    let https = HttpsConnector::new();
    let client = Client::builder().build::<_, hyper::Body>(https);
    let mut request_builder = Request::builder()
        .uri(uri)
        .method(method)
        .header("Authorization", &service_client.sas_token)
        .header("Content-Type", "application/json");
    if let Some(if_match) = if_match {
        request_builder = request_builder.header("If-Match", if_match.into());
    }
    let request =
        request_builder.body(Body::from(serde_json::to_string(&desired_twin.contents)?))?;
    let (status_code, _, whole_body) =
        extract_status_headers_and_body(client.request(request)).await?;
    if !status_code.is_success() {
        return Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult::new(
            StatusCode::OK,
            status_code,
            std::str::from_utf8(&whole_body)?,
        )));
    }
    Ok(serde_json::from_slice(&whole_body)?)
}
async fn update_twin < S , T , U , V > ( service_client : & ServiceClient , method : Method , device_id : S , module_id : Option < T > , if_match : Option < U > , desired_twin : DesiredTwin , ) -> Result < V , AzureError > where S : Into < String > , T : Into < String > , U : Into < String > , for < ' de > V : Deserialize < ' de > , { let uri = match module_id { Some ( val ) => format ! ( " " service_client iothub_name device_id into ( ) val into ( ) API_VERSION ) , None => format ! ( " " service_client iothub_name device_id into ( ) API_VERSION ) , } ; let https = HttpsConnector :: new ( ) ; let client = Client :: builder ( ) . build :: < _ , hyper :: Body > ( https ) ; let mut request_builder = Request :: builder ( ) . uri ( uri ) . method ( method ) . header ( " " , & service_client . sas_token ) . header ( " " , " " ) ; if let Some ( if_match ) = if_match { request_builder = request_builder . header ( " " , if_match . into ( ) ) ; } let request = request_builder . body ( Body :: from ( serde_json :: to_string ( & desired_twin . contents ) ? ) ) ? ; let ( status_code , _ , whole_body ) = extract_status_headers_and_body ( client . request ( request ) ) . await ? ; if ! status_code . is_success ( ) { return Err ( AzureError :: UnexpectedHTTPResult ( UnexpectedHTTPResult :: new ( StatusCode :: OK , status_code , std :: str :: from_utf8 ( & whole_body ) ? , ) ) ) ; } Ok ( serde_json :: from_slice ( & whole_body ) ? ) }
Helper function for updating the device/module twin
Helper function for updating the device / module twin
service_client
&ServiceClient
method
Method
device_id
S
module_id
Option<T>
if_match
Option<U>
desired_twin
DesiredTwin