#[cfg(test)]
mod tests;
use a2a_lf::{
DeleteTaskPushNotificationConfigRequest, GetTaskPushNotificationConfigRequest,
ListTaskPushNotificationConfigsRequest, ListTaskPushNotificationConfigsResponse,
TaskPushNotificationConfig, methods,
};
use reqwest::Method;
use super::binding::{Operation, checked_id};
use super::{A2aClient, segments};
use crate::a2a::error::A2aError;
const PUSH_CONFIGS: &str = "pushNotificationConfigs";
impl A2aClient {
pub async fn create_push_config(
&self,
mut config: TaskPushNotificationConfig,
) -> Result<TaskPushNotificationConfig, A2aError> {
let method = methods::CREATE_PUSH_CONFIG;
checked_id(method, &config.task_id)?;
let carries_credentials = config.token.is_some() || config.authentication.is_some();
if carries_credentials && self.transport.refuses_credentials_in_clear {
return Err(A2aError::PlaintextPushCredentials);
}
config.tenant = self.transport.tenant.clone();
let task_id = config.task_id.clone();
let operation = Operation::new(
methods::CREATE_PUSH_CONFIG,
&config,
Method::POST,
segments(["tasks", &task_id, PUSH_CONFIGS]),
)?;
let stored = self.transport.call(operation).await?;
expect_config(methods::CREATE_PUSH_CONFIG, &task_id, stored)
}
pub async fn get_push_config(
&self,
task_id: &str,
config_id: &str,
) -> Result<TaskPushNotificationConfig, A2aError> {
checked_id(methods::GET_PUSH_CONFIG, task_id)?;
checked_id(methods::GET_PUSH_CONFIG, config_id)?;
let request = GetTaskPushNotificationConfigRequest {
task_id: task_id.to_string(),
id: config_id.to_string(),
tenant: self.transport.tenant.clone(),
};
let operation = Operation::new(
methods::GET_PUSH_CONFIG,
&request,
Method::GET,
segments(["tasks", task_id, PUSH_CONFIGS, config_id]),
)?;
let config = self.transport.call(operation).await?;
expect_config(methods::GET_PUSH_CONFIG, task_id, config)
}
pub async fn list_push_configs(
&self,
mut request: ListTaskPushNotificationConfigsRequest,
) -> Result<ListTaskPushNotificationConfigsResponse, A2aError> {
checked_id(methods::LIST_PUSH_CONFIGS, &request.task_id)?;
request.tenant = self.transport.tenant.clone();
let operation = Operation::new(
methods::LIST_PUSH_CONFIGS,
&request,
Method::GET,
segments(["tasks", &request.task_id, PUSH_CONFIGS]),
)?
.query("pageSize", request.page_size)
.query("pageToken", request.page_token.as_ref());
let listed: ListTaskPushNotificationConfigsResponse =
self.transport.call(operation).await?;
if listed
.configs
.iter()
.any(|config| !names_task(config, &request.task_id))
{
return Err(A2aError::MismatchedTask {
method: methods::LIST_PUSH_CONFIGS,
});
}
Ok(listed)
}
pub async fn delete_push_config(&self, task_id: &str, config_id: &str) -> Result<(), A2aError> {
checked_id(methods::DELETE_PUSH_CONFIG, task_id)?;
checked_id(methods::DELETE_PUSH_CONFIG, config_id)?;
let request = DeleteTaskPushNotificationConfigRequest {
task_id: task_id.to_string(),
id: config_id.to_string(),
tenant: self.transport.tenant.clone(),
};
let operation = Operation::new(
methods::DELETE_PUSH_CONFIG,
&request,
Method::DELETE,
segments(["tasks", task_id, PUSH_CONFIGS, config_id]),
)?;
self.transport.call_value(operation).await.map(drop)
}
}
fn names_task(config: &TaskPushNotificationConfig, task_id: &str) -> bool {
config.task_id.is_empty() || config.task_id == task_id
}
fn expect_config(
method: &'static str,
task_id: &str,
config: TaskPushNotificationConfig,
) -> Result<TaskPushNotificationConfig, A2aError> {
if !names_task(&config, task_id) {
return Err(A2aError::MismatchedTask { method });
}
Ok(config)
}