Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
'fastapi-users[beanie]',
'pydantic-settings',
'uvicorn[standard]',
'sse-starlette'
]

[project.scripts]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ fastapi-users[beanie]==12.1.2
fastapi==0.103.2
pydantic-settings==2.0.3
uvicorn[standard]==0.23.2
sse-starlette==1.6.5
2 changes: 2 additions & 0 deletions src/unipoll_api/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .v2 import router as v2_router
from .swagger_docs import create_doc_router
from .websocket import router as websocket_router
from .streams import router as streams_router


# Function to generate unique operation IDs for different API versions
Expand All @@ -28,6 +29,7 @@ def create_router(app, default_version):
# Default API version
router.include_router(endpoints[default_version])
router.include_router(websocket_router, prefix="/ws", tags=["WebSocket"])
router.include_router(streams_router, tags=["Streams"])

# Add API v1 endpoints to the main router
router.include_router(v1_router,
Expand Down
13 changes: 13 additions & 0 deletions src/unipoll_api/routes/streams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fastapi import APIRouter
from sse_starlette.sse import EventSourceResponse
from unipoll_api.utils.streams import update_generator


router = APIRouter()


@router.get("/updates",
response_class=EventSourceResponse)
async def event_log():
updates = update_generator()
return EventSourceResponse(updates)
16 changes: 16 additions & 0 deletions src/unipoll_api/utils/streams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import asyncio
from unipoll_api.documents import Resource
from . import colored_dbg as Debug


# async def update_generator(resource: Resource):
async def update_generator():
i = 0
try:
while True:
i += 1
Comment on lines +6 to +11
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

This commented-out code should be removed if it's not needed, or properly implemented if it represents the intended functionality with Resource parameter.

Suggested change
# 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

Copilot uses AI. Check for mistakes.
yield dict(data=i)
await asyncio.sleep(0.2)
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

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.

Suggested change
await asyncio.sleep(0.2)
await asyncio.sleep(SLEEP_DURATION)

Copilot uses AI. Check for mistakes.
except asyncio.CancelledError as e:
Debug.info("Disconnected from client (via refresh/close)")
raise e
Loading