use std::collections::HashMap;
use jmap_types::{Id, PatchObject, State};
use super::{ChangesResponse, GetResponse, QueryChangesResponse, QueryResponse, SetResponse};
impl super::SessionClient {
pub async fn task_get(
&self,
ids: Option<&[Id]>,
properties: Option<&[&str]>,
) -> Result<GetResponse<jmap_tasks_types::Task>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({ "accountId": account_id });
if let Some(id_slice) = ids {
args["ids"] = serde_json::to_value(id_slice).expect("Id slice Serialize is infallible");
}
if let Some(props) = properties {
args["properties"] =
serde_json::to_value(props).expect("&[&str] Serialize is infallible");
}
let req = super::build_request("Task/get", args, super::USING_TASKS);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn task_changes(
&self,
since_state: &State,
max_changes: Option<u64>,
) -> Result<ChangesResponse, jmap_base_client::ClientError> {
if since_state.as_ref().is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"task_changes: since_state may not be empty".into(),
));
}
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
"sinceState": since_state,
});
if let Some(mc) = max_changes {
args["maxChanges"] = mc.into();
}
let req = super::build_request("Task/changes", args, super::USING_TASKS);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn task_set(
&self,
create: Option<serde_json::Value>,
update: Option<HashMap<Id, PatchObject>>,
destroy: Option<Vec<Id>>,
) -> Result<SetResponse<jmap_tasks_types::Task>, jmap_base_client::ClientError> {
if create.is_none() && update.is_none() && destroy.is_none() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"task_set: at least one of create, update, destroy must be Some \
(an all-None /set is a no-op round-trip)"
.into(),
));
}
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
});
if let Some(c) = create {
args["create"] = c;
}
if let Some(u) = update {
args["update"] = serde_json::to_value(&u).map_err(|e| {
jmap_base_client::ClientError::InvalidArgument(format!(
"task_set: serializing update map failed: {e}"
))
})?;
}
if let Some(d) = destroy {
args["destroy"] = serde_json::to_value(&d).expect("Id Vec Serialize is infallible");
}
let req = super::build_request("Task/set", args, super::USING_TASKS);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn task_copy(
&self,
from_account_id: &Id,
create: serde_json::Value,
) -> Result<SetResponse<jmap_tasks_types::Task>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let args = serde_json::json!({
"fromAccountId": from_account_id,
"accountId": account_id,
"create": create,
});
let req = super::build_request("Task/copy", args, super::USING_TASKS);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn task_query(
&self,
filter: Option<serde_json::Value>,
sort: Option<serde_json::Value>,
position: Option<u64>,
limit: Option<u64>,
) -> Result<QueryResponse, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
});
if let Some(f) = filter {
args["filter"] = f;
}
if let Some(s) = sort {
args["sort"] = s;
}
if let Some(p) = position {
args["position"] = p.into();
}
if let Some(l) = limit {
args["limit"] = l.into();
}
let req = super::build_request("Task/query", args, super::USING_TASKS);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn task_query_changes(
&self,
since_query_state: &State,
max_changes: Option<u64>,
filter: Option<serde_json::Value>,
sort: Option<serde_json::Value>,
up_to_id: Option<&Id>,
calculate_total: Option<bool>,
) -> Result<QueryChangesResponse, jmap_base_client::ClientError> {
if since_query_state.as_ref().is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"task_query_changes: since_query_state may not be empty".into(),
));
}
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
"sinceQueryState": since_query_state,
});
if let Some(f) = filter {
args["filter"] = f;
}
if let Some(s) = sort {
args["sort"] = s;
}
if let Some(mc) = max_changes {
args["maxChanges"] = mc.into();
}
if let Some(uti) = up_to_id {
args["upToId"] = serde_json::to_value(uti).expect("Id Serialize is infallible");
}
if let Some(ct) = calculate_total {
args["calculateTotal"] = ct.into();
}
let req = super::build_request("Task/queryChanges", args, super::USING_TASKS);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
}