-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathbase_push_notification_sender.py
More file actions
72 lines (60 loc) · 2.29 KB
/
base_push_notification_sender.py
File metadata and controls
72 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import asyncio
import logging
import httpx
from a2a.server.tasks.push_notification_config_store import (
PushNotificationConfigStore,
)
from a2a.server.tasks.push_notification_sender import PushNotificationSender
from a2a.types import PushNotificationConfig, Task
logger = logging.getLogger(__name__)
class BasePushNotificationSender(PushNotificationSender):
"""Base implementation of PushNotificationSender interface."""
def __init__(
self,
httpx_client: httpx.AsyncClient,
config_store: PushNotificationConfigStore,
) -> None:
"""Initializes the BasePushNotificationSender.
Args:
httpx_client: An async HTTP client instance to send notifications.
config_store: A PushNotificationConfigStore instance to retrieve configurations.
"""
self._client = httpx_client
self._config_store = config_store
async def send_notification(self, task: Task) -> None:
"""Sends a push notification for a task if configuration exists."""
push_configs = await self._config_store.get_info(task.id)
if not push_configs:
return
awaitables = [
self._dispatch_notification(task, push_info)
for push_info in push_configs
]
results = await asyncio.gather(*awaitables)
if not all(results):
logger.warning(
f'Some push notifications failed to send for task_id={task.id}'
)
async def _dispatch_notification(
self, task: Task, push_info: PushNotificationConfig
) -> bool:
url = push_info.url
try:
headers = None
if push_info.token:
headers = {'X-A2A-Notification-Token': push_info.token}
response = await self._client.post(
url,
json=task.model_dump(mode='json', exclude_none=True),
headers=headers,
)
response.raise_for_status()
logger.info(
f'Push-notification sent for task_id={task.id} to URL: {url}'
)
return True
except Exception as e:
logger.error(
f'Error sending push-notification for task_id={task.id} to URL: {url}. Error: {e}'
)
return False