added: types for api

This commit is contained in:
2023-11-26 02:23:49 +05:00
parent 8c2d706401
commit dbf8838183
6 changed files with 167 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
from ._post import Post
from ._image import Image
from ._admin import Admin
from ._user import User

22
neuroapi/types/_admin.py Normal file
View File

@@ -0,0 +1,22 @@
from dataclasses import dataclass
from typing import Any
from ._helpers import *
@dataclass
class Admin:
id: int
user_id: int
@staticmethod
def from_dict(obj: Any) -> 'Admin':
assert isinstance(obj, dict)
id = from_int(obj.get("id"))
user_id = int(from_str(obj.get("user_id")))
return Admin(id, user_id)
def to_dict(self) -> dict:
result: dict = {}
result["id"] = from_int(self.id)
result["user_id"] = from_str(str(self.user_id))
return result

View File

@@ -0,0 +1,47 @@
from datetime import datetime
from typing import Any, Callable, List, TypeVar, Type, cast
import dateutil.parser
T = TypeVar("T")
def from_bool(x: Any) -> bool:
assert isinstance(x, bool)
return x
def from_str(x: Any) -> str:
assert isinstance(x, str)
return x
def from_union(fs, x):
for f in fs:
try:
return f(x)
except:
pass
assert False
def from_none(x: Any) -> Any:
assert x is None
return x
def from_list(f: Callable[[Any], T], x: Any) -> List[T]:
assert isinstance(x, list)
return [f(y) for y in x]
def from_datetime(x: Any) -> datetime:
return dateutil.parser.parse(x)
def to_class(c: Type[T], x: Any) -> dict:
assert isinstance(x, c)
return cast(Any, x).to_dict()
def from_int(x: Any) -> int:
assert isinstance(x, int) and not isinstance(x, bool)
return x

28
neuroapi/types/_image.py Normal file
View File

@@ -0,0 +1,28 @@
from dataclasses import dataclass
from uuid import UUID
from ._helpers import *
@dataclass
class Image:
message_id: int
file_id: str
has_spoiler: bool
post_uuid: UUID
@staticmethod
def from_dict(obj: Any) -> 'Image':
assert isinstance(obj, dict)
message_id = from_int(obj.get("message_id"))
file_id = from_str(obj.get("file_id"))
has_spoiler = from_bool(obj.get("has_spoiler"))
post_uuid = UUID(obj.get("post_uuid"))
return Image(message_id, file_id, has_spoiler, post_uuid)
def to_dict(self) -> dict:
result: dict = {}
result["message_id"] = from_int(self.message_id)
result["file_id"] = from_str(self.file_id)
result["has_spoiler"] = from_bool(self.has_spoiler)
result["post_uuid"] = str(self.post_uuid)
return result

44
neuroapi/types/_post.py Normal file
View File

@@ -0,0 +1,44 @@
from dataclasses import dataclass
from uuid import UUID
from datetime import datetime
from typing import Any, List, Optional
from ._image import Image
from ._helpers import *
@dataclass
class Post:
uuid: UUID
posted: bool
text: str
media_group_id: int | str
timestamp: datetime
from_user_id: int
images: Optional[List[Image]] = None
@staticmethod
def from_dict(obj: Any) -> 'Post':
assert isinstance(obj, dict)
uuid = UUID(obj.get("uuid"))
posted = from_bool(obj.get("posted"))
text = from_str(obj.get("text"))
media_group_id = from_str(obj.get("media_group_id")) if obj.get(
"media_group_id") is not None else 'None'
timestamp = from_datetime(obj.get("timestamp"))
from_user_id = int(from_str(obj.get("from_user_id")))
images = from_union([lambda x: from_list(
Image.from_dict, x), from_none], obj.get("images"))
return Post(uuid, posted, text, media_group_id, timestamp, from_user_id, images)
def to_dict(self) -> dict:
result: dict = {}
result["uuid"] = str(self.uuid)
result["posted"] = from_bool(self.posted)
result["text"] = from_str(self.text)
result["media_group_id"] = from_str(str(self.media_group_id))
result["timestamp"] = self.timestamp.isoformat()
result["from_user_id"] = from_str(str(self.from_user_id))
if self.images is not None:
result["images"] = from_union([lambda x: from_list(
lambda x: to_class(Image, x), x), from_none], self.images)
return result

22
neuroapi/types/_user.py Normal file
View File

@@ -0,0 +1,22 @@
from dataclasses import dataclass
from typing import Any
from ._helpers import *
@dataclass
class User:
id: int
username: str
@staticmethod
def from_dict(obj: Any) -> 'User':
assert isinstance(obj, dict)
id = int(from_str(obj.get("id")))
username = from_str(obj.get("username"))
return User(id, username)
def to_dict(self) -> dict:
result: dict = {}
result["id"] = from_str(str(self.id))
result["username"] = from_str(self.username)
return result