diff --git a/README.md b/README.md index 66dbab42..90c0e536 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ pip install glQiwiApi |aiofiles | saving receipts in pdf | |uvloop | Optional(can boost API), but work only on Linux| |pydantic | Json data validator. Very fast instead of custom| +|loguru | library which aims to bring enjoyable logging in Python| --- @@ -175,9 +176,9 @@ asyncio.run(main()) ## 🌟Webhooks & handlers ```python -import logging from glQiwiApi import QiwiWrapper, types +from glQiwiApi.utils import executor wallet = QiwiWrapper( api_access_token='token from https://qiwi.com/api/', @@ -194,22 +195,16 @@ async def get_transaction(event: types.WebHook): async def fetch_bill(notification: types.Notification): print(notification) - -FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - -wallet.start_webhook( - port=80, - level=logging.INFO, - format=FORMAT -) - + +executor.start_webhook(wallet, port=80) ``` -## 🧑🏻‍🔬Polling updates +## 🧑🏻🔬Polling updates ```python import datetime from glQiwiApi import QiwiWrapper, types +from glQiwiApi.utils import executor api_access_token = "your token" phone_number = "your number" @@ -229,7 +224,8 @@ def on_startup(wrapper: QiwiWrapper): wrapper.dispatcher.logger.info("This message logged on startup") -wallet.start_polling( +executor.start_polling( + wallet, get_updates_from=datetime.datetime.now() - datetime.timedelta(hours=1), on_startup=on_startup ) diff --git a/SETUP_REQUIREMENTS.txt b/SETUP_REQUIREMENTS.txt index 22e0c25b..02b32f53 100644 --- a/SETUP_REQUIREMENTS.txt +++ b/SETUP_REQUIREMENTS.txt @@ -1,7 +1,7 @@ -pytz>=2021.1 -aiofiles>=0.6.0 -aiohttp>=3.7.4post0 -pydantic>=1.8.2 -wheel>=0.36.2 -loguru>=0.5.3 +pytz==2021.1 +aiofiles==0.6.0 +aiohttp==3.7.4post0 +pydantic==1.8.2 +wheel==0.36.2 +loguru==0.5.3 diff --git a/docs/API.rst b/docs/API.rst index 63289353..58fdb831 100644 --- a/docs/API.rst +++ b/docs/API.rst @@ -1,15 +1,15 @@ .. currentmodule:: glQiwiApi -=== -API -=== +============= +API reference +============= Payment wrappers ---------------- .. automodule:: glQiwiApi :members: QiwiWrapper, YooMoneyAPI, QiwiMaps -QiwiWebhooks +Qiwi Webhooks ------------ .. automodule:: glQiwiApi.core.web_hooks.filter :members: @@ -21,6 +21,14 @@ QiwiWebhooks :members: +.. _Executor overview: + +Polling +------- +.. automodule:: glQiwiApi.utils.executor + :members: + + Low level API ------------- .. automodule:: glQiwiApi.core.basic_requests_api @@ -31,12 +39,20 @@ Low level API .. automodule:: glQiwiApi.core.storage :members: - + + +Aiogram integration +------------------- +.. automodule:: glQiwiApi.core.builtin.telegram + :members: + + Synchronous adapter ------------------- .. automodule:: glQiwiApi.utils.basics :members: sync, _await_sync, _run_forever_safe + Exceptions ---------- .. automodule:: glQiwiApi.utils.exceptions diff --git a/docs/conf.py b/docs/conf.py index f24a38cb..f06a26ba 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -42,6 +42,7 @@ 'sphinx.ext.autodoc', 'sphinx.ext.ifconfig', "sphinx.ext.napoleon", + 'sphinx.ext.autosectionlabel' ] # Add any paths that contain templates here, relative to this directory. @@ -130,15 +131,15 @@ # # The paper size ('letterpaper' or 'a4paper'). # # # # 'papersize': 'letterpaper', - +# # # The font size ('10pt', '11pt' or '12pt'). # # # # 'pointsize': '10pt', - +# # # Additional stuff for the LaTeX preamble. # # # # 'preamble': '', - +# # # Latex figure (float) alignment # # # # 'figure_align': 'htbp', diff --git a/docs/examples/qiwi/polling.rst b/docs/examples/qiwi/polling.rst index 2cf3bb50..c227e602 100644 --- a/docs/examples/qiwi/polling.rst +++ b/docs/examples/qiwi/polling.rst @@ -2,20 +2,29 @@ Polling updates ================ -.. tip:: 👩🏻‍🎨 ``start_polling`` has the same signature as ``start_webhook`` +.. tip:: 👩🏻🎨 ``start_polling`` has the same signature as ``start_webhook`` "under the hood" -👩🏻‍🔬This API method gives a chance to hook updates without webhooks on your machine, +👩🏻🔬This API method gives a chance to hook updates without webhooks on your machine, but it's possible to use both approaches anyway. -🧑‍🎓 *First of all, before starting polling, you need to register handlers, just like when installing webhooks* +🧑🎓 *First of all, before starting polling, you need to register handlers, just like when installing webhooks.* +Lets do it with decorators, *but we also can do it another way, using* ``wallet.dispatcher.register_transaction_handler`` .. literalinclude:: ./../../../examples/qiwi/polling.py :caption: Add handlers :language: python - :emphasize-lines: 14-16 + :emphasize-lines: 15-17 -👨‍🔬 Then, you can start polling, but, let's make it clear which arguments you should pass on to ``start_polling`` method. +🧙♀️So, we also have to import ``executor`` :ref:`Executor overview` and pass on our client, +that contains user-friendly functions ``start_polling`` and ``start_webhook``. + +.. literalinclude:: ./../../../examples/qiwi/polling.py + :caption: import executor module + :language: python + :emphasize-lines: 4, 24 + +👨🔬 Then, you can start polling, but, let's make it clear which arguments you should pass on to ``start_polling`` function. So, in this example we see ``get_updates_from`` and ``on_startup``, it means, that in example we want to receive notifications that came an hour ago and execute some function on startup of polling updates @@ -23,21 +32,27 @@ ago and execute some function on startup of polling updates .. literalinclude:: ./../../../examples/qiwi/polling.py :caption: Args of polling :language: python - :emphasize-lines: 23-26 + :emphasize-lines: 26-27 😼 As you can see, in the example we have a function that we pass as an argument to ``on_startup``. - As you may have guessed, this function will be executed at the beginning of the polling. .. literalinclude:: ./../../../examples/qiwi/polling.py :caption: Args of polling :language: python - :emphasize-lines: 19-20,25 + :emphasize-lines: 20-21,27 😻 If you did everything correctly, you will get something like this .. code-block:: bash - 2021-05-13 16:27:22,921 - [INFO] - Start polling! - 2021-05-13 16:27:22,922 - [INFO] - This message came on startup - 2021-05-13 16:27:23,938 - [INFO] - Stop polling! + 2021-06-05 17:21:07.423 | DEBUG | glQiwiApi.utils.executor:welcome:373 - Start polling! + +🧚♀️ *Also, you can very easily implement simultaneous polling of updates from both aiogram and QIWI API.* + +In the example below, we catch all text messages and return the same "Hello" response. + +.. literalinclude:: ./../../../examples/qiwi/aiogram_integration.py + :caption: polling together with aiogram + :language: python + :emphasize-lines: 12-13,38,21-23,1-2,5,6 diff --git a/examples/qiwi/aiogram_integration.py b/examples/qiwi/aiogram_integration.py new file mode 100644 index 00000000..cabdb513 --- /dev/null +++ b/examples/qiwi/aiogram_integration.py @@ -0,0 +1,39 @@ +from aiogram import Bot, Dispatcher +from aiogram import types + +from glQiwiApi import QiwiWrapper +from glQiwiApi.core.builtin import TelegramPollingProxy +from glQiwiApi.types import Transaction +from glQiwiApi.utils import executor + +api_access_token = "your token" +phone_number = "your number" + +bot = Bot("token from BotFather") +dp = Dispatcher(bot) + +wallet = QiwiWrapper( + api_access_token=api_access_token, + phone_number=phone_number +) + + +@dp.message_handler() +async def message_handler(msg: types.Message): + await msg.answer(text="Привет😇") + + +@wallet.transaction_handler() +async def my_first_handler(update: Transaction): + assert isinstance(update, Transaction) + + +def on_startup(wrapper: QiwiWrapper): + wrapper.dispatcher.logger.info("This message logged on startup") + + +executor.start_polling( + wallet, + on_startup=on_startup, + tg_app=TelegramPollingProxy(dp) +) diff --git a/examples/qiwi/p2p.py b/examples/qiwi/p2p.py index 00f24ca7..a1569bba 100644 --- a/examples/qiwi/p2p.py +++ b/examples/qiwi/p2p.py @@ -17,7 +17,7 @@ async def p2p_usage(): status_1 = (await w.check_p2p_bill_status( bill_id=bill.bill_id )) == 'PAID' - # Или, начиная с версии 0.2.0 + # Или можно так(выглядит лаконичнее на мой взгляд) status_2 = await bill.paid print(status_1 == status_2) # Это выдаст ошибку, так как не передан api_access_token и phone_number diff --git a/examples/qiwi/pretty_webhook.py b/examples/qiwi/pretty_webhook.py index 92092e23..55c34805 100644 --- a/examples/qiwi/pretty_webhook.py +++ b/examples/qiwi/pretty_webhook.py @@ -17,6 +17,4 @@ async def fetch_bill(notification: types.Notification): print(notification) -FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - executor.start_webhook(wallet, port=80) diff --git a/examples/qiwi/qiwi_webhook.py b/examples/qiwi/qiwi_webhook.py index f2599134..a41ba368 100644 --- a/examples/qiwi/qiwi_webhook.py +++ b/examples/qiwi/qiwi_webhook.py @@ -4,6 +4,7 @@ from glQiwiApi import QiwiWrapper, types from glQiwiApi.core.web_hooks.config import Path +from glQiwiApi.utils import executor TOKEN = 'token from https://qiwi.com/api/' QIWI_SECRET = 'secret token from https://qiwi.com/p2p-admin/' @@ -47,12 +48,10 @@ async def main2(event: types.Notification): bill_path="/my_webhook/" ) -wallet.start_webhook( +executor.start_webhook( + wallet, # You can pass on any port, but it must be open for web # You can use any VPS server to catching webhook or # your configured local machine - port=8080, - level=logging.INFO, - format=FORMAT, path=path ) diff --git a/glQiwiApi/__init__.py b/glQiwiApi/__init__.py index f2effed2..cc54dac3 100644 --- a/glQiwiApi/__init__.py +++ b/glQiwiApi/__init__.py @@ -5,7 +5,7 @@ from .utils.exceptions import * # NOQA from .yoo_money import YooMoneyAPI # NOQA -__version__ = '1.0alpha' +__version__ = '1.0.0' __all__ = ( ( diff --git a/setup.py b/setup.py index bfc39858..4cca59c1 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ 'tests', 'examples', 'examples.*', 'tests', 'tests.*') ), name="glQiwiApi", # Replace with your own username - version="1.0beta", + version="1.0.0", author="GLEF1X", author_email="glebgar567@gmail.com", description="Light and fast wrapper of QIWI and YooMoney api's",