Skip to content
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

close session if HTTP response is not 200 or event is auth_revoked #10

Merged
merged 2 commits into from Nov 12, 2023

Conversation

dwatrous
Copy link
Contributor

These changes make it possible to respond to auth failures and auth expirations

aiosseclient.py Outdated
yield Event.parse(''.join(lines))
current_event = Event.parse(''.join(lines))
yield current_event
if current_event.event == "auth_revoked":
Copy link
Owner

@ebraminio ebraminio Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all thanks for the filing the issue and the patch, doesn't comparing with "auth_revoked" feels to be too specific? Maybe it's something that a client should do or maybe not. I'll apply the patch upon you response as I trust you given your expertise on the field. Thanks 😊

aiosseclient.py Outdated
@@ -20,6 +20,9 @@ async def aiosseclient(url, last_id=None, **kwargs):
sock_connect=None, sock_read=None)
async with aiohttp.ClientSession(timeout=timeout) as session:
response = await session.get(url, **kwargs)
if response.status != 200:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something other than 200 also can be OK? Perhaps all the statuses starting 2 and maybe handling 3 also?

@dwatrous
Copy link
Contributor Author

I wouldn't accept the PR as is. Based on the specification: https://html.spec.whatwg.org/multipage/server-sent-events.html,

Event stream requests can be redirected using HTTP 301 and 307 redirects as with normal HTTP requests. Clients will reconnect if the connection is closed; a client can be told to stop reconnecting using the HTTP 204 No Content response code.

What I do know is that a 401 should result in the session closing (and probably true for 4XX, 5XX and possibly others).

auth_revoked is an event type specified by Firebase Realtime Database, and probably isn't universal.

I'll have to think about how this could be modularized so that it would be easier to handle these cases for specific SSE implementations. For now I wanted to document that I had found a way to make this work for my case.

@ebraminio
Copy link
Owner

ebraminio commented Nov 12, 2023

Fantastic, thank you very much. I've invited you to the repository as a collaborator. You can merge this PR whenever you like or let me merge, or just leave it open as I understand the situation. Thanks 😊

parameterize valid http response codes and exit worthy events. This should not require existing users to change their code
@dwatrous
Copy link
Contributor Author

I updated this to be parameterized with sensible defaults. Let me know what you think

@ebraminio ebraminio merged commit f1ebf19 into ebraminio:main Nov 12, 2023
@ebraminio
Copy link
Owner

Just made a release containing the changes https://pypi.org/manage/project/aiosseclient/releases/ using GitHub Actions CI, feel free to test and let me know if anything missing. It's even fine if you want to introduce a breaking API change to the just added API so feel free to test and tweak whatever you like or not. Thanks 😊

@ebraminio
Copy link
Owner

ebraminio commented Nov 17, 2023

Also thanks to your work I've discovered https://github.com/rtfol/aiohttp-sse-client and https://github.com/JelleZijlstra/aiohttp-sse-client2 which started with inspiration from this project and I believe we can learn from their experiences now.

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

Successfully merging this pull request may close these issues.

None yet

2 participants