2024-09-10 10:08:12 +02:00
|
|
|
package notion
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-09-16 16:55:29 +02:00
|
|
|
"errors"
|
2024-09-17 23:22:58 +02:00
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
2024-09-10 10:08:12 +02:00
|
|
|
|
|
|
|
|
"github.com/jomei/notionapi"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var client *notionapi.Client
|
|
|
|
|
|
|
|
|
|
func Init(token string) {
|
|
|
|
|
client = notionapi.NewClient(notionapi.Token(token))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetClient() *notionapi.Client {
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 23:22:58 +02:00
|
|
|
func GetTableRows(tableId string, numberOfRows int, startFromId string) ([]notionapi.TableRowBlock, error) {
|
2024-09-16 16:55:29 +02:00
|
|
|
if client == nil {
|
|
|
|
|
return nil, errors.New("notion client not initialized")
|
|
|
|
|
}
|
2024-09-17 23:22:58 +02:00
|
|
|
var rows []notionapi.TableRowBlock
|
|
|
|
|
var nextNumberToGet int
|
|
|
|
|
if numberOfRows > 100 {
|
|
|
|
|
nextNumberToGet = 100
|
|
|
|
|
} else {
|
|
|
|
|
nextNumberToGet = numberOfRows
|
|
|
|
|
}
|
|
|
|
|
for numberOfRows > 0 {
|
|
|
|
|
block, err := client.Block.GetChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.Pagination{
|
|
|
|
|
StartCursor: notionapi.Cursor(startFromId),
|
|
|
|
|
PageSize: nextNumberToGet,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for _, block := range block.Results {
|
|
|
|
|
if block.GetType().String() == "table_row" {
|
|
|
|
|
tableRow, ok := block.(*notionapi.TableRowBlock)
|
|
|
|
|
if !ok {
|
|
|
|
|
slog.Error("Notion.GetTableRows Failed to cast block to table row")
|
|
|
|
|
return nil, errors.New("Notion.GetTableRows failed to cast block to table row")
|
|
|
|
|
}
|
|
|
|
|
rows = append(rows, *tableRow)
|
|
|
|
|
} else {
|
|
|
|
|
slog.Error(fmt.Sprintf("Block ID %s is not a table row", block.GetID()))
|
|
|
|
|
return nil, errors.New("Notion.GetAllTableRows block ID is not a table row")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
numberOfRows -= nextNumberToGet
|
|
|
|
|
if numberOfRows > 100 {
|
|
|
|
|
nextNumberToGet = 100
|
|
|
|
|
} else {
|
|
|
|
|
nextNumberToGet = numberOfRows
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rows, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetAllTableRows(tableId string) ([]notionapi.TableRowBlock, error) {
|
|
|
|
|
if client == nil {
|
|
|
|
|
return nil, errors.New("notion client not initialized")
|
|
|
|
|
}
|
|
|
|
|
rows := []notionapi.TableRowBlock{}
|
|
|
|
|
nextCursor := ""
|
|
|
|
|
hasMore := true
|
|
|
|
|
for hasMore {
|
|
|
|
|
blockChildren, err := client.Block.GetChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.Pagination{
|
|
|
|
|
StartCursor: notionapi.Cursor(nextCursor),
|
|
|
|
|
PageSize: 100,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for _, block := range blockChildren.Results {
|
|
|
|
|
if block.GetType().String() == "table_row" {
|
|
|
|
|
tableRow, ok := block.(*notionapi.TableRowBlock)
|
|
|
|
|
if !ok {
|
|
|
|
|
slog.Error("Notion.GetAllTableRows Failed to cast block to table row")
|
|
|
|
|
return nil, errors.New("Notion.GetAllTableRows failed to cast block to table row")
|
|
|
|
|
}
|
|
|
|
|
rows = append(rows, *tableRow)
|
|
|
|
|
} else {
|
|
|
|
|
slog.Error(fmt.Sprintf("Block ID %s is not a table row", block.GetID()))
|
|
|
|
|
return nil, errors.New("Notion.GetAllTableRows block ID is not a table row")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nextCursor = blockChildren.NextCursor
|
|
|
|
|
hasMore = blockChildren.HasMore
|
2024-09-16 16:55:29 +02:00
|
|
|
}
|
2024-09-17 23:22:58 +02:00
|
|
|
return rows, nil
|
2024-09-10 10:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-17 23:22:58 +02:00
|
|
|
func WriteTableRow(content []string, tableId string, after string) (string, error) {
|
2024-09-16 16:55:29 +02:00
|
|
|
if client == nil {
|
2024-09-17 23:22:58 +02:00
|
|
|
return "", errors.New("notion client not initialized")
|
2024-09-16 16:55:29 +02:00
|
|
|
}
|
|
|
|
|
rich := [][]notionapi.RichText{}
|
|
|
|
|
for _, c := range content {
|
|
|
|
|
rich = append(rich, []notionapi.RichText{
|
|
|
|
|
{
|
|
|
|
|
Type: "text",
|
|
|
|
|
Text: ¬ionapi.Text{
|
|
|
|
|
Content: c,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
tableRow := notionapi.TableRowBlock{
|
|
|
|
|
BasicBlock: notionapi.BasicBlock{
|
|
|
|
|
Object: "block",
|
|
|
|
|
Type: "table_row",
|
|
|
|
|
},
|
|
|
|
|
TableRow: notionapi.TableRow{
|
|
|
|
|
Cells: rich,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 23:22:58 +02:00
|
|
|
res, err := client.Block.AppendChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.AppendBlockChildrenRequest{
|
2024-09-16 16:55:29 +02:00
|
|
|
After: notionapi.BlockID(after),
|
|
|
|
|
Children: []notionapi.Block{tableRow},
|
2024-09-10 10:08:12 +02:00
|
|
|
})
|
2024-09-16 16:55:29 +02:00
|
|
|
|
2024-09-17 23:22:58 +02:00
|
|
|
return res.Results[0].GetID().String(), err
|
2024-09-10 10:08:12 +02:00
|
|
|
}
|