Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2023-11-28 14:29:16 -08:00
parent 6b8807feea
commit 373b5ef4ae
123 changed files with 1858 additions and 1955 deletions

View File

@ -1,11 +1,14 @@
from typing import Any, Dict, Union
from typing import Any, Dict, Type, TypeVar, Union
from typing_extensions import Self
import attr
from .failure_web_socket_response import FailureWebSocketResponse
from .success_web_socket_response import SuccessWebSocketResponse
GY = TypeVar("GY", bound="WebSocketResponse")
@attr.s(auto_attribs=True)
class WebSocketResponse:
"""Websocket responses can either be successful or unsuccessful. Slightly different schemas in either case."""
@ -13,37 +16,36 @@ class WebSocketResponse:
type: Union[
SuccessWebSocketResponse,
FailureWebSocketResponse,
] = None
]
def __init__(
self,
type: Union[
type(SuccessWebSocketResponse),
type(FailureWebSocketResponse),
SuccessWebSocketResponse,
FailureWebSocketResponse,
],
):
self.type = type
def to_dict(self) -> Dict[str, Any]:
if isinstance(self.type, SuccessWebSocketResponse):
n: SuccessWebSocketResponse = self.type
return n.to_dict()
XP: SuccessWebSocketResponse = self.type
return XP.to_dict()
elif isinstance(self.type, FailureWebSocketResponse):
n: FailureWebSocketResponse = self.type
return n.to_dict()
QM: FailureWebSocketResponse = self.type
return QM.to_dict()
raise Exception("Unknown type")
def from_dict(self, d) -> Self:
@classmethod
def from_dict(cls: Type[GY], d: Dict[str, Any]) -> GY:
if d.get("type") == "SuccessWebSocketResponse":
n: SuccessWebSocketResponse = SuccessWebSocketResponse()
n.from_dict(d)
self.type = n
return Self
VT: SuccessWebSocketResponse = SuccessWebSocketResponse()
VT.from_dict(d)
return cls(type=VT)
elif d.get("type") == "FailureWebSocketResponse":
n: FailureWebSocketResponse = FailureWebSocketResponse()
n.from_dict(d)
self.type = n
return self
UR: FailureWebSocketResponse = FailureWebSocketResponse()
UR.from_dict(d)
return cls(type=UR)
raise Exception("Unknown type")