From 673cc2eb39049063687817b395fe405ceef2e325 Mon Sep 17 00:00:00 2001 From: GLEF1X Date: Tue, 20 Dec 2022 18:37:22 -0500 Subject: [PATCH] [refactoring]: change name 'Context' to 'HandlerContext' --- docs/code/polling/events.py | 8 +++--- docs/code/polling/qiwi.py | 6 ++--- docs/code/polling/with_aiogram.py | 4 +-- .../code/polling/with_aiogram_non_blocking.py | 4 +-- docs/code/polling/without_globals.py | 8 +++--- docs/code/webhooks/qiwi.py | 4 +-- glQiwiApi/core/event_fetching/executor.py | 25 ++++++++++--------- .../unit/test_event_fetching/test_executor.py | 16 ++++++------ 8 files changed, 38 insertions(+), 37 deletions(-) diff --git a/docs/code/polling/events.py b/docs/code/polling/events.py index 69138a8d..07042375 100644 --- a/docs/code/polling/events.py +++ b/docs/code/polling/events.py @@ -1,7 +1,7 @@ from glQiwiApi import QiwiWallet from glQiwiApi.core.event_fetching import executor from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher -from glQiwiApi.core.event_fetching.executor import Context +from glQiwiApi.core.event_fetching.executor import HandlerContext from glQiwiApi.qiwi.clients.wallet.types import Transaction qiwi_dp = QiwiDispatcher() @@ -9,16 +9,16 @@ @qiwi_dp.transaction_handler() -async def handle_transaction(t: Transaction, ctx: Context): +async def handle_transaction(t: Transaction, ctx: HandlerContext): """Handle transaction here""" ctx.wallet # this way you can use QiwiWallet instance to avoid global variables -async def on_startup(ctx: Context): +async def on_startup(ctx: HandlerContext): ctx.wallet # do something here -async def on_shutdown(ctx: Context): +async def on_shutdown(ctx: HandlerContext): pass diff --git a/docs/code/polling/qiwi.py b/docs/code/polling/qiwi.py index bde69ae5..74450d53 100644 --- a/docs/code/polling/qiwi.py +++ b/docs/code/polling/qiwi.py @@ -1,7 +1,7 @@ from glQiwiApi import QiwiWallet from glQiwiApi.core.event_fetching import executor from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher -from glQiwiApi.core.event_fetching.executor import Context +from glQiwiApi.core.event_fetching.executor import HandlerContext from glQiwiApi.core.event_fetching.filters import ExceptionFilter from glQiwiApi.qiwi.clients.wallet.types import Transaction from glQiwiApi.qiwi.exceptions import QiwiAPIError @@ -11,13 +11,13 @@ @qiwi_dp.transaction_handler() -async def handle_transaction(t: Transaction, ctx: Context): +async def handle_transaction(t: Transaction, ctx: HandlerContext): """Handle transaction here""" ctx.wallet # this way you can use QiwiWallet instance to avoid global variables @qiwi_dp.exception_handler(ExceptionFilter(QiwiAPIError)) -async def handle_exception(err: QiwiAPIError, ctx: Context): +async def handle_exception(err: QiwiAPIError, ctx: HandlerContext): pass diff --git a/docs/code/polling/with_aiogram.py b/docs/code/polling/with_aiogram.py index c957207a..e359a8d2 100644 --- a/docs/code/polling/with_aiogram.py +++ b/docs/code/polling/with_aiogram.py @@ -4,7 +4,7 @@ from glQiwiApi import QiwiWallet from glQiwiApi.core.event_fetching import executor from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher -from glQiwiApi.core.event_fetching.executor import Context +from glQiwiApi.core.event_fetching.executor import HandlerContext from glQiwiApi.plugins import AiogramPollingPlugin from glQiwiApi.qiwi.clients.wallet.types import Transaction @@ -15,7 +15,7 @@ @qiwi_dp.transaction_handler() -async def handle_transaction(t: Transaction, ctx: Context): +async def handle_transaction(t: Transaction, ctx: HandlerContext): """Handle transaction here""" ctx.wallet # this way you can use QiwiWallet instance to avoid global variables diff --git a/docs/code/polling/with_aiogram_non_blocking.py b/docs/code/polling/with_aiogram_non_blocking.py index 47bb3d76..6af17f65 100644 --- a/docs/code/polling/with_aiogram_non_blocking.py +++ b/docs/code/polling/with_aiogram_non_blocking.py @@ -4,7 +4,7 @@ from glQiwiApi import QiwiWallet from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher -from glQiwiApi.core.event_fetching.executor import Context, start_non_blocking_qiwi_api_polling +from glQiwiApi.core.event_fetching.executor import HandlerContext, start_non_blocking_qiwi_api_polling from glQiwiApi.qiwi.clients.wallet.types import Transaction qiwi_dp = QiwiDispatcher() @@ -14,7 +14,7 @@ @qiwi_dp.transaction_handler() -async def handle_transaction(t: Transaction, ctx: Context): +async def handle_transaction(t: Transaction, ctx: HandlerContext): """Handle transaction here""" diff --git a/docs/code/polling/without_globals.py b/docs/code/polling/without_globals.py index 883195bc..5e6cc06e 100644 --- a/docs/code/polling/without_globals.py +++ b/docs/code/polling/without_globals.py @@ -6,7 +6,7 @@ from glQiwiApi import QiwiWrapper from glQiwiApi.core.event_fetching import executor from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher -from glQiwiApi.core.event_fetching.executor import Context +from glQiwiApi.core.event_fetching.executor import HandlerContext from glQiwiApi.plugins import AiogramPollingPlugin from glQiwiApi.qiwi.clients.wallet.types import Transaction @@ -20,16 +20,16 @@ async def aiogram_message_handler(msg: types.Message): await msg.answer(text='Привет😇') -async def qiwi_transaction_handler(update: Transaction, ctx: Context): +async def qiwi_transaction_handler(update: Transaction, ctx: HandlerContext): print(update) -def on_startup(ctx: Context) -> None: +def on_startup(ctx: HandlerContext) -> None: logger.info('This message logged on startup') register_handlers(ctx) -def register_handlers(ctx: Context): +def register_handlers(ctx: HandlerContext): ctx['qiwi_dp'].transaction_handler()(qiwi_transaction_handler) dispatcher = cast(Dispatcher, ctx['dp']) dispatcher.register_message_handler(aiogram_message_handler) diff --git a/docs/code/webhooks/qiwi.py b/docs/code/webhooks/qiwi.py index 5f8108ad..709e7af7 100644 --- a/docs/code/webhooks/qiwi.py +++ b/docs/code/webhooks/qiwi.py @@ -6,7 +6,7 @@ from glQiwiApi import QiwiWallet from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher -from glQiwiApi.core.event_fetching.executor import Context, configure_app_for_qiwi_webhooks +from glQiwiApi.core.event_fetching.executor import HandlerContext, configure_app_for_qiwi_webhooks from glQiwiApi.core.event_fetching.webhooks.config import ( EncryptionConfig, HookRegistrationConfig, @@ -21,7 +21,7 @@ @qiwi_dp.bill_handler() -async def handle_webhook(webhook: BillWebhook, ctx: Context): +async def handle_webhook(webhook: BillWebhook, ctx: HandlerContext): # handle bill bill = webhook.bill diff --git a/glQiwiApi/core/event_fetching/executor.py b/glQiwiApi/core/event_fetching/executor.py index b901f4a4..c03cc369 100644 --- a/glQiwiApi/core/event_fetching/executor.py +++ b/glQiwiApi/core/event_fetching/executor.py @@ -56,7 +56,7 @@ def is_awaitable(self) -> bool: class ExecutorEvent: def __init__( self, - context: Context, + context: HandlerContext, init_handlers: Iterable[Optional[_EventHandlerType]] = (), loop: Optional[asyncio.AbstractEventLoop] = None, ) -> None: @@ -94,7 +94,7 @@ def start_webhook( on_startup: Optional[_EventHandlerType] = None, on_shutdown: Optional[_EventHandlerType] = None, loop: Optional[asyncio.AbstractEventLoop] = None, - context: Union[Dict[str, Any], Context, None] = None, + context: Union[Dict[str, Any], HandlerContext, None] = None, ) -> None: """ Blocking function that listens for webhooks. @@ -121,7 +121,7 @@ def start_webhook( on_shutdown=on_shutdown, on_startup=on_startup, loop=loop, - context=Context(context), + context=HandlerContext(context), ) executor.start_webhook(config=webhook_config) @@ -135,7 +135,7 @@ def start_polling( on_startup: Optional[_EventHandlerType] = None, on_shutdown: Optional[_EventHandlerType] = None, loop: Optional[asyncio.AbstractEventLoop] = None, - context: Union[Dict[str, Any], Context, None] = None, + context: Union[Dict[str, Any], HandlerContext, None] = None, ) -> None: """ Setup for long-polling mode. Support only `glQiwiApi.types.Transaction` as event. @@ -167,7 +167,7 @@ def start_polling( on_startup=on_startup, on_shutdown=on_shutdown, loop=loop, - context=Context(context), + context=HandlerContext(context), ) executor.start_polling() @@ -180,7 +180,7 @@ async def start_non_blocking_qiwi_api_polling( on_startup: Optional[_EventHandlerType] = None, on_shutdown: Optional[_EventHandlerType] = None, loop: Optional[asyncio.AbstractEventLoop] = None, - context: Union[Dict[str, Any], Context, None] = None, + context: Union[Dict[str, Any], HandlerContext, None] = None, ) -> asyncio.Task: if context is None: context = {} @@ -192,7 +192,7 @@ async def start_non_blocking_qiwi_api_polling( on_startup=on_startup, on_shutdown=on_shutdown, loop=loop, - context=Context(context), + context=HandlerContext(context), ) return await executor.start_non_blocking_polling() @@ -203,11 +203,11 @@ def configure_app_for_qiwi_webhooks( app: web.Application, cfg: WebhookConfig, ) -> web.Application: - executor = WebhookExecutor(wallet, dispatcher, context=Context({})) + executor = WebhookExecutor(wallet, dispatcher, context=HandlerContext({})) return executor.add_routes_for_webhook(app, cfg) -class Context(Dict[str, Any]): +class HandlerContext(Dict[str, Any]): def __getattr__(self, item: str) -> Any: return self[item] @@ -216,6 +216,7 @@ def wallet(self) -> Union[QiwiWallet, QiwiWrapper]: return cast(Union[QiwiWallet, QiwiWrapper], self[WALLET_CTX_KEY]) +# for backward compatibility Context = HandlerContext @@ -224,7 +225,7 @@ def __init__( self, dispatcher: BaseDispatcher, *plugins: Pluggable, - context: Context, + context: HandlerContext, loop: Optional[asyncio.AbstractEventLoop] = None, on_startup: Optional[Callable[..., Any]] = None, on_shutdown: Optional[Callable[..., Any]] = None, @@ -290,7 +291,7 @@ def __init__( wallet: Union[QiwiWallet, QiwiWrapper], dispatcher: BaseDispatcher, *plugins: Pluggable, - context: Context, + context: HandlerContext, loop: Optional[asyncio.AbstractEventLoop] = None, timeout: Union[float, int] = DEFAULT_TIMEOUT, skip_updates: bool = False, @@ -401,7 +402,7 @@ def __init__( wallet: Union[QiwiWallet, QiwiWrapper], dispatcher: BaseDispatcher, *plugins: Pluggable, - context: Context, + context: HandlerContext, on_startup: Optional[_EventHandlerType] = None, on_shutdown: Optional[_EventHandlerType] = None, loop: Optional[asyncio.AbstractEventLoop] = None, diff --git a/tests/unit/test_event_fetching/test_executor.py b/tests/unit/test_event_fetching/test_executor.py index c249ac02..12bc5803 100644 --- a/tests/unit/test_event_fetching/test_executor.py +++ b/tests/unit/test_event_fetching/test_executor.py @@ -8,7 +8,7 @@ from glQiwiApi import QiwiWallet from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher from glQiwiApi.core.event_fetching.executor import ( - Context, + HandlerContext, ExecutorEvent, start_non_blocking_qiwi_api_polling, ) @@ -18,9 +18,9 @@ class TestExecutorEvent: async def test_fire(self): - context = Context({'api_key': 'fake_api_key'}) + context = HandlerContext({'api_key': 'fake_api_key'}) - async def init_event(ctx: Context) -> NoReturn: + async def init_event(ctx: HandlerContext) -> NoReturn: assert ctx == context raise RuntimeError() @@ -32,11 +32,11 @@ async def init_event(ctx: Context) -> NoReturn: await event.fire() async def test_fire_sync_handlers(self): - context = Context({'api_key': 'fake_api_key'}) + context = HandlerContext({'api_key': 'fake_api_key'}) event = ExecutorEvent(context) - def on_event(ctx: Context) -> NoReturn: + def on_event(ctx: HandlerContext) -> NoReturn: assert ctx == context raise RuntimeError() @@ -65,7 +65,7 @@ async def history( async def test_start_non_blocking_qiwi_api_polling(transaction: Transaction) -> None: - c = Context({'api_key': 'my_api_key'}) + c = HandlerContext({'api_key': 'my_api_key'}) wallet = WalletStub(transaction) dp = QiwiDispatcher() @@ -73,12 +73,12 @@ async def test_start_non_blocking_qiwi_api_polling(transaction: Transaction) -> handle_on_startup = asyncio.Event() @dp.transaction_handler() - async def handle_transaction(txn: Transaction, ctx: Context): + async def handle_transaction(txn: Transaction, ctx: HandlerContext): assert ctx['api_key'] == 'my_api_key' assert ctx.wallet == wallet handled_transaction_event.set() - async def on_startup(ctx: Context): + async def on_startup(ctx: HandlerContext): assert ctx['api_key'] == 'my_api_key' assert ctx.wallet == wallet handle_on_startup.set()