Skip to content

Commit

Permalink
feat: change the specification of synchronous requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Maximenyuk committed Jun 21, 2024
1 parent 646825c commit 92b64d4
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 228 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,12 @@ collector = HandlerCollector()
@collector.sync_smartapp_event
async def handle_sync_smartapp_event(
event: SmartAppEvent, bot: Bot,
) -> SyncSmartAppEventResponsePayload:
) -> BotAPISyncSmartAppEventResultResponse:
print(f"Got sync smartapp event: {event}")
return SyncSmartAppEventResponsePayload.from_domain(
return BotAPISyncSmartAppEventResultResponse.from_domain(
ref=event.ref,
smartapp_id=event.bot.id,
chat_id=event.chat.id,
data={},
opts={},
files=[],
encrypted=True,
)
```

Expand Down
81 changes: 44 additions & 37 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions pybotx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@
SmartappManifest,
SmartappManifestWebParams,
)
from pybotx.client.smartapps_api.sync_smartapp_event import (
SyncSmartAppEventResponsePayload,
)
from pybotx.client.stickers_api.exceptions import (
InvalidEmojiError,
InvalidImageError,
Expand Down Expand Up @@ -126,6 +123,11 @@
from pybotx.models.smartapps import SmartApp
from pybotx.models.status import BotMenu, StatusRecipient
from pybotx.models.stickers import Sticker, StickerPack
from pybotx.models.sync_smartapp_event import (
BotAPISyncSmartAppEventErrorResponse,
BotAPISyncSmartAppEventResponse,
BotAPISyncSmartAppEventResultResponse,
)
from pybotx.models.system_events.added_to_chat import AddedToChatEvent
from pybotx.models.system_events.chat_created import ChatCreatedEvent, ChatCreatedMember
from pybotx.models.system_events.cts_login import CTSLoginEvent
Expand All @@ -151,6 +153,9 @@
"BotAPIBotDisabledErrorData",
"BotAPIBotDisabledResponse",
"BotAPIMethodFailedCallback",
"BotAPISyncSmartAppEventErrorResponse",
"BotAPISyncSmartAppEventResponse",
"BotAPISyncSmartAppEventResultResponse",
"BotAPIUnverifiedRequestErrorData",
"BotAPIUnverifiedRequestResponse",
"BotAccount",
Expand Down Expand Up @@ -226,7 +231,6 @@
"RequestHeadersNotProvidedError",
"SmartApp",
"SmartAppEvent",
"SyncSmartAppEventResponsePayload",
"SmartappManifest",
"SmartappManifestWebLayoutChoices",
"SmartappManifestWebParams",
Expand Down
18 changes: 8 additions & 10 deletions pybotx/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@
BotXAPISmartAppsListRequestPayload,
SmartAppsListMethod,
)
from pybotx.client.smartapps_api.sync_smartapp_event import (
SyncSmartAppEventResponsePayload,
)
from pybotx.client.smartapps_api.upload_file import (
UploadFileMethod as SmartappsUploadFileMethod,
)
Expand Down Expand Up @@ -245,10 +242,11 @@
build_bot_status_response,
)
from pybotx.models.stickers import Sticker, StickerPack, StickerPackFromList
from pybotx.models.system_events.smartapp_event import (
BotAPISmartAppEvent,
SmartAppEvent,
from pybotx.models.sync_smartapp_event import (
BotAPISyncSmartAppEvent,
BotAPISyncSmartAppEventResponse,
)
from pybotx.models.system_events.smartapp_event import SmartAppEvent
from pybotx.models.users import UserFromCSV, UserFromSearch

MissingOptionalAttachment = MissingOptional[
Expand Down Expand Up @@ -331,7 +329,7 @@ async def sync_execute_raw_smartapp_event(
verify_request: bool = True,
request_headers: Optional[Mapping[str, str]] = None,
logging_command: bool = True,
) -> SyncSmartAppEventResponsePayload:
) -> BotAPISyncSmartAppEventResponse:
if logging_command:
log_incoming_request(
raw_smartapp_event,
Expand All @@ -342,8 +340,8 @@ async def sync_execute_raw_smartapp_event(
self._verify_request(request_headers)

try:
bot_api_smartapp_event: BotAPISmartAppEvent = parse_obj_as(
BotAPISmartAppEvent,
bot_api_smartapp_event: BotAPISyncSmartAppEvent = parse_obj_as(
BotAPISyncSmartAppEvent,
raw_smartapp_event,
)
except ValidationError as validation_exc:
Expand All @@ -357,7 +355,7 @@ async def sync_execute_raw_smartapp_event(
async def sync_execute_smartapp_event(
self,
smartapp_event: SmartAppEvent,
) -> SyncSmartAppEventResponsePayload:
) -> BotAPISyncSmartAppEventResponse:
self._bot_accounts_storage.ensure_bot_id_exists(smartapp_event.bot.id)
return await self._handler_collector.handle_sync_smartapp_event(
self,
Expand Down
6 changes: 2 additions & 4 deletions pybotx/bot/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
from functools import partial
from typing import TYPE_CHECKING, Awaitable, Callable, List, Literal, TypeVar, Union

from pybotx.client.smartapps_api.sync_smartapp_event import (
SyncSmartAppEventResponsePayload,
)
from pybotx.models.commands import BotCommand
from pybotx.models.message.incoming_message import IncomingMessage
from pybotx.models.status import StatusRecipient
from pybotx.models.sync_smartapp_event import BotAPISyncSmartAppEventResponse
from pybotx.models.system_events.added_to_chat import AddedToChatEvent
from pybotx.models.system_events.chat_created import ChatCreatedEvent
from pybotx.models.system_events.cts_login import CTSLoginEvent
Expand All @@ -28,7 +26,7 @@

SyncSmartAppEventHandlerFunc = Callable[
[SmartAppEvent, "Bot"],
Awaitable[SyncSmartAppEventResponsePayload],
Awaitable[BotAPISyncSmartAppEventResponse],
]

IncomingMessageHandlerFunc = HandlerFunc[IncomingMessage]
Expand Down
6 changes: 2 additions & 4 deletions pybotx/bot/handler_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
ExceptionMiddleware,
)
from pybotx.client.smartapps_api.exceptions import SyncSmartAppEventHandlerNotFoundError
from pybotx.client.smartapps_api.sync_smartapp_event import (
SyncSmartAppEventResponsePayload,
)
from pybotx.converters import optional_sequence_to_list
from pybotx.logger import logger
from pybotx.models.commands import BotCommand, SystemEvent
from pybotx.models.message.incoming_message import IncomingMessage
from pybotx.models.status import BotMenu, StatusRecipient
from pybotx.models.sync_smartapp_event import BotAPISyncSmartAppEventResponse
from pybotx.models.system_events.added_to_chat import AddedToChatEvent
from pybotx.models.system_events.chat_created import ChatCreatedEvent
from pybotx.models.system_events.cts_login import CTSLoginEvent
Expand Down Expand Up @@ -126,7 +124,7 @@ async def handle_sync_smartapp_event(
self,
bot: "Bot",
smartapp_event: SmartAppEvent,
) -> SyncSmartAppEventResponsePayload:
) -> BotAPISyncSmartAppEventResponse:
if not isinstance(smartapp_event, SmartAppEvent):
raise NotImplementedError(
f"Unsupported event type for sync smartapp event: `{smartapp_event}`",
Expand Down
Loading

0 comments on commit 92b64d4

Please sign in to comment.