8000 Cancelling a subscription · Issue #10 · Qluxzz/avanza · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cancelling a subscription #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
robiwano opened this issue Oct 13, 2020 · 5 comments
Open

Cancelling a subscription #10

robiwano opened this issue Oct 13, 2020 · 5 comments

Comments

@robiwano
Copy link

It seems it's not possible to cancel a avanza.subscription_id. The following code shows this. Idea is that task is created with ensure_future, and then cancelled after ten seconds. The subscription data continues to pour in after this.

import asyncio
from avanza import Avanza, ChannelType
import signal

user = ...
passw = ...
totpSecret = ...


def callback(data):
    # Do something with the quotes data here
    print(data)


async def subscribe_to_channel(avanza: Avanza):
    await avanza.subscribe_to_id(
        ChannelType.QUOTES,
        "19002",  # OMX Stockholm 30
        callback)


def cancel_request(unused_signum, unused_frame):
    asyncio.get_event_loop().stop()


async def cancel_task(task: asyncio.Task):
    await asyncio.sleep(10.0)
    print("Cancelling task " + task.get_name())
    task.cancel()


def main():
    signal.signal(signal.SIGINT, cancel_request)

    avanza = Avanza({
        'username': user,
        'password': passw,
        'totpSecret': totpSecret
    })

    task = asyncio.ensure_future(subscribe_to_channel(avanza))
    task.set_name("Subscription")

    asyncio.ensure_future(cancel_task(task))

    asyncio.get_event_loop().run_forever()
    print("Loop stopped")


if __name__ == "__main__":
    main()

@robiwano
Copy link
Author

Meanwhile I'll follow the suggestion to have a publish/subscribe proxy.

@ridulfo
Copy link
Contributor
ridulfo commented Jan 26, 2021

Hello @robiwano
Did you solve the problem, and if so how?
I saw that this library provides a function called _disconnect().

@robiwano
Copy link
Author

Unfortunately not.

@vonDassendorf
Copy link
vonDassendorf commented Mar 16, 2021

adding this function to the socket and then calling it from Avanza class with a wrapper method should do the trick:

async def __socket_unsubscribe(
        self,
        subscription_string
    ):
        await self.__send({
            'channel': '/meta/unsubscribe',
            'clientId': self._client_id,
            'subscription': subscription_string
        })

I haven't tested this yet but that is how fhqvist is doing it in his javascript wrapper.

@Qluxzz
Copy link
Owner
Qluxzz commented May 9, 2021

I'm not all that up to speed with async in Python and I haven't had time to really test it but I've created a draft with the functionality added if any of you could try it out and reiterate upon it if needed.

#30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
0