mirror of
https://github.com/remsky/Kokoro-FastAPI.git
synced 2025-08-05 16:48:53 +00:00
converted CRLF ending lines to LF ones in api/src/structures/custom_responses.py
let ruff organise the imports
This commit is contained in:
parent
7d73c3c7ee
commit
3c5029f801
1 changed files with 50 additions and 50 deletions
|
@ -1,51 +1,51 @@
|
||||||
from collections.abc import AsyncIterable, Iterable
|
import json
|
||||||
|
import typing
|
||||||
import json
|
from collections.abc import AsyncIterable, Iterable
|
||||||
import typing
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from starlette.background import BackgroundTask
|
from starlette.background import BackgroundTask
|
||||||
from starlette.concurrency import iterate_in_threadpool
|
from starlette.concurrency import iterate_in_threadpool
|
||||||
from starlette.responses import JSONResponse, StreamingResponse
|
from starlette.responses import JSONResponse, StreamingResponse
|
||||||
|
|
||||||
|
|
||||||
class JSONStreamingResponse(StreamingResponse, JSONResponse):
|
class JSONStreamingResponse(StreamingResponse, JSONResponse):
|
||||||
"""StreamingResponse that also render with JSON."""
|
"""StreamingResponse that also render with JSON."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
content: Iterable | AsyncIterable,
|
content: Iterable | AsyncIterable,
|
||||||
status_code: int = 200,
|
status_code: int = 200,
|
||||||
headers: dict[str, str] | None = None,
|
headers: dict[str, str] | None = None,
|
||||||
media_type: str | None = None,
|
media_type: str | None = None,
|
||||||
background: BackgroundTask | None = None,
|
background: BackgroundTask | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if isinstance(content, AsyncIterable):
|
if isinstance(content, AsyncIterable):
|
||||||
self._content_iterable: AsyncIterable = content
|
self._content_iterable: AsyncIterable = content
|
||||||
else:
|
else:
|
||||||
self._content_iterable = iterate_in_threadpool(content)
|
self._content_iterable = iterate_in_threadpool(content)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def body_iterator() -> AsyncIterable[bytes]:
|
async def body_iterator() -> AsyncIterable[bytes]:
|
||||||
async for content_ in self._content_iterable:
|
async for content_ in self._content_iterable:
|
||||||
if isinstance(content_, BaseModel):
|
if isinstance(content_, BaseModel):
|
||||||
content_ = content_.model_dump()
|
content_ = content_.model_dump()
|
||||||
yield self.render(content_)
|
yield self.render(content_)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self.body_iterator = body_iterator()
|
self.body_iterator = body_iterator()
|
||||||
self.status_code = status_code
|
self.status_code = status_code
|
||||||
if media_type is not None:
|
if media_type is not None:
|
||||||
self.media_type = media_type
|
self.media_type = media_type
|
||||||
self.background = background
|
self.background = background
|
||||||
self.init_headers(headers)
|
self.init_headers(headers)
|
||||||
|
|
||||||
def render(self, content: typing.Any) -> bytes:
|
def render(self, content: typing.Any) -> bytes:
|
||||||
return (json.dumps(
|
return (json.dumps(
|
||||||
content,
|
content,
|
||||||
ensure_ascii=False,
|
ensure_ascii=False,
|
||||||
allow_nan=False,
|
allow_nan=False,
|
||||||
indent=None,
|
indent=None,
|
||||||
separators=(",", ":"),
|
separators=(",", ":"),
|
||||||
) + "\n").encode("utf-8")
|
) + "\n").encode("utf-8")
|
Loading…
Add table
Reference in a new issue