import asyncio
import dhooks
import aiohttp
import selectors
async def main():
hook = dhooks.Webhook("url", is_async=True)
await hook.send('hello')
await hook.modify('bob')
await hook.get_info()
await hook.delete() # since we await it, we have to return a corutine or simular, but method delete will return None
await hook.close() # close the client session
# monkeypatch for windows machine
selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
Error:
C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\dhooks\client.py:323: RuntimeWarning: coroutine 'Webhook._async_request' was never awaited
self._request(method='DELETE')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/-/-/-/-/dev/async_error.py", line 20, in <module>
loop.run_until_complete(main())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:/Users/Administrator/PycharmProjects/-/-/-/-/dev/async_error.py", line 12, in main
await hook.delete()
TypeError: object NoneType can't be used in 'await' expression
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000020B56C3BD30>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x0000020B56C4E1C0>, 1322769.828)]']
connector: <aiohttp.connector.TCPConnector object at 0x0000020B56C3BD60>
Fix:
# dhooks/client.py
# line 318 -323
def delete(self) -> 'Webhook':
"""
Deletes the :class:`Webhook` permanently.
"""
return self._request(method='DELETE')
# and since we return now the webhook object again it would be good to delete it in some way with an if statement in the _request method after it was executed, (if method == "DELETE: )
My test code:
Error:
Fix: