From f4698aa4dc339c14325aede556b162c8c1f539bd Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Thu, 29 Dec 2022 07:27:35 -0500 Subject: [PATCH] fix RuntimeError when running commands involving http client --- relay/http_client.py | 24 ++++++++++++++++++++++++ relay/manage.py | 13 +++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/relay/http_client.py b/relay/http_client.py index 9f28760..81fcd46 100644 --- a/relay/http_client.py +++ b/relay/http_client.py @@ -38,6 +38,15 @@ class HttpClient: self._session = None + async def __aenter__(self): + await self.open() + return self + + + async def __aexit__(self, *_): + await self.close() + + @property def limit(self): return self.cfg['limit'] @@ -196,3 +205,18 @@ class HttpClient: return False return await self.get(nodeinfo_url, loads=Nodeinfo.new_from_json) or False + + +async def get(database, *args, **kwargs): + async with HttpClient(database) as client: + return await client.get(*args, **kwargs) + + +async def post(database, *args, **kwargs): + async with HttpClient(database) as client: + return await client.post(*args, **kwargs) + + +async def fetch_nodeinfo(database, *args, **kwargs): + async with HttpClient(database) as client: + return await client.fetch_nodeinfo(*args, **kwargs) diff --git a/relay/manage.py b/relay/manage.py index 0d7decc..c36f876 100644 --- a/relay/manage.py +++ b/relay/manage.py @@ -7,6 +7,7 @@ import platform from urllib.parse import urlparse from . import misc, __version__ +from . import http_client as http from .application import Application from .config import RELAY_SOFTWARE @@ -148,7 +149,7 @@ def cli_inbox_follow(actor): inbox = inbox_data['inbox'] except KeyError: - actor_data = asyncio.run(app.client.get(actor, sign_headers=True)) + actor_data = asyncio.run(http.get(app.database, actor, sign_headers=True)) if not actor_data: return click.echo(f'Failed to fetch actor: {actor}') @@ -160,7 +161,7 @@ def cli_inbox_follow(actor): actor = actor ) - asyncio.run(app.client.post(inbox, message)) + asyncio.run(http.post(app.database, inbox, message)) click.echo(f'Sent follow message to actor: {actor}') @@ -186,7 +187,7 @@ def cli_inbox_unfollow(actor): ) except KeyError: - actor_data = asyncio.run(app.client.get(actor, sign_headers=True)) + actor_data = asyncio.run(http.get(app.database, actor, sign_headers=True)) inbox = actor_data.shared_inbox message = misc.Message.new_unfollow( host = app.config.host, @@ -198,7 +199,7 @@ def cli_inbox_unfollow(actor): } ) - asyncio.run(app.client.post(inbox, message)) + asyncio.run(http.post(app.database, inbox, message)) click.echo(f'Sent unfollow message to: {actor}') @@ -322,7 +323,7 @@ def cli_software_ban(name, fetch_nodeinfo): return click.echo('Banned all relay software') if fetch_nodeinfo: - nodeinfo = asyncio.run(app.client.fetch_nodeinfo(name)) + nodeinfo = asyncio.run(http.fetch_nodeinfo(app.database, name)) if not nodeinfo: click.echo(f'Failed to fetch software name from domain: {name}') @@ -352,7 +353,7 @@ def cli_software_unban(name, fetch_nodeinfo): return click.echo('Unbanned all relay software') if fetch_nodeinfo: - nodeinfo = asyncio.run(app.client.fetch_nodeinfo(name)) + nodeinfo = asyncio.run(http.fetch_nodeinfo(app.database, name)) if not nodeinfo: click.echo(f'Failed to fetch software name from domain: {name}')