forked from OpenBMB/XAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_body.py
35 lines (23 loc) · 899 Bytes
/
response_body.py
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
import json
from typing import Optional, Union
from pydantic import BaseModel, Json
class ResponseBody(BaseModel):
data: Union[str, dict, list, Json, None] = None
success: bool = True
message: Union[str, None] = None
def to_dict(self):
return self.dict()
def to_json(self):
return self.json()
class WebsocketResponseBody():
def __init__(self, data: Union[str, dict, list, Json, None], status: str = "success", message: Union[str, None] = None, **kwargs):
self.data = data
self.status = status
self.message = message
self.extend(kwargs)
def to_text(self):
return json.dumps(self.__dict__, ensure_ascii=False, indent=2)
def extend(self, extend: dict):
for key, value in extend.items():
if key not in self.__dict__.keys():
self.__dict__[key] = value