2024-09-10 10:08:12 +02:00
|
|
|
package notion
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-09-16 16:55:29 +02:00
|
|
|
"errors"
|
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-16 16:55:29 +02:00
|
|
|
func GetTableRows(tableId string, numberOfRows int, startFromId string) ([]notionapi.Block, error) {
|
|
|
|
|
if client == nil {
|
|
|
|
|
return nil, errors.New("notion client not initialized")
|
|
|
|
|
}
|
|
|
|
|
block, err := client.Block.GetChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.Pagination{
|
|
|
|
|
StartCursor: notionapi.Cursor(startFromId),
|
|
|
|
|
PageSize: numberOfRows,
|
2024-09-10 10:08:12 +02:00
|
|
|
})
|
2024-09-16 16:55:29 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return block.Results, nil
|
2024-09-10 10:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:55:29 +02:00
|
|
|
func WriteTableRow(content []string, tableId string, after 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,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := client.Block.AppendChildren(context.Background(), notionapi.BlockID(tableId), ¬ionapi.AppendBlockChildrenRequest{
|
|
|
|
|
After: notionapi.BlockID(after),
|
|
|
|
|
Children: []notionapi.Block{tableRow},
|
2024-09-10 10:08:12 +02:00
|
|
|
})
|
2024-09-16 16:55:29 +02:00
|
|
|
|
|
|
|
|
return err
|
2024-09-10 10:08:12 +02:00
|
|
|
}
|