From 7603b06856bbb6f956389457b2af2e0711f3fa03 Mon Sep 17 00:00:00 2001 From: "C. Eric Mathey" Date: Fri, 11 Aug 2023 23:17:30 -0600 Subject: [PATCH 1/4] Add anyio and replace asyncio sleep --- .pre-commit-config.yaml | 2 +- discord_webhook/async_webhook.py | 4 ++-- pyproject.toml | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 774b9a8..45c69d6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: rev: 22.8.0 hooks: - id: black - language_version: python3.10 + language_version: python3 - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.0.272 hooks: diff --git a/discord_webhook/async_webhook.py b/discord_webhook/async_webhook.py index d66bc73..0b2b41a 100644 --- a/discord_webhook/async_webhook.py +++ b/discord_webhook/async_webhook.py @@ -1,4 +1,4 @@ -import asyncio +import anyio import json import logging from contextlib import asynccontextmanager @@ -90,7 +90,7 @@ async def handle_rate_limit(self, response, request) -> "httpx.Response": wh_sleep=round(wh_sleep, 2) ) ) - await asyncio.sleep(wh_sleep) + await anyio.sleep(wh_sleep) response = await request() if response.status_code in [200, 204]: return response diff --git a/pyproject.toml b/pyproject.toml index f6f7b1d..6b51acc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ keywords = ["discord", "webhook"] python = "^3.8" requests = "^2.28.1" httpx = { version = "^0.23.0", optional = true } +anyio = "^3.7.1" [tool.poetry.extras] async = ["httpx"] From 605f854cc546502778b3b12c725afb6346134d0b Mon Sep 17 00:00:00 2001 From: "C. Eric Mathey" Date: Fri, 11 Aug 2023 23:29:19 -0600 Subject: [PATCH 2/4] Update README to include both trio/asyncio examples of async usage --- README.md | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fcb1133..bbab1c1 100644 --- a/README.md +++ b/README.md @@ -349,25 +349,41 @@ In order to use the async version, you need to install the package using: ``` pip install discord-webhook[async] ``` +python-discord-webhook supports both asyncio and [trio](https://trio.readthedocs.io/en/stable/) through the use of [anyio](https://anyio.readthedocs.io/en/3.x/) + Example usage: ```python import asyncio -from discord_webhook import AsyncDiscordWebhook +import os + +import trio + +import discord_webhook -async def send_webhook(message): - webhook = AsyncDiscordWebhook(url="your webhook url", content=message) +async def send_webhook(message: str): + url = os.getenv("TEST_WEBHOOK_URL") + webhook = discord_webhook.AsyncDiscordWebhook(url=url, content=message) await webhook.execute() -async def main(): +async def trio_main(): + async with trio.open_nursery() as nursery: + nursery.start_soon(send_webhook, "Async webhook message 1") + nursery.start_soon(send_webhook, "Async webhook message 2") + + +async def asyncio_main(): await asyncio.gather( send_webhook("Async webhook message 1"), send_webhook("Async webhook message 2"), ) # sends both messages asynchronously -asyncio.run(main()) +if __name__ == "__main__": + trio.run(trio_main) + asyncio.run(asyncio_main()) + ``` ### Use CLI From 2e1971ea8ae4a6f88fc00cc9c6120de82f8c4da6 Mon Sep 17 00:00:00 2001 From: "C. Eric Mathey" Date: Fri, 11 Aug 2023 23:32:50 -0600 Subject: [PATCH 3/4] Match the previous async example in the REAMDE more closely --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bbab1c1..58d6a4f 100644 --- a/README.md +++ b/README.md @@ -362,8 +362,7 @@ import discord_webhook async def send_webhook(message: str): - url = os.getenv("TEST_WEBHOOK_URL") - webhook = discord_webhook.AsyncDiscordWebhook(url=url, content=message) + webhook = discord_webhook.AsyncDiscordWebhook(url="your webhook url", content=message) await webhook.execute() @@ -384,6 +383,7 @@ if __name__ == "__main__": trio.run(trio_main) asyncio.run(asyncio_main()) + ``` ### Use CLI From 8b7f01028aa140b9228b27df5da4b2ceb981c47a Mon Sep 17 00:00:00 2001 From: "C. Eric Mathey" <48801688+cemathey@users.noreply.github.com> Date: Fri, 11 Aug 2023 23:35:13 -0600 Subject: [PATCH 4/4] Update .pre-commit-config.yaml Restore language version --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 45c69d6..774b9a8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: rev: 22.8.0 hooks: - id: black - language_version: python3 + language_version: python3.10 - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.0.272 hooks: