-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: /api/v4/botx/smartapps/notification support
- Loading branch information
1 parent
b1ff851
commit 818407c
Showing
5 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
81 changes: 81 additions & 0 deletions
81
pybotx/client/smartapps_api/smartapp_custom_notification.py
This file contains 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,81 @@ | ||
from typing import Any, Dict, Literal, Optional | ||
from uuid import UUID | ||
|
||
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod | ||
from pybotx.missing import Missing | ||
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel | ||
|
||
|
||
class BotXAPISmartAppCustomNotificationNestedPayload(UnverifiedPayloadBaseModel): | ||
title: str | ||
body: str | ||
meta: Missing[Dict[str, Any]] | ||
|
||
|
||
class BotXAPISmartAppCustomNotificationRequestPayload(UnverifiedPayloadBaseModel): | ||
group_chat_id: UUID | ||
payload: BotXAPISmartAppCustomNotificationNestedPayload | ||
|
||
@classmethod | ||
def from_domain( | ||
cls, | ||
group_chat_id: UUID, | ||
title: str, | ||
body: str, | ||
meta: Missing[Dict[str, Any]], | ||
) -> "BotXAPISmartAppCustomNotificationRequestPayload": | ||
return cls( | ||
group_chat_id=group_chat_id, | ||
payload=BotXAPISmartAppCustomNotificationNestedPayload( | ||
title=title, | ||
body=body, | ||
meta=meta, | ||
), | ||
) | ||
|
||
|
||
class BotXAPISyncIdResult(VerifiedPayloadBaseModel): | ||
sync_id: UUID | ||
|
||
|
||
class BotXAPISmartAppCustomNotificationResponsePayload(VerifiedPayloadBaseModel): | ||
status: Literal["ok"] | ||
result: BotXAPISyncIdResult | ||
|
||
def to_domain(self) -> UUID: | ||
return self.result.sync_id | ||
|
||
|
||
class SmartAppCustomNotificationMethod(AuthorizedBotXMethod): | ||
error_callback_handlers = { | ||
**AuthorizedBotXMethod.error_callback_handlers, | ||
} | ||
|
||
async def execute( | ||
self, | ||
payload: BotXAPISmartAppCustomNotificationRequestPayload, | ||
wait_callback: bool, | ||
callback_timeout: Optional[float], | ||
default_callback_timeout: float, | ||
) -> BotXAPISmartAppCustomNotificationResponsePayload: | ||
path = "/api/v4/botx/smartapps/notification" | ||
|
||
response = await self._botx_method_call( | ||
"POST", | ||
self._build_url(path), | ||
json=payload.jsonable_dict(), | ||
) | ||
|
||
api_model = self._verify_and_extract_api_model( | ||
BotXAPISmartAppCustomNotificationResponsePayload, | ||
response, | ||
) | ||
|
||
await self._process_callback( | ||
api_model.result.sync_id, | ||
wait_callback, | ||
callback_timeout, | ||
default_callback_timeout, | ||
) | ||
|
||
return api_model |
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pybotx" | ||
version = "0.59.0" | ||
version = "0.60.0" | ||
description = "A python library for interacting with eXpress BotX API" | ||
authors = [ | ||
"Sidnev Nikolay <[email protected]>", | ||
|
This file contains 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
71 changes: 71 additions & 0 deletions
71
tests/client/smartapps_api/test_smartapp_custom_notification.py
This file contains 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,71 @@ | ||
import asyncio | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
import httpx | ||
import pytest | ||
from respx.router import MockRouter | ||
|
||
from pybotx import Bot, BotAccountWithSecret, HandlerCollector, lifespan_wrapper | ||
|
||
pytestmark = [ | ||
pytest.mark.asyncio, | ||
pytest.mark.mock_authorization, | ||
pytest.mark.usefixtures("respx_mock"), | ||
] | ||
|
||
|
||
async def test__send_smartapp_custom_notification__succeed( | ||
respx_mock: MockRouter, | ||
host: str, | ||
bot_id: UUID, | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
endpoint = respx_mock.post( | ||
f"https://{host}/api/v4/botx/smartapps/notification", | ||
headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, | ||
json={ | ||
"group_chat_id": "705df263-6bfd-536a-9d51-13524afaab5c", | ||
"payload": { | ||
"title": "test", | ||
"body": "test", | ||
"meta": {"message": "ping"}, | ||
}, | ||
}, | ||
).mock( | ||
return_value=httpx.Response( | ||
HTTPStatus.ACCEPTED, | ||
json={ | ||
"status": "ok", | ||
"result": {"sync_id": "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3"}, | ||
}, | ||
), | ||
) | ||
|
||
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
task = asyncio.create_task( | ||
bot.send_smartapp_custom_notification( | ||
bot_id=bot_id, | ||
group_chat_id=UUID("705df263-6bfd-536a-9d51-13524afaab5c"), | ||
title="test", | ||
body="test", | ||
meta={"message": "ping"}, | ||
), | ||
) | ||
await asyncio.sleep(0) # Return control to event loop | ||
|
||
await bot.set_raw_botx_method_result( | ||
{ | ||
"status": "ok", | ||
"sync_id": "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3", | ||
"result": {}, | ||
}, | ||
) | ||
|
||
# - Assert - | ||
assert await task == UUID("21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3") | ||
assert endpoint.called |