-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathdoc_response.py
More file actions
34 lines (25 loc) · 1020 Bytes
/
Copy pathdoc_response.py
File metadata and controls
34 lines (25 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import Any
try:
from fastapi.responses import JSONResponse, Response
except ImportError:
class NoImportResponse:
def __init__(self, *args, **kwargs):
ImportError('fastapi is not installed')
Response = JSONResponse = NoImportResponse # type: ignore
class DocResponse(JSONResponse):
"""
This is a custom Response class for FastAPI and starlette. This is needed
to handle serialization of the Document types when using FastAPI
EXAMPLE USAGE
.. code-block:: python
from docarray.documets import Text
from docarray.base_doc import DocResponse
@app.post("/doc/", response_model=Text, response_class=DocResponse)
async def create_item(doc: Text) -> Text:
return doc
"""
def render(self, content: Any) -> bytes:
if isinstance(content, bytes):
return content
else:
raise ValueError(f'{self.__class__} only work with json bytes content')