-
Notifications
You must be signed in to change notification settings - Fork 303
feat: Add push notification support #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
08bb1a1
Add push notification support
swapydapy fa6e074
Merge branch 'google:main' into push-notifs
swapydapy 957162d
Merge branch 'google:main' into push-notifs
swapydapy e5adaa1
adress review comments: rename notif & reverted elifs
swapydapy 7af195a
Merge remote-tracking branch 'upstream' into push-notifs
swapydapy 4706c68
use json mode & internal error
swapydapy 9e2b3b2
add push for blocking
swapydapy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| import httpx | ||
|
|
||
| from a2a.server.tasks.push_notifier import PushNotifier | ||
| from a2a.types import PushNotificationConfig, Task | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class InMemoryPushNotifier(PushNotifier): | ||
| """In-memory implementation of PushNotifier interface.""" | ||
|
|
||
| def __init__(self, httpx_client: httpx.AsyncClient) -> None: | ||
| self._client = httpx_client | ||
| self.lock = asyncio.Lock() | ||
| self._push_notification_infos: dict[str, PushNotificationConfig] = {} | ||
|
|
||
| async def set_info( | ||
| self, task_id: str, notification_config: PushNotificationConfig | ||
| ): | ||
| async with self.lock: | ||
| self._push_notification_infos[task_id] = notification_config | ||
|
|
||
| async def get_info(self, task_id: str) -> PushNotificationConfig | None: | ||
| async with self.lock: | ||
| return self._push_notification_infos.get(task_id) | ||
|
|
||
| async def delete_info(self, task_id: str): | ||
| async with self.lock: | ||
| if task_id in self._push_notification_infos: | ||
| del self._push_notification_infos[task_id] | ||
|
|
||
| async def send_notification(self, task: Task): | ||
| push_info = await self.get_info(task.id) | ||
| if not push_info: | ||
| return | ||
| url = push_info.url | ||
|
|
||
| try: | ||
| response = await self._client.post( | ||
| url, json=task.model_dump(mode='json', exclude_none=True) | ||
| ) | ||
| response.raise_for_status() | ||
| logger.info(f'Push-notification sent for URL: {url}') | ||
| except Exception as e: | ||
| logger.error(f'Error sending push-notification: {e}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from a2a.types import PushNotificationConfig, Task | ||
|
|
||
|
|
||
| class PushNotifier(ABC): | ||
| """PushNotifier interface to store, retrieve push notification for tasks and send push notifications.""" | ||
|
|
||
| @abstractmethod | ||
| async def set_info( | ||
| self, task_id: str, notification_config: PushNotificationConfig | ||
| ): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| async def get_info(self, task_id: str) -> PushNotificationConfig | None: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| async def delete_info(self, task_id: str): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| async def send_notification(self, task: Task): | ||
| pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.