Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database: unstable create & insert api #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
dist
pywolai.egg-info
pywolai/__pycache__/
.idea
28 changes: 24 additions & 4 deletions pywolai/api.py
Original file line number Diff line number Diff line change
@@ -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):
"""安全请求
Expand Down Expand Up @@ -176,6 +177,25 @@ def create_blocks(self, parent_id:str, blocks:List[Block] ):

return data


# Database
# TODO: Database API
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
62 changes: 62 additions & 0 deletions pywolai/database_format.py
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions pywolai/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions pywolai/enums/request_enum.py
Original file line number Diff line number Diff line change
@@ -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'