From 0edbe40e4a980ce1c92be1a8ba68e276dbcbd3f0 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 21 Aug 2022 22:15:05 +0100 Subject: Move to async cog loading This is required as of Discord.py 2.0 --- arthur/exts/cloudflare/zones.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'arthur/exts/cloudflare/zones.py') diff --git a/arthur/exts/cloudflare/zones.py b/arthur/exts/cloudflare/zones.py index 7f9ca35..f265651 100644 --- a/arthur/exts/cloudflare/zones.py +++ b/arthur/exts/cloudflare/zones.py @@ -75,6 +75,6 @@ class Zones(commands.Cog): await ctx.send(":cloud: Pick which zone(s) that should have their cache purged", view=view) -def setup(bot: KingArthur) -> None: +async def setup(bot: KingArthur) -> None: """Add the extension to the bot.""" - bot.add_cog(Zones(bot)) + await bot.add_cog(Zones(bot)) -- cgit v1.2.3 From 083ceebfadfbf88752fb1b50bf0c86feff94875e Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 21 Aug 2022 22:16:26 +0100 Subject: Update ordering of Interaction callbacks The order the args get passed to callbacks has been changed in Discord.py 2.0. --- arthur/exts/cloudflare/zones.py | 2 +- arthur/exts/kubernetes/deployments.py | 4 ++-- arthur/exts/kubernetes/jobs.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'arthur/exts/cloudflare/zones.py') diff --git a/arthur/exts/cloudflare/zones.py b/arthur/exts/cloudflare/zones.py index f265651..657de71 100644 --- a/arthur/exts/cloudflare/zones.py +++ b/arthur/exts/cloudflare/zones.py @@ -31,7 +31,7 @@ class ZonesView(discord.ui.View): placeholder="Select a zone to purge...", ) async def select_zones( - self, dropdown: discord.ui.Select, interaction: discord.Interaction + self, interaction: discord.Interaction, dropdown: discord.ui.Select ) -> None: """Drop down menu contains the list of zones.""" zone_name = dropdown.values[0] diff --git a/arthur/exts/kubernetes/deployments.py b/arthur/exts/kubernetes/deployments.py index a55dc5f..7a69baf 100644 --- a/arthur/exts/kubernetes/deployments.py +++ b/arthur/exts/kubernetes/deployments.py @@ -36,7 +36,7 @@ class ConfirmDeployment(ui.View): return False @ui.button(label="Confirm", style=ButtonStyle.green, row=0) - async def confirm(self, _button: ui.Button, interaction: Interaction) -> None: + async def confirm(self, interaction: Interaction, _button: ui.Button) -> None: """Redeploy the specified service.""" try: await deployments.restart_deployment(self.deployment, self.namespace) @@ -66,7 +66,7 @@ class ConfirmDeployment(ui.View): self.stop() @ui.button(label="Cancel", style=ButtonStyle.grey, row=0) - async def cancel(self, _button: ui.Button, interaction: Interaction) -> None: + async def cancel(self, interaction: Interaction, _button: ui.Button) -> None: """Logic for if the deployment is not approved.""" await interaction.message.edit( content=":x: Redeployment aborted", diff --git a/arthur/exts/kubernetes/jobs.py b/arthur/exts/kubernetes/jobs.py index 2a0e147..6259f65 100644 --- a/arthur/exts/kubernetes/jobs.py +++ b/arthur/exts/kubernetes/jobs.py @@ -37,7 +37,7 @@ class CronJobView(discord.ui.View): placeholder="Select a CronJob to trigger...", ) async def select_job( - self, dropdown: discord.ui.Select, interaction: discord.Interaction + self, interaction: discord.Interaction, dropdown: discord.ui.Select ) -> None: """Drop down menu contains the list of cronjobsb.""" cronjob_namespace, cronjob_name = dropdown.values[0].split("/") -- cgit v1.2.3 From 850928ae382b33698cda07c3612b8cc6ee0d134b Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 21 Aug 2022 22:16:59 +0100 Subject: Use the bot's http_session when calling cloudflare's API --- arthur/apis/cloudflare/zones.py | 20 ++++++++++++-------- arthur/exts/cloudflare/zones.py | 10 ++++++---- 2 files changed, 18 insertions(+), 12 deletions(-) (limited to 'arthur/exts/cloudflare/zones.py') diff --git a/arthur/apis/cloudflare/zones.py b/arthur/apis/cloudflare/zones.py index 81a54f1..7d407a0 100644 --- a/arthur/apis/cloudflare/zones.py +++ b/arthur/apis/cloudflare/zones.py @@ -8,30 +8,34 @@ from arthur.config import CONFIG AUTH_HEADER = {"Authorization": f"Bearer {CONFIG.cloudflare_token}"} -async def list_zones(zone_name: Optional[str] = None) -> dict[str, str]: +async def list_zones( + session: aiohttp.ClientSession, + zone_name: Optional[str] = None, +) -> dict[str, str]: """List all Cloudflare zones.""" endpoint = "https://api.cloudflare.com/client/v4/zones" if zone_name is not None: endpoint += f"?name={zone_name}" - async with aiohttp.ClientSession() as session: - async with session.get(endpoint, headers=AUTH_HEADER) as response: - info = await response.json() + async with session.get(endpoint, headers=AUTH_HEADER) as response: + info = await response.json() zones = info["result"] return {zone["name"]: zone["id"] for zone in zones} -async def purge_zone(zone_identifier: str) -> dict: +async def purge_zone( + session: aiohttp.ClientSession, + zone_identifier: str, +) -> dict: """Purge the cache for a Cloudflare zone.""" endpoint = f"https://api.cloudflare.com/client/v4/zones/{zone_identifier}/purge_cache" request_body = {"purge_everything": True} - async with aiohttp.ClientSession() as session: - async with session.post(endpoint, headers=AUTH_HEADER, json=request_body) as response: - info = await response.json() + async with session.post(endpoint, headers=AUTH_HEADER, json=request_body) as response: + info = await response.json() return {"success": info["success"], "errors": info["errors"]} diff --git a/arthur/exts/cloudflare/zones.py b/arthur/exts/cloudflare/zones.py index 657de71..5840d36 100644 --- a/arthur/exts/cloudflare/zones.py +++ b/arthur/exts/cloudflare/zones.py @@ -1,4 +1,5 @@ """The zones cog helps with managing Cloudflare zones.""" +import aiohttp import discord from discord.ext import commands @@ -11,10 +12,11 @@ from arthur.utils import generate_error_message class ZonesView(discord.ui.View): """This view allows users to select and purge the zones specified.""" - def __init__(self, domains: dict[str, str]) -> None: + def __init__(self, domains: dict[str, str], session: aiohttp.ClientSession) -> None: super().__init__() self.domains = domains + self.session = session for domain, zone_id in self.domains.items(): self.children[0].add_option(label=domain, value=domain, description=zone_id, emoji="🌐") @@ -37,7 +39,7 @@ class ZonesView(discord.ui.View): zone_name = dropdown.values[0] required_id = self.domains[zone_name] - purge_attempt_response = await zones.purge_zone(required_id) + purge_attempt_response = await zones.purge_zone(self.session, required_id) if purge_attempt_response["success"]: message = ":white_check_mark:" message += " **Cache cleared!** " @@ -69,9 +71,9 @@ class Zones(commands.Cog): @zones.command(name="purge") async def purge(self, ctx: commands.Context) -> None: """Command to clear the Cloudflare cache of the specified zone.""" - cf_zones = await zones.list_zones() + cf_zones = await zones.list_zones(self.bot.http_session) - view = ZonesView(cf_zones) + view = ZonesView(cf_zones, self.bot.http_session) await ctx.send(":cloud: Pick which zone(s) that should have their cache purged", view=view) -- cgit v1.2.3