package notion import ( "context" "errors" "fmt" "log/slog" "github.com/jomei/notionapi" ) var client *notionapi.Client func Init(token string) { client = notionapi.NewClient(notionapi.Token(token)) } func GetClient() *notionapi.Client { return client } func GetTableRows(tableId string, numberOfRows int, startFromId string) ([]notionapi.TableRowBlock, error) { if client == nil { return nil, errors.New("notion client not initialized") } 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 } return rows, nil } func WriteTableRow(content []string, tableId string, after string) (string, error) { if client == nil { return "", errors.New("notion client not initialized") } 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, }, } res, err := client.Block.AppendChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.AppendBlockChildrenRequest{ After: notionapi.BlockID(after), Children: []notionapi.Block{tableRow}, }) return res.Results[0].GetID().String(), err }