a2a_protocol_server/handler/
renamed.rs1use std::collections::HashMap;
11
12use a2a_protocol_types::params::TaskIdParams;
13use a2a_protocol_types::push::TaskPushNotificationConfig;
14
15use super::RequestHandler;
16use crate::error::ServerResult;
17use crate::streaming::InMemoryQueueReader;
18
19impl RequestHandler {
20 #[deprecated(
27 since = "0.16.0",
28 note = "use `on_subscribe_to_task`, named for the `SubscribeToTask` wire method; \
29 removed no earlier than 0.17.0"
30 )]
31 pub async fn on_resubscribe(
32 &self,
33 params: TaskIdParams,
34 headers: Option<&HashMap<String, String>>,
35 ) -> ServerResult<InMemoryQueueReader> {
36 Box::pin(self.on_subscribe_to_task(params, headers)).await
37 }
38
39 #[deprecated(
46 since = "0.16.0",
47 note = "use `on_create_push_config`, named for the \
48 `CreateTaskPushNotificationConfig` wire method; removed no earlier than 0.17.0"
49 )]
50 pub async fn on_set_push_config(
51 &self,
52 config: TaskPushNotificationConfig,
53 headers: Option<&HashMap<String, String>>,
54 ) -> ServerResult<TaskPushNotificationConfig> {
55 Box::pin(self.on_create_push_config(config, headers)).await
56 }
57}
58
59#[cfg(test)]
62#[allow(deprecated)]
63mod tests {
64 use a2a_protocol_types::params::{GetPushConfigParams, TaskIdParams};
65 use a2a_protocol_types::push::TaskPushNotificationConfig;
66 use a2a_protocol_types::task::{ContextId, Task, TaskId, TaskState, TaskStatus};
67
68 use crate::agent_executor;
69 use crate::builder::RequestHandlerBuilder;
70 use crate::error::ServerError;
71 use crate::handler::RequestHandler;
72 use crate::streaming::event_queue::EventQueueReader as _;
73
74 struct Idle;
75 agent_executor!(Idle, |_ctx, _queue| async { Ok(()) });
76
77 struct Accepting;
79 impl crate::push::PushSender for Accepting {
80 fn send<'a>(
81 &'a self,
82 _url: &'a str,
83 _event: &'a a2a_protocol_types::events::StreamResponse,
84 _config: &'a TaskPushNotificationConfig,
85 ) -> std::pin::Pin<
86 Box<
87 dyn std::future::Future<Output = a2a_protocol_types::error::A2aResult<()>>
88 + Send
89 + 'a,
90 >,
91 > {
92 Box::pin(async { Ok(()) })
93 }
94 fn allows_private_urls(&self) -> bool {
95 true
96 }
97 }
98
99 fn handler() -> RequestHandler {
100 RequestHandlerBuilder::new(Idle)
101 .without_agent_card()
102 .with_push_sender(Accepting)
103 .build()
104 .unwrap()
105 }
106
107 async fn save(handler: &RequestHandler, id: &str, state: TaskState) {
108 let task = Task::new(
109 TaskId::new(id),
110 ContextId::new("ctx"),
111 TaskStatus::new(state),
112 );
113 handler.task_store.save(&task).await.unwrap();
114 }
115
116 #[tokio::test]
119 async fn on_resubscribe_is_on_subscribe_to_task() {
120 let handler = handler();
121 save(&handler, "t-open", TaskState::Working).await;
122 let mut reader = handler
123 .on_resubscribe(TaskIdParams::new("t-open"), None)
124 .await
125 .expect("subscribed");
126 let first = reader.read().await.expect("a first event").expect("ok");
127 assert!(
128 matches!(&first.event, a2a_protocol_types::events::StreamResponse::Task(t)
129 if t.id == TaskId::new("t-open") && t.status.state == TaskState::Working),
130 "{first:?}"
131 );
132
133 let missing = handler
134 .on_resubscribe(TaskIdParams::new("t-missing"), None)
135 .await;
136 assert!(
137 matches!(missing, Err(ServerError::TaskNotFound(_))),
138 "{missing:?}"
139 );
140 }
141
142 #[tokio::test]
145 async fn on_set_push_config_is_on_create_push_config() {
146 let handler = handler();
147 save(&handler, "t1", TaskState::Working).await;
148 let config =
149 TaskPushNotificationConfig::new("t1", "https://example.com/hook").with_id("cfg-old");
150 let stored = handler
151 .on_set_push_config(config, None)
152 .await
153 .expect("stored");
154 assert_eq!(stored.id.as_deref(), Some("cfg-old"));
155 assert_eq!(stored.url, "https://example.com/hook");
156 let read = handler
157 .on_get_push_config(GetPushConfigParams::new("t1", "cfg-old"), None)
158 .await
159 .expect("found");
160 assert_eq!(read.url, stored.url);
161
162 let orphan = handler
163 .on_set_push_config(
164 TaskPushNotificationConfig::new("t-missing", "https://example.com/hook"),
165 None,
166 )
167 .await;
168 assert!(
169 matches!(orphan, Err(ServerError::TaskNotFound(_))),
170 "{orphan:?}"
171 );
172 }
173}