ebraminio/aiosseclient

How to handle 401 and expired credentials

Closed this issue · 3 comments

I am operating against the Firebase Realtime Database.

I have the following code

import threading
from time import sleep

url = "https://databaseurl/messages.json?auth=idtoken"

async def message_reader():
    async for event in aiosseclient(url):
        print(event.event)
        print(event)

message_reader_thread = threading.Thread(target=asyncio.run, args=(message_reader(),))
message_reader_thread.start()

while True:
    print("Still reading messages")
    sleep(30)

This code works, but breaks in two cases that I can't understand how to handle. First is when the idtoken is bad and response gets a 401 back. The While loop still runs, but there is no error or way to exit the for loop. The second is when idtoken expires (they only last an hour) and an event "auth_revoked" is received. Again, there is no way to observe the error, and the for loop keeps going because it is still getting "keep-alive" events, even though it's not receiving data anymore.

I need some way to have the script fail and exit on 401 and I need a way to somehow refresh/restart it with a new idtoken periodically (hourly in this case).

See #10 for my solution

I wanted to answer this yesterday but somehow have missed that, thanks for sending the PR, I've put some comments there. Thanks 😊

#10 is merged so closing this, thanks 😊