Asynchronous Client (AIO)
Relevant source files
The following files were used as context for generating this wiki page:
The Asynchronous Client (AIO) module provides a high-performance, non-blocking interface for interacting with MBBank's retail banking services. It is designed for applications requiring concurrent operations, such as automated monitoring or high-volume transaction processing, leveraging the aiohttp library for asynchronous HTTP networking.
Sources: mbbank/aio/mbbank.py:1-50, mbbank/aio/init.py:1-15
Core Architecture
The AIO client is built upon a hierarchical structure where MBBankAsync inherits shared logic from MBBankBase. This architecture ensures consistency between synchronous and asynchronous implementations while providing specialized methods for non-blocking execution.
Class Hierarchy and Dependencies
The following diagram illustrates the relationship between the core asynchronous classes and their base components.
The MBBankAsync class manages a thread pool executor specifically for CPU-bound tasks like WASM encryption, preventing the event loop from blocking during cryptographic operations.
Sources: mbbank/aio/mbbank.py:53-85, mbbank/base/mbbank.py:7-50
Authentication Flow
Authentication is an asynchronous process involving multi-step verification, including captcha solving and WASM-based payload encryption.
Login Sequence
The authentication flow requires fetching a captcha image, processing it through an OCR class, and submitting encrypted credentials.
Authentication Logic Details
| Component | Description | Source |
|---|---|---|
_authenticate | Internal loop that retries login up to retry_times (default 30) if captcha errors (GW283) occur. | mbbank/aio/mbbank.py:149-170 |
wasm_encrypt | CPU-bound function executed in THREAD_POOL to encrypt login payloads using a local WASM runtime. | mbbank/aio/mbbank.py:126-128 |
deviceIdCommon | Unique identifier generated per instance using the current timestamp. | mbbank/base/mbbank.py:58-59 |
Sources: mbbank/aio/mbbank.py:112-170, mbbank/wasm_helper/init.py:365-385
Asynchronous Transfer System
Money transfers are handled via the TransferContextAsync, which manages the stateful multi-step process required by the bank's security protocols.
Transfer Life Cycle
The transfer process follows a specific lifecycle: initialization, inquiry, verification, authentication, and execution.
Key Transfer Methods
start(): Orchestrates the initial inquiry for bank codes and account names, followed by a verification call.get_qr_code(): Generates a transaction ID (TRANID) used for secondary authentication (e.g., via the MBBank mobile app).transfer(otp, auth_type): The final step that submits the crafted OTP and authentication type to complete the transaction.
Sources: mbbank/aio/transfer.py:171-218, mbbank/aio/mbbank.py:441-460
Data Models and Responses
The module uses Pydantic-based models to ensure type safety for asynchronous responses.
| Model | Purpose | Relevant Fields |
|---|---|---|
BalanceResponseModal | Account balances | acct_list, currencyEquivalent |
TransactionHistoryResponseModal | Account history | transactionHistoryList |
TransferResponseModal | Transfer result | ftId, totalFee, authenType |
AuthTransferResponseModal | Security methods | authList (code, name, alias) |
Sources: mbbank/modals/transfer.py:20-60, mbbank/aio/mbbank.py:192-212
Network and Session Management
The AIO client utilizes aiohttp.ClientSession for all network requests.
- Session Persistence: The
sessionIdis maintained within the class instance and automatically injected into request headers. - Automatic Re-authentication: The
_reqmethod interceptsGW200error codes (session expired) and triggers an automatic_authenticate()call before retrying the original request. - Encryption: Sensitive endpoints (like transfer execution) use the
encrypt=Trueflag in_req, which triggers the WASM encryption routine.
Sources: mbbank/aio/mbbank.py:87-109, mbbank/aio/mbbank.py:172-212
The Asynchronous Client provides a robust framework for complex banking operations, ensuring that cryptographic and network latencies do not block application execution.