chore: stylize sse code; fix: add sseclient-py to requirements

This commit is contained in:
firedotguy
2026-02-10 17:53:48 +03:00
parent 51518ce0d7
commit 8e8b0b3bb9
5 changed files with 30 additions and 45 deletions

View File

@@ -2,8 +2,8 @@ from uuid import UUID
from _io import BufferedReader from _io import BufferedReader
from typing import cast, Iterator from typing import cast, Iterator
from datetime import datetime from datetime import datetime
import json from json import JSONDecodeError, loads
import time from time import sleep
from requests.exceptions import ConnectionError, HTTPError from requests.exceptions import ConnectionError, HTTPError
from sseclient import SSEClient from sseclient import SSEClient
@@ -1144,14 +1144,14 @@ class Client:
if not event.data or event.data.strip() == '': if not event.data or event.data.strip() == '':
continue continue
data = json.loads(event.data) data = loads(event.data)
if 'userId' in data and 'timestamp' in data and 'type' not in data: if 'userId' in data and 'timestamp' in data and 'type' not in data:
yield StreamConnect.model_validate(data) yield StreamConnect.model_validate(data)
else: else:
yield StreamNotification.model_validate(data) yield StreamNotification.model_validate(data)
except json.JSONDecodeError: except JSONDecodeError:
print(f'Не удалось распарсить сообщение: {event.data}') print(f'Не удалось распарсить сообщение: {event.data}')
continue continue
except Exception as e: except Exception as e:
@@ -1169,7 +1169,7 @@ class Client:
if not self._stream_active: if not self._stream_active:
return return
print(f'Ошибка соединения: {e}, переподключение через 5 секунд...') print(f'Ошибка соединения: {e}, переподключение через 5 секунд...')
time.sleep(5) sleep(5)
continue continue
def stop_stream(self): def stop_stream(self):
@@ -1197,4 +1197,5 @@ class Client:
thread.join() thread.join()
``` ```
""" """
print('stop event')
self._stream_active = False self._stream_active = False

View File

@@ -1,11 +1,8 @@
from uuid import UUID from uuid import UUID
from datetime import datetime
from typing import Literal
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from itd.enums import NotificationType, NotificationTargetType from itd.models.notification import Notification
from itd.models.user import UserNotification
class StreamConnect(BaseModel): class StreamConnect(BaseModel):
@@ -14,20 +11,7 @@ class StreamConnect(BaseModel):
timestamp: int timestamp: int
class StreamNotification(BaseModel): class StreamNotification(Notification):
"""Уведомление из SSE потока""" """Уведомление из SSE потока"""
id: UUID
type: NotificationType
target_type: NotificationTargetType | None = Field(None, alias='targetType')
target_id: UUID | None = Field(None, alias='targetId')
preview: str | None = None
read_at: datetime | None = Field(None, alias='readAt')
created_at: datetime = Field(alias='createdAt')
user_id: UUID = Field(alias='userId') user_id: UUID = Field(alias='userId')
actor: UserNotification
read: bool = False
sound: bool = True sound: bool = True

View File

@@ -12,6 +12,6 @@ authors = [
] ]
license = "MIT" license = "MIT"
dependencies = [ dependencies = [
"requests", "pydantic" "requests", "pydantic", "sseclient-py"
] ]
requires-python = ">=3.9" requires-python = ">=3.9"

View File

@@ -5,7 +5,7 @@ setup(
version='1.1.0', version='1.1.0',
packages=find_packages(), packages=find_packages(),
install_requires=[ install_requires=[
'requests', 'pydantic' 'requests', 'pydantic', 'sseclient-py'
], ],
python_requires=">=3.9" python_requires=">=3.9"
) )