Qluxzz / avanza

A Python library for the unofficial Avanza API
https://qluxzz.github.io/avanza/
MIT License
85 stars 40 forks source link

Nice-to-have: websocket receiver task should be returned/exposed by API to allow exception handling #64

Open svennystrom opened 1 year ago

svennystrom commented 1 year ago

If there is a network issue an exception is thrown by the websocket library; for code consuming the API it would be convenient to get the task created by AvanzaSocket::init() to receive the socket data - making it possible to wait for and handle possible exceptions together with other tasks (using for example asyncio.gather()), as well as being able to cancel it for a controlled shutdown. Since the init() method does not return the result of asyncio.ensure_future(self.__create_socket()) this requires some extra fiddling in the consuming code, making it sensitive to changes in the API.

async def init(self):
    asyncio.ensure_future(self.__create_socket())             # nice if the result of this could be returned at the end of the method
    await self.__wait_for_websocket_to_be_connected()

Ideally the actual call to AvanzaSocket::init() should also be wrapped by the Avanza class allowing access without having to go through the _socket member, as - say - Avanza::init_socket() --> return await self._socket.init()

svennystrom commented 1 year ago

And a big Thank You for publishing your work here!

00prometheus commented 1 year ago

I found a work-around for this, but it isn't pretty: https://github.com/Qluxzz/avanza/issues/86