use crate::errors::Error;
use crate::rest_api::RestApi;
use std::collections::HashMap;
impl RestApi {
pub async fn is_user_password_expired(&self, user_id: &str) -> Result<bool, Error> {
let url = format!(
"{}/sobjects/User/{}/password",
self.client.services_data_version_path(),
user_id
);
let params: Vec<(String, String)> = Vec::new();
let response = self.client.get_json(&url, ¶ms, &HashMap::new()).await?;
let response: HashMap<String, bool> = RestApi::handle_json_response(response).await?;
match response.get("isExpired") {
Some(is_expired) => Ok(*is_expired),
None => Err(Error::Other("Expected 'isExpired' property".to_string())),
}
}
pub async fn user_password_reset(&self, user_id: &str) -> Result<String, Error> {
let url = format!(
"{}/sobjects/User/{}/password",
self.client.services_data_version_path(),
user_id
);
let response = self.client.delete(&url, &HashMap::new()).await?;
let response: HashMap<String, String> = RestApi::handle_json_response(response).await?;
match response.get("NewPassword") {
Some(new_password) => Ok(new_password.to_string()),
None => Err(Error::Other("Expected 'NewPassword' property".to_string())),
}
}
pub async fn set_user_password(&self, user_id: &str, password: &str) -> Result<(), Error> {
let url = format!(
"{}/sobjects/User/{}/password",
self.client.services_data_version_path(),
user_id
);
let params = vec![("NewPassword", password)];
self.client.post_json(&url, ¶ms, &HashMap::new()).await?;
Ok(())
}
}