pub fn from_connection_string<S>(
        http_client: Arc<dyn HttpClient>,
        connection_string: S,
        expires_in_seconds: i64,
    ) -> azure_core::Result<Self>
    where
        S: AsRef<str>,
    {
        let parts: Vec<&str> = connection_string.as_ref().split(';').collect();
        let mut iot_hub_name: Option<&str> = None;
        let mut key_name: Option<&str> = None;
        let mut primary_key: Option<&str> = None;

        if parts.len() != 3 {
            return Err(Error::message(
                ErrorKind::Other,
                "given connection string is invalid",
            ));
        }

        for val in parts.iter() {
            let start = match val.find('=') {
                Some(size) => size + 1,
                None => continue,
            };

            if val.contains("HostName=") {
                let end = match val.find(".azure-devices.net") {
                    Some(size) => size,
                    None => continue,
                };
                iot_hub_name = Some(&val[start..end]);
            }

            if val.contains("SharedAccessKeyName=") {
                key_name = Some(&val[start..]);
            }

            if val.contains("SharedAccessKey=") {
                primary_key = Some(&val[start..]);
            }
        }

        let iot_hub_name = iot_hub_name.ok_or_else(|| {
            Error::message(
                ErrorKind::Other,
                "failed to get the hostname from the given connection string",
            )
        })?;

        let key_name = key_name.ok_or_else(|| {
            Error::message(
                ErrorKind::Other,
                "failed to get the shared access key name from the given connection string",
            )
        })?;

        let primary_key = primary_key.ok_or_else(|| {
            Error::message(
                ErrorKind::Other,
                "failed to get the primary key from the given connection string",
            )
        })?;

        let sas_token =
            Self::generate_sas_token(iot_hub_name, key_name, primary_key, expires_in_seconds)
                .context(ErrorKind::Other, "generate SAS token error")?;

        Ok(Self {
            http_client,
            iot_hub_name: iot_hub_name.to_string(),
            sas_token,
        })
    }
/// Create a new IoTHubService struct based on a given connection string
///
/// The connection string should preferably be from a user / group that has the rights to make service requests.
/// ```
/// use std::sync::Arc;
/// use azure_core::HttpClient;
/// use azure_iot_hub::service::ServiceClient;
///
/// let http_client = azure_core::new_http_client();
/// let connection_string = "HostName=cool-iot-hub.azure-devices.net;SharedAccessKeyName=iot_hubowner;SharedAccessKey=YSB2ZXJ5IHNlY3VyZSBrZXkgaXMgaW1wb3J0YW50Cg==";
///
/// let result = ServiceClient::from_connection_string(http_client, connection_string, 3600);
/// assert!(result.is_ok());
/// ```
Create a new IoTHubService struct based on a given connection string
The connection string should preferably be from a user / group that has the rights to make service requests.




Create a new IoTHubService struct based on a given connection string The connection string should preferably be from a user / group that has the rights to make service requests .
pub fn from_connection_string<S>(
        http_client: Arc<dyn HttpClient>,
        connection_string: S,
        expires_in_seconds: i64,
    ) -> azure_core::Result<Self>
    where
        S: AsRef<str>,
    {
        let parts: Vec<&str> = connection_string.as_ref().split(';').collect();
        let mut iot_hub_name: Option<&str> = None;
        let mut key_name: Option<&str> = None;
        let mut primary_key: Option<&str> = None;
        if parts.len() != 3 {
            return Err(Error::message(
                ErrorKind::Other,
                "given connection string is invalid",
            ));
        }
        for val in parts.iter() {
            let start = match val.find('=') {
                Some(size) => size + 1,
                None => continue,
            };
            if val.contains("HostName=") {
                let end = match val.find(".azure-devices.net") {
                    Some(size) => size,
                    None => continue,
                };
                iot_hub_name = Some(&val[start..end]);
            }
            if val.contains("SharedAccessKeyName=") {
                key_name = Some(&val[start..]);
            }
            if val.contains("SharedAccessKey=") {
                primary_key = Some(&val[start..]);
            }
        }
        let iot_hub_name = iot_hub_name.ok_or_else(|| {
            Error::message(
                ErrorKind::Other,
                "failed to get the hostname from the given connection string",
            )
        })?;
        let key_name = key_name.ok_or_else(|| {
            Error::message(
                ErrorKind::Other,
                "failed to get the shared access key name from the given connection string",
            )
        })?;
        let primary_key = primary_key.ok_or_else(|| {
            Error::message(
                ErrorKind::Other,
                "failed to get the primary key from the given connection string",
            )
        })?;
        let sas_token =
            Self::generate_sas_token(iot_hub_name, key_name, primary_key, expires_in_seconds)
                .context(ErrorKind::Other, "generate SAS token error")?;
        Ok(Self {
            http_client,
            iot_hub_name: iot_hub_name.to_string(),
            sas_token,
        })
    }
pub fn from_connection_string < S > ( http_client : Arc < dyn HttpClient > , connection_string : S , expires_in_seconds : i64 , ) -> azure_core :: Result < Self > where S : AsRef < str > , { let parts : Vec < & str > = connection_string . as_ref ( ) . split ( ';' ) . collect ( ) ; let mut iot_hub_name : Option < & str > = None ; let mut key_name : Option < & str > = None ; let mut primary_key : Option < & str > = None ; if parts . len ( ) != 3 { return Err ( Error :: message ( ErrorKind :: Other , " " , ) ) ; } for val in parts . iter ( ) { let start = match val . find ( '=' ) { Some ( size ) => size + 1 , None => continue , } ; if val . contains ( " " ) { let end = match val . find ( " " ) { Some ( size ) => size , None => continue , } ; iot_hub_name = Some ( & val [ start .. end ] ) ; } if val . contains ( " " ) { key_name = Some ( & val [ start .. ] ) ; } if val . contains ( " " ) { primary_key = Some ( & val [ start .. ] ) ; } } let iot_hub_name = iot_hub_name . ok_or_else ( | | { Error :: message ( ErrorKind :: Other , " " , ) } ) ? ; let key_name = key_name . ok_or_else ( | | { Error :: message ( ErrorKind :: Other , " " , ) } ) ? ; let primary_key = primary_key . ok_or_else ( | | { Error :: message ( ErrorKind :: Other , " " , ) } ) ? ; let sas_token = Self :: generate_sas_token ( iot_hub_name , key_name , primary_key , expires_in_seconds ) . context ( ErrorKind :: Other , " " ) ? ; Ok ( Self { http_client , iot_hub_name : iot_hub_name . to_string ( ) , sas_token , } ) }
Create a new IoTHubService struct based on a given connection string
The connection string should preferably be from a user / group that has the rights to make service requests.
Create a new IoTHubService struct based on a given connection string The connection string should preferably be from a user / group that has the rights to make service requests .
http_client
Arc<dyn HttpClient>
connection_string
S
expires_in_seconds
i64