use std::collections::HashMap;
use jmap_types::{Id, PatchObject};
use super::{GetResponse, SetResponse};
impl super::SessionClient {
pub async fn vacation_response_get(
&self,
) -> Result<jmap_mail_types::VacationResponse, jmap_base_client::ClientError> {
let envelope = self.vacation_response_get_envelope().await?;
envelope.list.into_iter().next().ok_or_else(|| {
jmap_base_client::ClientError::InvalidSession(
"VacationResponse/get returned an empty list; RFC 8621 §8 requires \
the singleton to exist for every account"
.into(),
)
})
}
pub async fn vacation_response_get_envelope(
&self,
) -> Result<GetResponse<jmap_mail_types::VacationResponse>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let args = serde_json::json!({
"accountId": account_id,
"ids": ["singleton"],
});
let req = super::build_request("VacationResponse/get", args, super::USING_VACATION);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn vacation_response_set(
&self,
update: Option<HashMap<Id, PatchObject>>,
) -> Result<SetResponse<jmap_mail_types::VacationResponse>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
});
if let Some(u) = update {
args["update"] = serde_json::to_value(&u).map_err(|e| {
jmap_base_client::ClientError::InvalidArgument(format!(
"vacation_response_set: serializing update map failed: {e}"
))
})?;
}
let req = super::build_request("VacationResponse/set", args, super::USING_VACATION);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
#[test]
fn vacation_response_get_response_deserializes() {
let json = json!({
"accountId": "acc1",
"state": "s1",
"list": [
{
"id": "singleton",
"isEnabled": false
}
],
"notFound": []
});
use super::super::GetResponse;
let resp: GetResponse<jmap_mail_types::VacationResponse> =
serde_json::from_value(json).expect("must deserialize VacationResponse GetResponse");
assert_eq!(resp.list.len(), 1);
assert_eq!(resp.list[0].id.as_ref(), "singleton");
assert!(!resp.list[0].is_enabled);
}
}