Skip to content

Commit bf2c157

Browse files
authored
Change the specification of synchronous requests (#476)
1 parent c6d3d17 commit bf2c157

File tree

15 files changed

+307
-212
lines changed

15 files changed

+307
-212
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,11 @@ collector = HandlerCollector()
234234
@collector.sync_smartapp_event
235235
async def handle_sync_smartapp_event(
236236
event: SmartAppEvent, bot: Bot,
237-
) -> SyncSmartAppEventResponsePayload:
237+
) -> BotAPISyncSmartAppEventResultResponse:
238238
print(f"Got sync smartapp event: {event}")
239-
return SyncSmartAppEventResponsePayload.from_domain(
240-
ref=event.ref,
241-
smartapp_id=event.bot.id,
242-
chat_id=event.chat.id,
239+
return BotAPISyncSmartAppEventResultResponse.from_domain(
243240
data={},
244-
opts={},
245241
files=[],
246-
encrypted=True,
247242
)
248243
```
249244

pybotx/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@
6464
SmartappManifest,
6565
SmartappManifestWebParams,
6666
)
67-
from pybotx.client.smartapps_api.sync_smartapp_event import (
68-
SyncSmartAppEventResponsePayload,
69-
)
7067
from pybotx.client.stickers_api.exceptions import (
7168
InvalidEmojiError,
7269
InvalidImageError,
@@ -126,6 +123,11 @@
126123
from pybotx.models.smartapps import SmartApp
127124
from pybotx.models.status import BotMenu, StatusRecipient
128125
from pybotx.models.stickers import Sticker, StickerPack
126+
from pybotx.models.sync_smartapp_event import (
127+
BotAPISyncSmartAppEventErrorResponse,
128+
BotAPISyncSmartAppEventResponse,
129+
BotAPISyncSmartAppEventResultResponse,
130+
)
129131
from pybotx.models.system_events.added_to_chat import AddedToChatEvent
130132
from pybotx.models.system_events.chat_created import ChatCreatedEvent, ChatCreatedMember
131133
from pybotx.models.system_events.chat_deleted_by_user import ChatDeletedByUserEvent
@@ -152,6 +154,9 @@
152154
"BotAPIBotDisabledErrorData",
153155
"BotAPIBotDisabledResponse",
154156
"BotAPIMethodFailedCallback",
157+
"BotAPISyncSmartAppEventErrorResponse",
158+
"BotAPISyncSmartAppEventResponse",
159+
"BotAPISyncSmartAppEventResultResponse",
155160
"BotAPIUnverifiedRequestErrorData",
156161
"BotAPIUnverifiedRequestResponse",
157162
"BotAccount",
@@ -228,7 +233,6 @@
228233
"RequestHeadersNotProvidedError",
229234
"SmartApp",
230235
"SmartAppEvent",
231-
"SyncSmartAppEventResponsePayload",
232236
"SmartappManifest",
233237
"SmartappManifestWebLayoutChoices",
234238
"SmartappManifestWebParams",

pybotx/bot/bot.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@
148148
BotXAPISmartAppsListRequestPayload,
149149
SmartAppsListMethod,
150150
)
151-
from pybotx.client.smartapps_api.sync_smartapp_event import (
152-
SyncSmartAppEventResponsePayload,
153-
)
154151
from pybotx.client.smartapps_api.upload_file import (
155152
UploadFileMethod as SmartappsUploadFileMethod,
156153
)
@@ -221,7 +218,7 @@
221218
ensure_file_content_is_png,
222219
ensure_sticker_image_size_valid,
223220
)
224-
from pybotx.logger import logger, pformat_jsonable_obj, trim_file_data_in_incoming_json
221+
from pybotx.logger import log_incoming_request, logger, pformat_jsonable_obj
225222
from pybotx.missing import Missing, MissingOptional, Undefined
226223
from pybotx.models.async_files import File
227224
from pybotx.models.attachments import IncomingFileAttachment, OutgoingAttachment
@@ -245,10 +242,11 @@
245242
build_bot_status_response,
246243
)
247244
from pybotx.models.stickers import Sticker, StickerPack, StickerPackFromList
248-
from pybotx.models.system_events.smartapp_event import (
249-
BotAPISmartAppEvent,
250-
SmartAppEvent,
245+
from pybotx.models.sync_smartapp_event import (
246+
BotAPISyncSmartAppEvent,
247+
BotAPISyncSmartAppEventResponse,
251248
)
249+
from pybotx.models.system_events.smartapp_event import SmartAppEvent
252250
from pybotx.models.users import UserFromCSV, UserFromSearch
253251

254252
MissingOptionalAttachment = MissingOptional[
@@ -299,16 +297,9 @@ def async_execute_raw_bot_command(
299297
logging_command: bool = True,
300298
) -> None:
301299
if logging_command:
302-
logger.opt(lazy=True).debug(
303-
"Got command: {command}",
304-
command=lambda: pformat_jsonable_obj(
305-
trim_file_data_in_incoming_json(raw_bot_command),
306-
),
307-
)
300+
log_incoming_request(raw_bot_command, message="Got command: ")
308301

309302
if verify_request:
310-
if request_headers is None:
311-
raise RequestHeadersNotProvidedError
312303
self._verify_request(request_headers)
313304

314305
try:
@@ -338,23 +329,19 @@ async def sync_execute_raw_smartapp_event(
338329
verify_request: bool = True,
339330
request_headers: Optional[Mapping[str, str]] = None,
340331
logging_command: bool = True,
341-
) -> SyncSmartAppEventResponsePayload:
332+
) -> BotAPISyncSmartAppEventResponse:
342333
if logging_command:
343-
logger.opt(lazy=True).debug(
344-
"Got sync smartapp event: {command}",
345-
command=lambda: pformat_jsonable_obj(
346-
trim_file_data_in_incoming_json(raw_smartapp_event),
347-
),
334+
log_incoming_request(
335+
raw_smartapp_event,
336+
message="Got sync smartapp event: ",
348337
)
349338

350339
if verify_request:
351-
if request_headers is None:
352-
raise RequestHeadersNotProvidedError
353340
self._verify_request(request_headers)
354341

355342
try:
356-
bot_api_smartapp_event: BotAPISmartAppEvent = parse_obj_as(
357-
BotAPISmartAppEvent,
343+
bot_api_smartapp_event: BotAPISyncSmartAppEvent = parse_obj_as(
344+
BotAPISyncSmartAppEvent,
358345
raw_smartapp_event,
359346
)
360347
except ValidationError as validation_exc:
@@ -368,7 +355,7 @@ async def sync_execute_raw_smartapp_event(
368355
async def sync_execute_smartapp_event(
369356
self,
370357
smartapp_event: SmartAppEvent,
371-
) -> SyncSmartAppEventResponsePayload:
358+
) -> BotAPISyncSmartAppEventResponse:
372359
self._bot_accounts_storage.ensure_bot_id_exists(smartapp_event.bot.id)
373360
return await self._handler_collector.handle_sync_smartapp_event(
374361
self,
@@ -2028,7 +2015,13 @@ async def collect_metric(
20282015
)
20292016
await method.execute(payload)
20302017

2031-
def _verify_request(self, headers: Mapping[str, str]) -> None: # noqa: WPS238
2018+
def _verify_request( # noqa: WPS231, WPS238
2019+
self,
2020+
headers: Optional[Mapping[str, str]],
2021+
) -> None:
2022+
if headers is None:
2023+
raise RequestHeadersNotProvidedError
2024+
20322025
authorization_header = headers.get("authorization")
20332026
if not authorization_header:
20342027
raise UnverifiedRequestError("The authorization token was not provided.")

pybotx/bot/handler.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
from functools import partial
33
from typing import TYPE_CHECKING, Awaitable, Callable, List, Literal, TypeVar, Union
44

5-
from pybotx.client.smartapps_api.sync_smartapp_event import (
6-
SyncSmartAppEventResponsePayload,
7-
)
85
from pybotx.models.commands import BotCommand
96
from pybotx.models.message.incoming_message import IncomingMessage
107
from pybotx.models.status import StatusRecipient
8+
from pybotx.models.sync_smartapp_event import BotAPISyncSmartAppEventResponse
119
from pybotx.models.system_events.added_to_chat import AddedToChatEvent
1210
from pybotx.models.system_events.chat_created import ChatCreatedEvent
1311
from pybotx.models.system_events.chat_deleted_by_user import ChatDeletedByUserEvent
@@ -29,7 +27,7 @@
2927

3028
SyncSmartAppEventHandlerFunc = Callable[
3129
[SmartAppEvent, "Bot"],
32-
Awaitable[SyncSmartAppEventResponsePayload],
30+
Awaitable[BotAPISyncSmartAppEventResponse],
3331
]
3432

3533
IncomingMessageHandlerFunc = HandlerFunc[IncomingMessage]

pybotx/bot/handler_collector.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@
3333
ExceptionMiddleware,
3434
)
3535
from pybotx.client.smartapps_api.exceptions import SyncSmartAppEventHandlerNotFoundError
36-
from pybotx.client.smartapps_api.sync_smartapp_event import (
37-
SyncSmartAppEventResponsePayload,
38-
)
3936
from pybotx.converters import optional_sequence_to_list
4037
from pybotx.logger import logger
4138
from pybotx.models.commands import BotCommand, SystemEvent
4239
from pybotx.models.message.incoming_message import IncomingMessage
4340
from pybotx.models.status import BotMenu, StatusRecipient
41+
from pybotx.models.sync_smartapp_event import BotAPISyncSmartAppEventResponse
4442
from pybotx.models.system_events.added_to_chat import AddedToChatEvent
4543
from pybotx.models.system_events.chat_created import ChatCreatedEvent
4644
from pybotx.models.system_events.chat_deleted_by_user import ChatDeletedByUserEvent
@@ -126,7 +124,7 @@ async def handle_sync_smartapp_event(
126124
self,
127125
bot: "Bot",
128126
smartapp_event: SmartAppEvent,
129-
) -> SyncSmartAppEventResponsePayload:
127+
) -> BotAPISyncSmartAppEventResponse:
130128
if not isinstance(smartapp_event, SmartAppEvent):
131129
raise NotImplementedError(
132130
f"Unsupported event type for sync smartapp event: `{smartapp_event}`",

pybotx/client/smartapps_api/smartapp_event.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Literal, Type, TypeVar
1+
from typing import Any, Dict, List, Literal
22
from uuid import UUID
33

44
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
@@ -7,11 +7,6 @@
77
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel
88
from pybotx.models.async_files import APIAsyncFile, File, convert_async_file_from_domain
99

10-
TBotXAPISmartAppEventRequestPayload = TypeVar(
11-
"TBotXAPISmartAppEventRequestPayload",
12-
bound="BotXAPISmartAppEventRequestPayload",
13-
)
14-
1510

1611
class BotXAPISmartAppEventRequestPayload(UnverifiedPayloadBaseModel):
1712
ref: MissingOptional[UUID]
@@ -25,15 +20,15 @@ class BotXAPISmartAppEventRequestPayload(UnverifiedPayloadBaseModel):
2520

2621
@classmethod
2722
def from_domain(
28-
cls: Type[TBotXAPISmartAppEventRequestPayload],
23+
cls,
2924
ref: MissingOptional[UUID],
3025
smartapp_id: UUID,
3126
chat_id: UUID,
3227
data: Dict[str, Any],
3328
opts: Missing[Dict[str, Any]],
3429
files: Missing[List[File]],
3530
encrypted: bool,
36-
) -> TBotXAPISmartAppEventRequestPayload:
31+
) -> "BotXAPISmartAppEventRequestPayload":
3732
api_async_files: Missing[List[APIAsyncFile]] = Undefined
3833
if files:
3934
api_async_files = [convert_async_file_from_domain(file) for file in files]

pybotx/client/smartapps_api/sync_smartapp_event.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

pybotx/logger.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def trim_file_data_in_incoming_json(json_body: Dict[str, Any]) -> Dict[str, Any]
4141
return json_body
4242

4343

44+
def log_incoming_request(request: Dict[str, Any], *, message: str = "") -> None:
45+
logger.opt(lazy=True).debug(
46+
message + "{command}",
47+
command=lambda: pformat_jsonable_obj(
48+
trim_file_data_in_incoming_json(request),
49+
),
50+
)
51+
52+
4453
def setup_logger() -> "Logger":
4554
return _logger
4655

pybotx/models/bot_account.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass
22
from functools import cached_property
3+
from typing import Optional
34
from urllib.parse import urlparse
45
from uuid import UUID
56

@@ -9,7 +10,7 @@
910
@dataclass
1011
class BotAccount:
1112
id: UUID
12-
host: str
13+
host: Optional[str]
1314

1415

1516
class BotAccountWithSecret(BaseModel):

0 commit comments

Comments
 (0)