use in-built aiohttp client instead of requests

This commit is contained in:
William Kray 2020-12-22 15:36:58 -08:00
parent 6a52e7b96f
commit f38eeeeb2f
2 changed files with 13 additions and 10 deletions

View File

@ -4,7 +4,6 @@ from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
from maubot import Plugin, MessageEvent from maubot import Plugin, MessageEvent
from maubot.handlers import command from maubot.handlers import command
import requests
import json import json
import datetime import datetime
@ -52,16 +51,17 @@ class Invite(Plugin):
} }
try: try:
response = requests.request("POST", f"{self.config['reg_url']}/token", headers=headers, \ response = await self.http.post(f"{self.config['reg_url']}/token", headers=headers, \
json={"one_time": True, "ex_date": ex_date}) json={"one_time": True, "ex_date": ex_date})
resp_json = await response.json()
except Exception as e: except Exception as e:
await evt.respond(f"request failed: {e.message}") await evt.respond(f"request failed: {e.message}")
return None return None
try: try:
token = response.json()['name'] token = resp_json['name']
except Exception as e: except Exception as e:
await evt.respond(f"I got a bad response back, sorry, something is borked. \n\ await evt.respond(f"I got a bad response back, sorry, something is borked. \n\
{response.json()}") {resp_json}")
self.log.exception(e) self.log.exception(e)
return None return None
@ -93,13 +93,14 @@ class Invite(Plugin):
} }
try: try:
response = requests.request("GET", f"{self.config['reg_url']}/token/{token}", headers=headers) response = await self.http.get(f"{self.config['reg_url']}/token/{token}", headers=headers)
resp_json = await response.json()
except Exception as e: except Exception as e:
await evt.respond(f"request failed: {e.message}") await evt.respond(f"request failed: {e.message}")
return None return None
# this isn't formatted nicely but i don't really care that much # this isn't formatted nicely but i don't really care that much
await evt.respond(f"Status of token {token}: {json.dumps(response.json(), indent=4)}") await evt.respond(f"Status of token {token}: {json.dumps(resp_json, indent=4)}")
@invite.subcommand("revoke", help="Disable an existing invite token.") @invite.subcommand("revoke", help="Disable an existing invite token.")
@command.argument("token", "Token", pass_raw=True, required=True) @command.argument("token", "Token", pass_raw=True, required=True)
@ -118,14 +119,15 @@ class Invite(Plugin):
} }
try: try:
response = requests.request("PUT", f"{self.config['reg_url']}/token/{token}", headers=headers, \ response = await self.http.put(f"{self.config['reg_url']}/token/{token}", headers=headers, \
json={"disable": True}) json={"disable": True})
resp_json = await response.json()
except Exception as e: except Exception as e:
await evt.respond(f"request failed: {e.message}") await evt.respond(f"request failed: {e.message}")
return None return None
# this isn't formatted nicely but i don't really care that much # this isn't formatted nicely but i don't really care that much
await evt.respond(f"{json.dumps(response.json(), indent=4)}") await evt.respond(f"{json.dumps(resp_json, indent=4)}")
@invite.subcommand("list", help="List all tokens that have been generated.") @invite.subcommand("list", help="List all tokens that have been generated.")
async def list(self, evt: MessageEvent) -> None: async def list(self, evt: MessageEvent) -> None:
@ -139,7 +141,8 @@ class Invite(Plugin):
} }
try: try:
response = requests.request("GET", f"{self.config['reg_url']}/token", headers=headers) response = await self.http.get(f"{self.config['reg_url']}/token", headers=headers)
resp_json = await response.json()
except Exception as e: except Exception as e:
await evt.respond(f"request failed: {e.message}") await evt.respond(f"request failed: {e.message}")
return None return None

View File

@ -1,6 +1,6 @@
maubot: 0.1.0 maubot: 0.1.0
id: org.jobmachine.invitebot id: org.jobmachine.invitebot
version: 0.0.2 version: 0.0.3
license: MIT license: MIT
modules: modules:
- invite - invite