Add optional route_name argument to dispatcher.webhook.configure_app and utils.executor.start_webhook with default value "webhook_handler"

This commit is contained in:
Roman Inflianskas 2018-10-31 15:24:28 +03:00 committed by Roman Inflianskas
parent 61e0f3601d
commit b515f5abc2
2 changed files with 15 additions and 9 deletions

View file

@ -21,6 +21,7 @@ from ..utils.exceptions import TimeoutWarning
from ..utils.payload import prepare_arg from ..utils.payload import prepare_arg
DEFAULT_WEB_PATH = '/webhook' DEFAULT_WEB_PATH = '/webhook'
DEFAULT_ROUTE_NAME = 'webhook_handler'
BOT_DISPATCHER_KEY = 'BOT_DISPATCHER' BOT_DISPATCHER_KEY = 'BOT_DISPATCHER'
RESPONSE_TIMEOUT = 55 RESPONSE_TIMEOUT = 55
@ -266,16 +267,17 @@ class GoneRequestHandler(web.View):
raise HTTPGone() raise HTTPGone()
def configure_app(dispatcher, app: web.Application, path=DEFAULT_WEB_PATH): def configure_app(dispatcher, app: web.Application, path=DEFAULT_WEB_PATH, route_name=DEFAULT_ROUTE_NAME):
""" """
You can prepare web.Application for working with webhook handler. You can prepare web.Application for working with webhook handler.
:param dispatcher: Dispatcher instance :param dispatcher: Dispatcher instance
:param app: :class:`aiohttp.web.Application` :param app: :class:`aiohttp.web.Application`
:param path: Path to your webhook. :param path: Path to your webhook.
:param route_name: Name of webhook handler route
:return: :return:
""" """
app.router.add_route('*', path, WebhookRequestHandler, name='webhook_handler') app.router.add_route('*', path, WebhookRequestHandler, name=route_name)
app[BOT_DISPATCHER_KEY] = dispatcher app[BOT_DISPATCHER_KEY] = dispatcher

View file

@ -7,7 +7,7 @@ from warnings import warn
from aiohttp import web from aiohttp import web
from ..bot.api import log from ..bot.api import log
from ..dispatcher.webhook import BOT_DISPATCHER_KEY, WebhookRequestHandler from ..dispatcher.webhook import BOT_DISPATCHER_KEY, DEFAULT_ROUTE_NAME, WebhookRequestHandler
APP_EXECUTOR_KEY = 'APP_EXECUTOR' APP_EXECUTOR_KEY = 'APP_EXECUTOR'
@ -39,7 +39,8 @@ def start_polling(dispatcher, *, loop=None, skip_updates=False, reset_webhook=Tr
def start_webhook(dispatcher, webhook_path, *, loop=None, skip_updates=None, def start_webhook(dispatcher, webhook_path, *, loop=None, skip_updates=None,
on_startup=None, on_shutdown=None, check_ip=False, retry_after=None, **kwargs): on_startup=None, on_shutdown=None, check_ip=False, retry_after=None, route_name=DEFAULT_ROUTE_NAME,
**kwargs):
""" """
Start bot in webhook mode Start bot in webhook mode
@ -50,6 +51,7 @@ def start_webhook(dispatcher, webhook_path, *, loop=None, skip_updates=None,
:param on_startup: :param on_startup:
:param on_shutdown: :param on_shutdown:
:param check_ip: :param check_ip:
:param route_name:
:param kwargs: :param kwargs:
:return: :return:
""" """
@ -57,7 +59,7 @@ def start_webhook(dispatcher, webhook_path, *, loop=None, skip_updates=None,
loop=loop) loop=loop)
_setup_callbacks(executor, on_startup, on_shutdown) _setup_callbacks(executor, on_startup, on_shutdown)
executor.start_webhook(webhook_path, **kwargs) executor.start_webhook(webhook_path, route_name=route_name, **kwargs)
def start(dispatcher, future, *, loop=None, skip_updates=None, def start(dispatcher, future, *, loop=None, skip_updates=None,
@ -181,7 +183,7 @@ class Executor:
# self.loop.set_task_factory(context.task_factory) # self.loop.set_task_factory(context.task_factory)
def _prepare_webhook(self, path=None, handler=WebhookRequestHandler): def _prepare_webhook(self, path=None, handler=WebhookRequestHandler, route_name=DEFAULT_ROUTE_NAME):
self._check_frozen() self._check_frozen()
self._freeze = True self._freeze = True
@ -199,7 +201,7 @@ class Executor:
return return
if path is not None: if path is not None:
app.router.add_route('*', path, handler, name='webhook_handler') app.router.add_route('*', path, handler, name=route_name)
async def _wrap_callback(cb, _): async def _wrap_callback(cb, _):
return await cb(self.dispatcher) return await cb(self.dispatcher)
@ -219,16 +221,18 @@ class Executor:
app[self._identity] = datetime.datetime.now() app[self._identity] = datetime.datetime.now()
app['_check_ip'] = self.check_ip app['_check_ip'] = self.check_ip
def start_webhook(self, webhook_path=None, request_handler=WebhookRequestHandler, **kwargs): def start_webhook(self, webhook_path=None, request_handler=WebhookRequestHandler, route_name=DEFAULT_ROUTE_NAME,
**kwargs):
""" """
Start bot in webhook mode Start bot in webhook mode
:param webhook_path: :param webhook_path:
:param request_handler: :param request_handler:
:param route_name: Name of webhook handler route
:param kwargs: :param kwargs:
:return: :return:
""" """
self._prepare_webhook(webhook_path, request_handler) self._prepare_webhook(webhook_path, request_handler, route_name)
self.loop.run_until_complete(self._startup_webhook()) self.loop.run_until_complete(self._startup_webhook())
web.run_app(self._web_app, **kwargs) web.run_app(self._web_app, **kwargs)