Skip to content

Commit cb4732a

Browse files
committed
[refactoring]: change name 'Context' to 'HandlerContext'
1 parent 249f20b commit cb4732a

File tree

8 files changed

+37
-37
lines changed

8 files changed

+37
-37
lines changed

docs/code/polling/events.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
from glQiwiApi import QiwiWallet
22
from glQiwiApi.core.event_fetching import executor
33
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
4-
from glQiwiApi.core.event_fetching.executor import Context
4+
from glQiwiApi.core.event_fetching.executor import HandlerContext
55
from glQiwiApi.qiwi.clients.wallet.types import Transaction
66

77
qiwi_dp = QiwiDispatcher()
88
wallet = QiwiWallet(api_access_token='token', phone_number='+phone number')
99

1010

1111
@qiwi_dp.transaction_handler()
12-
async def handle_transaction(t: Transaction, ctx: Context):
12+
async def handle_transaction(t: Transaction, ctx: HandlerContext):
1313
"""Handle transaction here"""
1414
ctx.wallet # this way you can use QiwiWallet instance to avoid global variables
1515

1616

17-
async def on_startup(ctx: Context):
17+
async def on_startup(ctx: HandlerContext):
1818
ctx.wallet # do something here
1919

2020

21-
async def on_shutdown(ctx: Context):
21+
async def on_shutdown(ctx: HandlerContext):
2222
pass
2323

2424

docs/code/polling/qiwi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from glQiwiApi import QiwiWallet
22
from glQiwiApi.core.event_fetching import executor
33
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
4-
from glQiwiApi.core.event_fetching.executor import Context
4+
from glQiwiApi.core.event_fetching.executor import HandlerContext
55
from glQiwiApi.core.event_fetching.filters import ExceptionFilter
66
from glQiwiApi.qiwi.clients.wallet.types import Transaction
77
from glQiwiApi.qiwi.exceptions import QiwiAPIError
@@ -11,13 +11,13 @@
1111

1212

1313
@qiwi_dp.transaction_handler()
14-
async def handle_transaction(t: Transaction, ctx: Context):
14+
async def handle_transaction(t: Transaction, ctx: HandlerContext):
1515
"""Handle transaction here"""
1616
ctx.wallet # this way you can use QiwiWallet instance to avoid global variables
1717

1818

1919
@qiwi_dp.exception_handler(ExceptionFilter(QiwiAPIError))
20-
async def handle_exception(err: QiwiAPIError, ctx: Context):
20+
async def handle_exception(err: QiwiAPIError, ctx: HandlerContext):
2121
pass
2222

2323

docs/code/polling/with_aiogram.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from glQiwiApi import QiwiWallet
55
from glQiwiApi.core.event_fetching import executor
66
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
7-
from glQiwiApi.core.event_fetching.executor import Context
7+
from glQiwiApi.core.event_fetching.executor import HandlerContext
88
from glQiwiApi.plugins import AiogramPollingPlugin
99
from glQiwiApi.qiwi.clients.wallet.types import Transaction
1010

@@ -15,7 +15,7 @@
1515

1616

1717
@qiwi_dp.transaction_handler()
18-
async def handle_transaction(t: Transaction, ctx: Context):
18+
async def handle_transaction(t: Transaction, ctx: HandlerContext):
1919
"""Handle transaction here"""
2020
ctx.wallet # this way you can use QiwiWallet instance to avoid global variables
2121

docs/code/polling/with_aiogram_non_blocking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from glQiwiApi import QiwiWallet
66
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
7-
from glQiwiApi.core.event_fetching.executor import Context, start_non_blocking_qiwi_api_polling
7+
from glQiwiApi.core.event_fetching.executor import HandlerContext, start_non_blocking_qiwi_api_polling
88
from glQiwiApi.qiwi.clients.wallet.types import Transaction
99

1010
qiwi_dp = QiwiDispatcher()
@@ -14,7 +14,7 @@
1414

1515

1616
@qiwi_dp.transaction_handler()
17-
async def handle_transaction(t: Transaction, ctx: Context):
17+
async def handle_transaction(t: Transaction, ctx: HandlerContext):
1818
"""Handle transaction here"""
1919

2020

docs/code/polling/without_globals.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from glQiwiApi import QiwiWrapper
77
from glQiwiApi.core.event_fetching import executor
88
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
9-
from glQiwiApi.core.event_fetching.executor import Context
9+
from glQiwiApi.core.event_fetching.executor import HandlerContext
1010
from glQiwiApi.plugins import AiogramPollingPlugin
1111
from glQiwiApi.qiwi.clients.wallet.types import Transaction
1212

@@ -20,16 +20,16 @@ async def aiogram_message_handler(msg: types.Message):
2020
await msg.answer(text='Привет😇')
2121

2222

23-
async def qiwi_transaction_handler(update: Transaction, ctx: Context):
23+
async def qiwi_transaction_handler(update: Transaction, ctx: HandlerContext):
2424
print(update)
2525

2626

27-
def on_startup(ctx: Context) -> None:
27+
def on_startup(ctx: HandlerContext) -> None:
2828
logger.info('This message logged on startup')
2929
register_handlers(ctx)
3030

3131

32-
def register_handlers(ctx: Context):
32+
def register_handlers(ctx: HandlerContext):
3333
ctx['qiwi_dp'].transaction_handler()(qiwi_transaction_handler)
3434
dispatcher = cast(Dispatcher, ctx['dp'])
3535
dispatcher.register_message_handler(aiogram_message_handler)

docs/code/webhooks/qiwi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from glQiwiApi import QiwiWallet
88
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
9-
from glQiwiApi.core.event_fetching.executor import Context, configure_app_for_qiwi_webhooks
9+
from glQiwiApi.core.event_fetching.executor import HandlerContext, configure_app_for_qiwi_webhooks
1010
from glQiwiApi.core.event_fetching.webhooks.config import (
1111
EncryptionConfig,
1212
HookRegistrationConfig,
@@ -21,7 +21,7 @@
2121

2222

2323
@qiwi_dp.bill_handler()
24-
async def handle_webhook(webhook: BillWebhook, ctx: Context):
24+
async def handle_webhook(webhook: BillWebhook, ctx: HandlerContext):
2525
# handle bill
2626
bill = webhook.bill
2727

glQiwiApi/core/event_fetching/executor.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def is_awaitable(self) -> bool:
5656
class ExecutorEvent:
5757
def __init__(
5858
self,
59-
context: Context,
59+
context: HandlerContext,
6060
init_handlers: Iterable[Optional[_EventHandlerType]] = (),
6161
loop: Optional[asyncio.AbstractEventLoop] = None,
6262
) -> None:
@@ -94,7 +94,7 @@ def start_webhook(
9494
on_startup: Optional[_EventHandlerType] = None,
9595
on_shutdown: Optional[_EventHandlerType] = None,
9696
loop: Optional[asyncio.AbstractEventLoop] = None,
97-
context: Union[Dict[str, Any], Context, None] = None,
97+
context: Union[Dict[str, Any], HandlerContext, None] = None,
9898
) -> None:
9999
"""
100100
Blocking function that listens for webhooks.
@@ -121,7 +121,7 @@ def start_webhook(
121121
on_shutdown=on_shutdown,
122122
on_startup=on_startup,
123123
loop=loop,
124-
context=Context(context),
124+
context=HandlerContext(context),
125125
)
126126
executor.start_webhook(config=webhook_config)
127127

@@ -135,7 +135,7 @@ def start_polling(
135135
on_startup: Optional[_EventHandlerType] = None,
136136
on_shutdown: Optional[_EventHandlerType] = None,
137137
loop: Optional[asyncio.AbstractEventLoop] = None,
138-
context: Union[Dict[str, Any], Context, None] = None,
138+
context: Union[Dict[str, Any], HandlerContext, None] = None,
139139
) -> None:
140140
"""
141141
Setup for long-polling mode. Support only `glQiwiApi.types.Transaction` as event.
@@ -167,7 +167,7 @@ def start_polling(
167167
on_startup=on_startup,
168168
on_shutdown=on_shutdown,
169169
loop=loop,
170-
context=Context(context),
170+
context=HandlerContext(context),
171171
)
172172
executor.start_polling()
173173

@@ -180,7 +180,7 @@ async def start_non_blocking_qiwi_api_polling(
180180
on_startup: Optional[_EventHandlerType] = None,
181181
on_shutdown: Optional[_EventHandlerType] = None,
182182
loop: Optional[asyncio.AbstractEventLoop] = None,
183-
context: Union[Dict[str, Any], Context, None] = None,
183+
context: Union[Dict[str, Any], HandlerContext, None] = None,
184184
) -> asyncio.Task:
185185
if context is None:
186186
context = {}
@@ -192,7 +192,7 @@ async def start_non_blocking_qiwi_api_polling(
192192
on_startup=on_startup,
193193
on_shutdown=on_shutdown,
194194
loop=loop,
195-
context=Context(context),
195+
context=HandlerContext(context),
196196
)
197197
return await executor.start_non_blocking_polling()
198198

@@ -203,11 +203,11 @@ def configure_app_for_qiwi_webhooks(
203203
app: web.Application,
204204
cfg: WebhookConfig,
205205
) -> web.Application:
206-
executor = WebhookExecutor(wallet, dispatcher, context=Context({}))
206+
executor = WebhookExecutor(wallet, dispatcher, context=HandlerContext({}))
207207
return executor.add_routes_for_webhook(app, cfg)
208208

209209

210-
class Context(Dict[str, Any]):
210+
class HandlerContext(Dict[str, Any]):
211211
def __getattr__(self, item: str) -> Any:
212212
return self[item]
213213

@@ -224,7 +224,7 @@ def __init__(
224224
self,
225225
dispatcher: BaseDispatcher,
226226
*plugins: Pluggable,
227-
context: Context,
227+
context: HandlerContext,
228228
loop: Optional[asyncio.AbstractEventLoop] = None,
229229
on_startup: Optional[Callable[..., Any]] = None,
230230
on_shutdown: Optional[Callable[..., Any]] = None,
@@ -290,7 +290,7 @@ def __init__(
290290
wallet: Union[QiwiWallet, QiwiWrapper],
291291
dispatcher: BaseDispatcher,
292292
*plugins: Pluggable,
293-
context: Context,
293+
context: HandlerContext,
294294
loop: Optional[asyncio.AbstractEventLoop] = None,
295295
timeout: Union[float, int] = DEFAULT_TIMEOUT,
296296
skip_updates: bool = False,
@@ -401,7 +401,7 @@ def __init__(
401401
wallet: Union[QiwiWallet, QiwiWrapper],
402402
dispatcher: BaseDispatcher,
403403
*plugins: Pluggable,
404-
context: Context,
404+
context: HandlerContext,
405405
on_startup: Optional[_EventHandlerType] = None,
406406
on_shutdown: Optional[_EventHandlerType] = None,
407407
loop: Optional[asyncio.AbstractEventLoop] = None,

tests/unit/test_event_fetching/test_executor.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from glQiwiApi import QiwiWallet
99
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
1010
from glQiwiApi.core.event_fetching.executor import (
11-
Context,
11+
HandlerContext,
1212
ExecutorEvent,
1313
start_non_blocking_qiwi_api_polling,
1414
)
@@ -18,9 +18,9 @@
1818

1919
class TestExecutorEvent:
2020
async def test_fire(self):
21-
context = Context({'api_key': 'fake_api_key'})
21+
context = HandlerContext({'api_key': 'fake_api_key'})
2222

23-
async def init_event(ctx: Context) -> NoReturn:
23+
async def init_event(ctx: HandlerContext) -> NoReturn:
2424
assert ctx == context
2525
raise RuntimeError()
2626

@@ -32,11 +32,11 @@ async def init_event(ctx: Context) -> NoReturn:
3232
await event.fire()
3333

3434
async def test_fire_sync_handlers(self):
35-
context = Context({'api_key': 'fake_api_key'})
35+
context = HandlerContext({'api_key': 'fake_api_key'})
3636

3737
event = ExecutorEvent(context)
3838

39-
def on_event(ctx: Context) -> NoReturn:
39+
def on_event(ctx: HandlerContext) -> NoReturn:
4040
assert ctx == context
4141
raise RuntimeError()
4242

@@ -65,20 +65,20 @@ async def history(
6565

6666

6767
async def test_start_non_blocking_qiwi_api_polling(transaction: Transaction) -> None:
68-
c = Context({'api_key': 'my_api_key'})
68+
c = HandlerContext({'api_key': 'my_api_key'})
6969
wallet = WalletStub(transaction)
7070
dp = QiwiDispatcher()
7171

7272
handled_transaction_event = asyncio.Event()
7373
handle_on_startup = asyncio.Event()
7474

7575
@dp.transaction_handler()
76-
async def handle_transaction(txn: Transaction, ctx: Context):
76+
async def handle_transaction(txn: Transaction, ctx: HandlerContext):
7777
assert ctx['api_key'] == 'my_api_key'
7878
assert ctx.wallet == wallet
7979
handled_transaction_event.set()
8080

81-
async def on_startup(ctx: Context):
81+
async def on_startup(ctx: HandlerContext):
8282
assert ctx['api_key'] == 'my_api_key'
8383
assert ctx.wallet == wallet
8484
handle_on_startup.set()

0 commit comments

Comments
 (0)