use crate::{errors, PiShocker};
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct PiShockAccount {
app_name: String,
api_username: String,
api_key: String,
}
impl PiShockAccount {
#[must_use]
pub fn new<S: Into<String>>(api_name: S, api_username: S, api_key: S) -> PiShockAccount {
PiShockAccount {
app_name: api_name.into(),
api_username: api_username.into(),
api_key: api_key.into(),
}
}
pub async fn get_shocker<S: Into<String>>(
&self,
share_code: S,
) -> Result<PiShocker, errors::PiShockError> {
let mut pishock_instance = self.get_shocker_without_verification(share_code).await?;
pishock_instance.refresh_metadata().await?;
if pishock_instance.get_shocker_online().is_some()
&& pishock_instance.get_shocker_online().unwrap_or(false)
{
return Err(errors::PiShockError::ShockerOffline);
}
Ok(pishock_instance)
}
pub async fn get_shocker_without_verification<S: Into<String>>(
&self,
share_code: S,
) -> Result<PiShocker, errors::PiShockError> {
let pishock_instance = PiShocker::new(
share_code.into(),
self.api_key.clone(),
self.api_username.clone(),
self.app_name.clone(),
);
Ok(pishock_instance)
}
}