-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
54 lines (42 loc) · 1.37 KB
/
models.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import dataclasses
from typing import Any, Optional, Union
__all__ = ("SurrealRequest","SurrealResponse","LoginParams")
@dataclasses.dataclass(frozen=True)
class LoginParams:
"""
A class used to represent SurrealDB login parameters.
...
Attributes
----------
username: str
The username of the user. alias of field is user
password: str
The password of the user. alias of field is pass
Methods
-------
to_dict() -> dict[str, Any]
Convert the SurrealDB login parameters to a dict with alias as keys
"""
username: str = dataclasses.field(metadata={"alias": "user"})
password: str = dataclasses.field(metadata={"alias": "pass"})
def __post_init__(self):
if not self.username:
raise ValueError("username is required")
if not self.password:
raise ValueError("password is required")
def to_dict(self) -> dict[str, Any]:
return {
dataclasses.fields(self)[i].metadata["alias"]: getattr(
self, dataclasses.fields(self)[i].name
)
for i in range(len(dataclasses.fields(self)))
}
@dataclasses.dataclass(frozen=True)
class SurrealRequest:
id: str
method: str
params: tuple[Any]
@dataclasses.dataclass(frozen=True)
class SurrealResponse:
result: Union[dict, list[Any]]
id: Optional[str] = None