From a0117b2f398448d99a4faddf71e792af696a7871 Mon Sep 17 00:00:00 2001 From: fangzn8 Date: Wed, 3 Jan 2024 15:33:25 +0800 Subject: [PATCH] database: unstable create & insert api --- .gitignore | 1 + pywolai/api.py | 28 +++++++++++++--- pywolai/database_format.py | 62 +++++++++++++++++++++++++++++++++++ pywolai/enums/__init__.py | 2 ++ pywolai/enums/request_enum.py | 10 ++++++ 5 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 pywolai/database_format.py create mode 100644 pywolai/enums/request_enum.py diff --git a/.gitignore b/.gitignore index 833cf63..65d5930 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build dist pywolai.egg-info pywolai/__pycache__/ +.idea \ No newline at end of file diff --git a/pywolai/api.py b/pywolai/api.py index 35383ec..57a13a1 100644 --- a/pywolai/api.py +++ b/pywolai/api.py @@ -1,9 +1,10 @@ from typing import List import requests import json -from pywolai.enums import ErrorCode, BlockTypes +from pywolai.enums import ErrorCode, BlockTypes, RequestMethod, RequestAddr from pywolai.block import Block from pywolai.block_format import BlockFormat +from pywolai.database_format import DatabaseFormat def safe_request(url, method, params=None, data=None, headers=None): """安全请求 @@ -176,6 +177,25 @@ def create_blocks(self, parent_id:str, blocks:List[Block] ): return data - - # Database - # TODO: Database API \ No newline at end of file + def get_database(self, database_id:str) -> DatabaseFormat: + """ + :param database_id: 数据库ID + :return 序列化后的数据库实例 + """ + url = RequestAddr.DATABASE_GET % (self._base_url, database_id) + + data = safe_request(url, RequestMethod.GET, headers={"Authorization": self.get_token()}) + return DatabaseFormat(**data) + + def insert_database(self, database_id:str, value:[{str:any}]): + """ + :param database_id: 数据库ID + :return: + """ + url = RequestAddr.DATABASE_INSERT % (self._base_url, database_id) + data = { + "rows": value + } + + data = safe_request(url, "POST", data = data, headers={"Authorization": self.get_token()}) + return data diff --git a/pywolai/database_format.py b/pywolai/database_format.py new file mode 100644 index 0000000..ef26866 --- /dev/null +++ b/pywolai/database_format.py @@ -0,0 +1,62 @@ +from pywolai.enums import BlockTypes + + +class DatabaseRowDataItemFormat: + """ + Database format column + """ + def __init__( + self, + type: BlockTypes, + value: str, + ): + self.id = id + self.type = type + self.value = value + + +class DatabaseRowDataFormat: + """ + Database format column + """ + def __init__( + self, + 标题: DatabaseRowDataItemFormat, + 标签: DatabaseRowDataItemFormat, + 字段名: DatabaseRowDataItemFormat, + ): + self.id = id + self.标题 = 标题 + self.标签 = 标签 + self.字段名 = 字段名 + + + +class DatabaseRowFormat: + """ + Database format row + """ + def __init__( + self, + page_id: str, + data: DatabaseRowDataFormat, + ): + self.id = id + self.page_id = page_id + self.data = data + + +class DatabaseFormat: + """ + Database format + """ + def __init__( + self, + column_order: [str], + rows: [DatabaseRowFormat], + **kwargs + ): + self.column_order = column_order + self.rows = rows + self.other_fields = kwargs + diff --git a/pywolai/enums/__init__.py b/pywolai/enums/__init__.py index f8f6e52..7dfeb6f 100644 --- a/pywolai/enums/__init__.py +++ b/pywolai/enums/__init__.py @@ -7,3 +7,5 @@ from pywolai.enums.rich_text import RichText from pywolai.enums.error_code import ErrorCode + +from pywolai.enums.request_enum import RequestMethod, RequestAddr diff --git a/pywolai/enums/request_enum.py b/pywolai/enums/request_enum.py new file mode 100644 index 0000000..f308a0a --- /dev/null +++ b/pywolai/enums/request_enum.py @@ -0,0 +1,10 @@ +class RequestAddr: + DATABASE_GET = '%s/databases/%s' + DATABASE_INSERT = '%s/databases/%s/rows' + + +class RequestMethod: + GET = 'GET' + POST = 'POST' + PUT = 'PUT' + DELETE = 'DELETE'