Skip to content

Commit

Permalink
Reverse sync request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Kudinov committed Jun 4, 2024
1 parent b4c0459 commit b02dceb
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 185 deletions.
27 changes: 0 additions & 27 deletions app/api/endpoints/botx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@
from http import HTTPStatus

from fastapi import APIRouter, Request
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pybotx import (
Bot,
UnknownBotAccountError,
build_bot_disabled_response,
build_command_accepted_response,
)
from pybotx_smartapp_rpc import SmartApp
from pybotx_smartapp_rpc.models.request import RPCRequest

from app.api.dependencies.bot import bot_dependency
from app.schemas.sync_request import (
SyncRequest,
transfrorm_sync_request_to_smartapp_event,
)
from app.logger import logger

router = APIRouter()
Expand Down Expand Up @@ -66,23 +59,3 @@ async def callback_handler(request: Request, bot: Bot = bot_dependency) -> JSONR
build_command_accepted_response(),
status_code=HTTPStatus.ACCEPTED,
)


@router.post("/smartapps/request")
async def sync_request_handler(
request: SyncRequest, bot: Bot = bot_dependency
) -> JSONResponse:
smartapp_event = transfrorm_sync_request_to_smartapp_event(request, bot)
smartapp = SmartApp(
bot, smartapp_event.bot.id, smartapp_event.chat.id, smartapp_event
)
rpc_request = RPCRequest(**smartapp_event.data)

rpc_response = (
await bot.state.smartapp_rpc._router.perform_rpc_request( # noqa: WPS437
smartapp, rpc_request
)
)

json = jsonable_encoder(rpc_response.jsonable_dict())
return JSONResponse(json)
8 changes: 0 additions & 8 deletions app/bot/commands/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,10 @@
)

from app.resources import strings
from app.services.answers import build_incoming_command_text

collector = HandlerCollector()


@collector.default_message_handler
async def default_handler(message: IncomingMessage, bot: Bot) -> None:
"""Show bot command data."""
if isinstance(message.data, dict) and message.data.get("command") is not None:
await bot.answer_message(build_incoming_command_text(message))


@collector.smartapp_event
async def handle_smartapp_event(event: SmartAppEvent, bot: Bot) -> None:
await bot.state.smartapp_rpc.handle_smartapp_event(event, bot)
Expand Down
8 changes: 4 additions & 4 deletions app/bot/middlewares/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from pybotx_smartapp_rpc import RPCArgsBaseModel, SmartApp
from pybotx_smartapp_rpc.typing import Handler, RPCResponse

from app.services.answers import build_incoming_smartapp_event_text


async def debug_middleware(
smartapp: SmartApp, rpc_arguments: RPCArgsBaseModel, call_next: Handler
) -> RPCResponse:
"""TODO: uncomment code after pybotx implements sync requests.
await smartapp.bot.answer_message(
build_incoming_smartapp_event_text(smartapp.event.raw_command)
build_incoming_smartapp_event_text(smartapp.event.raw_command)
)
"""

return await call_next(smartapp, rpc_arguments)
5 changes: 0 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,3 @@ def get_application() -> FastAPI:


app = get_application()


if __name__ == "__main__":
import uvicorn
uvicorn.run("app.main:app", host='0.0.0.0', port=4004, reload=True, debug=True, workers=3)
132 changes: 0 additions & 132 deletions app/schemas/sync_request.py

This file was deleted.

10 changes: 1 addition & 9 deletions app/services/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Any, Dict

from aiofiles.tempfile import SpooledTemporaryFile
from pybotx import File, Image, IncomingMessage, UserFromSearch
from pybotx import File, Image, UserFromSearch
from pybotx_smartapp_rpc import SmartApp

from app.services.beautify import beautify_dict
Expand Down Expand Up @@ -64,11 +64,3 @@ async def build_static_image_url(image_file: Image, smartapp: SmartApp) -> str:
)

return link


def build_incoming_command_text(message: IncomingMessage) -> str:
return (
f"Incoming command:\n\n"
f"body:\n```\n{message.body}\n```\n\n"
f"data:\n```\n{beautify_dict(message.data)}\n```"
)

0 comments on commit b02dceb

Please sign in to comment.