From dbf88381839f691ffe053326d72d807244398828 Mon Sep 17 00:00:00 2001 From: burdukov Date: Sun, 26 Nov 2023 02:23:49 +0500 Subject: [PATCH] added: types for api --- neuroapi/types/__init__.py | 4 ++++ neuroapi/types/_admin.py | 22 ++++++++++++++++++ neuroapi/types/_helpers.py | 47 ++++++++++++++++++++++++++++++++++++++ neuroapi/types/_image.py | 28 +++++++++++++++++++++++ neuroapi/types/_post.py | 44 +++++++++++++++++++++++++++++++++++ neuroapi/types/_user.py | 22 ++++++++++++++++++ 6 files changed, 167 insertions(+) create mode 100644 neuroapi/types/__init__.py create mode 100644 neuroapi/types/_admin.py create mode 100644 neuroapi/types/_helpers.py create mode 100644 neuroapi/types/_image.py create mode 100644 neuroapi/types/_post.py create mode 100644 neuroapi/types/_user.py diff --git a/neuroapi/types/__init__.py b/neuroapi/types/__init__.py new file mode 100644 index 0000000..cb41a50 --- /dev/null +++ b/neuroapi/types/__init__.py @@ -0,0 +1,4 @@ +from ._post import Post +from ._image import Image +from ._admin import Admin +from ._user import User diff --git a/neuroapi/types/_admin.py b/neuroapi/types/_admin.py new file mode 100644 index 0000000..6ce6e5e --- /dev/null +++ b/neuroapi/types/_admin.py @@ -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 diff --git a/neuroapi/types/_helpers.py b/neuroapi/types/_helpers.py new file mode 100644 index 0000000..9324d7d --- /dev/null +++ b/neuroapi/types/_helpers.py @@ -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 diff --git a/neuroapi/types/_image.py b/neuroapi/types/_image.py new file mode 100644 index 0000000..bce6fb7 --- /dev/null +++ b/neuroapi/types/_image.py @@ -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 diff --git a/neuroapi/types/_post.py b/neuroapi/types/_post.py new file mode 100644 index 0000000..cb1badb --- /dev/null +++ b/neuroapi/types/_post.py @@ -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 diff --git a/neuroapi/types/_user.py b/neuroapi/types/_user.py new file mode 100644 index 0000000..71300a0 --- /dev/null +++ b/neuroapi/types/_user.py @@ -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