fastlib-py 0.3.1__tar.gz → 0.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/PKG-INFO +1 -1
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/_server_config.py +2 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/middleware/i18n.py +40 -40
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/stream/enums.py +36 -36
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/stream/event.py +101 -101
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/stream/handler.py +187 -187
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/stream/schema.py +140 -140
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/stream/sse.py +272 -272
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib_py.egg-info/PKG-INFO +1 -1
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/pyproject.toml +1 -1
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/LICENSE +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/README.md +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/cache/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/cache/_disk_cache.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/cache/_redis_cache.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/cache/base.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/cache/manager.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/_database_config.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/_security_config.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/base.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/loader.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/manager.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/registry.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/config/utils.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/constants.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/contextvars.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/db_engine.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/enums.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/exception/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/exception/base.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/exception/exception_handler.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/exception/exception_manager.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/i18n/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/i18n/manager.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/i18n/messages.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/i18n/types.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/i18n/utils.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/logging/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/logging/config.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/logging/handlers.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/mapper/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/mapper/base_mapper.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/mapper/impl/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/mapper/impl/base_mapper_impl.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/middleware/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/middleware/db_session.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/middleware/jwt.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/migrate.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/openapi.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/py.typed +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/request.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/response.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/router.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/schema.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/security/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/security/security.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/security/utils.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/service/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/service/base_service.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/service/impl/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/service/impl/base_service_impl.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/stream/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/utils/__init__.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/utils/excel_util.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/utils/file_util.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/utils/http_util.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/utils/ip_util.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/utils/snowflake_util.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib/utils/str_util.py +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib_py.egg-info/SOURCES.txt +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib_py.egg-info/dependency_links.txt +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib_py.egg-info/requires.txt +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/fastlib_py.egg-info/top_level.txt +0 -0
- {fastlib_py-0.3.1 → fastlib_py-0.3.2}/setup.cfg +0 -0
|
@@ -19,6 +19,7 @@ class ServerConfig(BaseConfig):
|
|
|
19
19
|
name: The server name.
|
|
20
20
|
version: The server version.
|
|
21
21
|
app_desc: The server description.
|
|
22
|
+
enable_api_prefix: Enable the API prefix for routes.
|
|
22
23
|
api_prefix: The API prefix for routes.
|
|
23
24
|
debug: Whether to enable debug mode.
|
|
24
25
|
workers: The number of worker processes.
|
|
@@ -35,6 +36,7 @@ class ServerConfig(BaseConfig):
|
|
|
35
36
|
app_desc: str = field(
|
|
36
37
|
default_factory=lambda: ServerConfig._project_config.description
|
|
37
38
|
)
|
|
39
|
+
enable_api_prefix: bool = False
|
|
38
40
|
api_prefix: str = "/api"
|
|
39
41
|
debug: bool = False
|
|
40
42
|
workers: int = 1
|
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
|
-
from starlette.middleware.base import BaseHTTPMiddleware
|
|
3
|
-
|
|
4
|
-
from fastlib.i18n.manager import reset_language, set_language
|
|
5
|
-
from fastlib.i18n.types import Language
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class I18nMiddleware(BaseHTTPMiddleware):
|
|
9
|
-
"""
|
|
10
|
-
Middleware for automatic language detection and setting based on HTTP headers.
|
|
11
|
-
"""
|
|
12
|
-
|
|
13
|
-
async def dispatch(self, request, call_next):
|
|
14
|
-
"""
|
|
15
|
-
Process the request and set the language based on Accept-Language header.
|
|
16
|
-
|
|
17
|
-
Args:
|
|
18
|
-
request: The incoming HTTP request
|
|
19
|
-
call_next: The next middleware or route handler in the chain
|
|
20
|
-
|
|
21
|
-
Returns:
|
|
22
|
-
The HTTP response from the next handler
|
|
23
|
-
"""
|
|
24
|
-
# Get language preference from request headers
|
|
25
|
-
accept_lang = request.headers.get("Accept-Language", "en")
|
|
26
|
-
|
|
27
|
-
# Set the language for current request
|
|
28
|
-
if accept_lang.startswith("en"):
|
|
29
|
-
language = Language.CHINESE
|
|
30
|
-
elif accept_lang.startswith("zh"):
|
|
31
|
-
language = Language.CHINESE
|
|
32
|
-
|
|
33
|
-
set_language(language)
|
|
34
|
-
try:
|
|
35
|
-
response = await call_next(request)
|
|
36
|
-
finally:
|
|
37
|
-
# Always reset to prevent leaking context between requests
|
|
38
|
-
reset_language()
|
|
39
|
-
|
|
40
|
-
return response
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
3
|
+
|
|
4
|
+
from fastlib.i18n.manager import reset_language, set_language
|
|
5
|
+
from fastlib.i18n.types import Language
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class I18nMiddleware(BaseHTTPMiddleware):
|
|
9
|
+
"""
|
|
10
|
+
Middleware for automatic language detection and setting based on HTTP headers.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
async def dispatch(self, request, call_next):
|
|
14
|
+
"""
|
|
15
|
+
Process the request and set the language based on Accept-Language header.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
request: The incoming HTTP request
|
|
19
|
+
call_next: The next middleware or route handler in the chain
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
The HTTP response from the next handler
|
|
23
|
+
"""
|
|
24
|
+
# Get language preference from request headers
|
|
25
|
+
accept_lang = request.headers.get("Accept-Language", "en")
|
|
26
|
+
|
|
27
|
+
# Set the language for current request
|
|
28
|
+
if accept_lang.startswith("en"):
|
|
29
|
+
language = Language.CHINESE
|
|
30
|
+
elif accept_lang.startswith("zh"):
|
|
31
|
+
language = Language.CHINESE
|
|
32
|
+
|
|
33
|
+
set_language(language)
|
|
34
|
+
try:
|
|
35
|
+
response = await call_next(request)
|
|
36
|
+
finally:
|
|
37
|
+
# Always reset to prevent leaking context between requests
|
|
38
|
+
reset_language()
|
|
39
|
+
|
|
40
|
+
return response
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
|
-
"""
|
|
3
|
-
Enums for stream module
|
|
4
|
-
"""
|
|
5
|
-
|
|
6
|
-
from enum import Enum
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class MessageRole(str, Enum):
|
|
10
|
-
"""Message role enumeration"""
|
|
11
|
-
|
|
12
|
-
SYSTEM = "system"
|
|
13
|
-
USER = "user"
|
|
14
|
-
ASSISTANT = "assistant"
|
|
15
|
-
FUNCTION = "function"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class MessageStatus(str, Enum):
|
|
19
|
-
"""Message status enumeration"""
|
|
20
|
-
|
|
21
|
-
CREATED = "created"
|
|
22
|
-
RUNNING = "running"
|
|
23
|
-
COMPLETED = "completed"
|
|
24
|
-
FAILED = "failed"
|
|
25
|
-
CANCELLED = "cancelled"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class StreamEventType(str, Enum):
|
|
29
|
-
"""Stream event type"""
|
|
30
|
-
|
|
31
|
-
START = "start"
|
|
32
|
-
CONTENT = "content"
|
|
33
|
-
FUNCTION_CALL = "function_call"
|
|
34
|
-
ERROR = "error"
|
|
35
|
-
END = "end"
|
|
36
|
-
METADATA = "metadata"
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Enums for stream module
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from enum import Enum
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MessageRole(str, Enum):
|
|
10
|
+
"""Message role enumeration"""
|
|
11
|
+
|
|
12
|
+
SYSTEM = "system"
|
|
13
|
+
USER = "user"
|
|
14
|
+
ASSISTANT = "assistant"
|
|
15
|
+
FUNCTION = "function"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MessageStatus(str, Enum):
|
|
19
|
+
"""Message status enumeration"""
|
|
20
|
+
|
|
21
|
+
CREATED = "created"
|
|
22
|
+
RUNNING = "running"
|
|
23
|
+
COMPLETED = "completed"
|
|
24
|
+
FAILED = "failed"
|
|
25
|
+
CANCELLED = "cancelled"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class StreamEventType(str, Enum):
|
|
29
|
+
"""Stream event type"""
|
|
30
|
+
|
|
31
|
+
START = "start"
|
|
32
|
+
CONTENT = "content"
|
|
33
|
+
FUNCTION_CALL = "function_call"
|
|
34
|
+
ERROR = "error"
|
|
35
|
+
END = "end"
|
|
36
|
+
METADATA = "metadata"
|
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
# SPDX-License-Identifier: MIT
|
|
2
|
-
"""
|
|
3
|
-
Copy from https://github.com/sysid/sse-starlette/blob/main/sse_starlette/event.py
|
|
4
|
-
"""
|
|
5
|
-
|
|
6
|
-
import io
|
|
7
|
-
import json
|
|
8
|
-
import re
|
|
9
|
-
from typing import Any, Optional, Union
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class ServerSentEvent:
|
|
13
|
-
"""
|
|
14
|
-
Helper class to format data for Server-Sent Events (SSE).
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
_LINE_SEP_EXPR = re.compile(r"\r\n|\r|\n")
|
|
18
|
-
DEFAULT_SEPARATOR = "\r\n"
|
|
19
|
-
|
|
20
|
-
def __init__(
|
|
21
|
-
self,
|
|
22
|
-
data: Optional[Any] = None,
|
|
23
|
-
*,
|
|
24
|
-
event: Optional[str] = None,
|
|
25
|
-
id: Optional[str] = None,
|
|
26
|
-
retry: Optional[int] = None,
|
|
27
|
-
comment: Optional[str] = None,
|
|
28
|
-
sep: Optional[str] = None,
|
|
29
|
-
) -> None:
|
|
30
|
-
self.data = data
|
|
31
|
-
self.event = event
|
|
32
|
-
self.id = id
|
|
33
|
-
self.retry = retry
|
|
34
|
-
self.comment = comment
|
|
35
|
-
self._sep = sep if sep is not None else self.DEFAULT_SEPARATOR
|
|
36
|
-
|
|
37
|
-
def encode(self) -> bytes:
|
|
38
|
-
buffer = io.StringIO()
|
|
39
|
-
if self.comment is not None:
|
|
40
|
-
for chunk in self._LINE_SEP_EXPR.split(str(self.comment)):
|
|
41
|
-
buffer.write(f": {chunk}{self._sep}")
|
|
42
|
-
|
|
43
|
-
if self.id is not None:
|
|
44
|
-
# Clean newlines in the event id
|
|
45
|
-
buffer.write("id: " + self._LINE_SEP_EXPR.sub("", self.id) + self._sep)
|
|
46
|
-
|
|
47
|
-
if self.event is not None:
|
|
48
|
-
# Clean newlines in the event name
|
|
49
|
-
buffer.write(
|
|
50
|
-
"event: " + self._LINE_SEP_EXPR.sub("", self.event) + self._sep
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
if self.data is not None:
|
|
54
|
-
# Break multi-line data into multiple data: lines
|
|
55
|
-
for chunk in self._LINE_SEP_EXPR.split(str(self.data)):
|
|
56
|
-
buffer.write(f"data: {chunk}{self._sep}")
|
|
57
|
-
|
|
58
|
-
if self.retry is not None:
|
|
59
|
-
if not isinstance(self.retry, int):
|
|
60
|
-
raise TypeError("retry argument must be int")
|
|
61
|
-
buffer.write(f"retry: {self.retry}{self._sep}")
|
|
62
|
-
|
|
63
|
-
buffer.write(self._sep)
|
|
64
|
-
return buffer.getvalue().encode("utf-8")
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class JSONServerSentEvent(ServerSentEvent):
|
|
68
|
-
"""
|
|
69
|
-
Helper class to format JSON data for Server-Sent Events (SSE).
|
|
70
|
-
"""
|
|
71
|
-
|
|
72
|
-
def __init__(
|
|
73
|
-
self,
|
|
74
|
-
data: Optional[Any] = None,
|
|
75
|
-
*args,
|
|
76
|
-
**kwargs,
|
|
77
|
-
) -> None:
|
|
78
|
-
super().__init__(
|
|
79
|
-
json.dumps(
|
|
80
|
-
data,
|
|
81
|
-
ensure_ascii=False,
|
|
82
|
-
allow_nan=False,
|
|
83
|
-
indent=None,
|
|
84
|
-
separators=(",", ":"),
|
|
85
|
-
)
|
|
86
|
-
if data is not None
|
|
87
|
-
else None,
|
|
88
|
-
*args,
|
|
89
|
-
**kwargs,
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
def ensure_bytes(data: Union[bytes, dict, ServerSentEvent, Any], sep: str) -> bytes:
|
|
94
|
-
if isinstance(data, bytes):
|
|
95
|
-
return data
|
|
96
|
-
if isinstance(data, ServerSentEvent):
|
|
97
|
-
return data.encode()
|
|
98
|
-
if isinstance(data, dict):
|
|
99
|
-
data["sep"] = sep
|
|
100
|
-
return ServerSentEvent(**data).encode()
|
|
101
|
-
return ServerSentEvent(str(data), sep=sep).encode()
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""
|
|
3
|
+
Copy from https://github.com/sysid/sse-starlette/blob/main/sse_starlette/event.py
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import io
|
|
7
|
+
import json
|
|
8
|
+
import re
|
|
9
|
+
from typing import Any, Optional, Union
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ServerSentEvent:
|
|
13
|
+
"""
|
|
14
|
+
Helper class to format data for Server-Sent Events (SSE).
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
_LINE_SEP_EXPR = re.compile(r"\r\n|\r|\n")
|
|
18
|
+
DEFAULT_SEPARATOR = "\r\n"
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
data: Optional[Any] = None,
|
|
23
|
+
*,
|
|
24
|
+
event: Optional[str] = None,
|
|
25
|
+
id: Optional[str] = None,
|
|
26
|
+
retry: Optional[int] = None,
|
|
27
|
+
comment: Optional[str] = None,
|
|
28
|
+
sep: Optional[str] = None,
|
|
29
|
+
) -> None:
|
|
30
|
+
self.data = data
|
|
31
|
+
self.event = event
|
|
32
|
+
self.id = id
|
|
33
|
+
self.retry = retry
|
|
34
|
+
self.comment = comment
|
|
35
|
+
self._sep = sep if sep is not None else self.DEFAULT_SEPARATOR
|
|
36
|
+
|
|
37
|
+
def encode(self) -> bytes:
|
|
38
|
+
buffer = io.StringIO()
|
|
39
|
+
if self.comment is not None:
|
|
40
|
+
for chunk in self._LINE_SEP_EXPR.split(str(self.comment)):
|
|
41
|
+
buffer.write(f": {chunk}{self._sep}")
|
|
42
|
+
|
|
43
|
+
if self.id is not None:
|
|
44
|
+
# Clean newlines in the event id
|
|
45
|
+
buffer.write("id: " + self._LINE_SEP_EXPR.sub("", self.id) + self._sep)
|
|
46
|
+
|
|
47
|
+
if self.event is not None:
|
|
48
|
+
# Clean newlines in the event name
|
|
49
|
+
buffer.write(
|
|
50
|
+
"event: " + self._LINE_SEP_EXPR.sub("", self.event) + self._sep
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
if self.data is not None:
|
|
54
|
+
# Break multi-line data into multiple data: lines
|
|
55
|
+
for chunk in self._LINE_SEP_EXPR.split(str(self.data)):
|
|
56
|
+
buffer.write(f"data: {chunk}{self._sep}")
|
|
57
|
+
|
|
58
|
+
if self.retry is not None:
|
|
59
|
+
if not isinstance(self.retry, int):
|
|
60
|
+
raise TypeError("retry argument must be int")
|
|
61
|
+
buffer.write(f"retry: {self.retry}{self._sep}")
|
|
62
|
+
|
|
63
|
+
buffer.write(self._sep)
|
|
64
|
+
return buffer.getvalue().encode("utf-8")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class JSONServerSentEvent(ServerSentEvent):
|
|
68
|
+
"""
|
|
69
|
+
Helper class to format JSON data for Server-Sent Events (SSE).
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
def __init__(
|
|
73
|
+
self,
|
|
74
|
+
data: Optional[Any] = None,
|
|
75
|
+
*args,
|
|
76
|
+
**kwargs,
|
|
77
|
+
) -> None:
|
|
78
|
+
super().__init__(
|
|
79
|
+
json.dumps(
|
|
80
|
+
data,
|
|
81
|
+
ensure_ascii=False,
|
|
82
|
+
allow_nan=False,
|
|
83
|
+
indent=None,
|
|
84
|
+
separators=(",", ":"),
|
|
85
|
+
)
|
|
86
|
+
if data is not None
|
|
87
|
+
else None,
|
|
88
|
+
*args,
|
|
89
|
+
**kwargs,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def ensure_bytes(data: Union[bytes, dict, ServerSentEvent, Any], sep: str) -> bytes:
|
|
94
|
+
if isinstance(data, bytes):
|
|
95
|
+
return data
|
|
96
|
+
if isinstance(data, ServerSentEvent):
|
|
97
|
+
return data.encode()
|
|
98
|
+
if isinstance(data, dict):
|
|
99
|
+
data["sep"] = sep
|
|
100
|
+
return ServerSentEvent(**data).encode()
|
|
101
|
+
return ServerSentEvent(str(data), sep=sep).encode()
|