Conversation
There was a problem hiding this comment.
Pull Request Overview
This pull request introduces server-sent events (SSE) functionality to enable real-time updates via a new /updates endpoint. The implementation adds SSE support through a new dependency and creates the necessary routing and utility components.
- Added
sse-starlettedependency for SSE functionality - Created a new
/updatesendpoint with EventSourceResponse for real-time streaming - Implemented a basic update generator utility that yields incremental data
Reviewed Changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pyproject.toml | Adds sse-starlette dependency for SSE support |
| src/unipoll_api/utils/streams.py | Implements update generator utility for SSE data |
| src/unipoll_api/routes/streams.py | Creates new streams route with /updates endpoint |
| src/unipoll_api/routes/init.py | Integrates streams router into main router configuration |
Comments suppressed due to low confidence (2)
src/unipoll_api/utils/streams.py:8
- The variable name 'i' is too generic for a counter variable. Consider using a more descriptive name like 'counter' or 'update_count'.
i = 0
src/unipoll_api/routes/streams.py:11
- The function name 'event_log' doesn't clearly describe its purpose of streaming updates. Consider renaming to 'stream_updates' or 'get_updates_stream'.
async def event_log():
| while True: | ||
| i += 1 | ||
| yield dict(data=i) | ||
| await asyncio.sleep(0.2) |
There was a problem hiding this comment.
The sleep duration of 0.2 seconds is a magic number. Consider defining this as a named constant to improve maintainability and make it configurable.
| await asyncio.sleep(0.2) | |
| await asyncio.sleep(SLEEP_DURATION) |
| # async def update_generator(resource: Resource): | ||
| async def update_generator(): | ||
| i = 0 | ||
| try: | ||
| while True: | ||
| i += 1 |
There was a problem hiding this comment.
This commented-out code should be removed if it's not needed, or properly implemented if it represents the intended functionality with Resource parameter.
| # async def update_generator(resource: Resource): | |
| async def update_generator(): | |
| i = 0 | |
| try: | |
| while True: | |
| i += 1 | |
| async def update_generator(resource: Resource): | |
| """ | |
| A generator function that yields data and interacts with a Resource object. | |
| Args: | |
| resource (Resource): The resource object to interact with. | |
| """ | |
| i = 0 | |
| try: | |
| while True: | |
| i += 1 | |
| # Example interaction with the resource object | |
| resource.update_last_accessed() # Placeholder for actual logic |
This pull request introduces server-sent events (SSE) functionality to the project, enabling real-time updates via a new
/updatesendpoint. The changes include adding a new dependency, creating a new route and utility for handling SSE, and integrating the new route into the existing router setup.SSE Functionality Implementation:
sse-starlettedependency to thepyproject.tomlfile to support server-sent events. (pyproject.toml, pyproject.tomlR24)streamsroute insrc/unipoll_api/routes/streams.py, which defines an SSE endpoint (/updates) usingEventSourceResponse. (src/unipoll_api/routes/streams.py, src/unipoll_api/routes/streams.pyR1-R13)update_generatorutility insrc/unipoll_api/utils/streams.pyto generate real-time updates for the SSE endpoint. (src/unipoll_api/utils/streams.py, src/unipoll_api/utils/streams.pyR1-R16)Router Integration:
streamsroute into the router by importing it insrc/unipoll_api/routes/__init__.pyand including it in the main router with the tag "Streams". (src/unipoll_api/routes/__init__.py, [1] [2]