2023-07-08 00:20:14 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package notion
|
|
|
|
|
|
|
|
import (
|
2023-08-14 06:58:45 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-07-08 00:20:14 +02:00
|
|
|
"fmt"
|
2023-08-14 06:58:45 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2023-07-08 00:20:14 +02:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
"miniflux.app/v2/internal/version"
|
2023-07-08 00:20:14 +02:00
|
|
|
)
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
const defaultClientTimeout = 10 * time.Second
|
|
|
|
|
2023-07-08 00:20:14 +02:00
|
|
|
type Client struct {
|
2023-08-14 06:58:45 +02:00
|
|
|
apiToken string
|
|
|
|
pageID string
|
2023-07-08 00:20:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
func NewClient(apiToken, pageID string) *Client {
|
|
|
|
return &Client{apiToken, pageID}
|
2023-07-08 00:20:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
func (c *Client) UpdateDocument(entryURL string, entryTitle string) error {
|
|
|
|
if c.apiToken == "" || c.pageID == "" {
|
|
|
|
return fmt.Errorf("notion: missing API token or page ID")
|
2023-07-08 00:20:14 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
|
|
|
|
apiEndpoint := "https://api.notion.com/v1/blocks/" + c.pageID + "/children"
|
|
|
|
requestBody, err := json.Marshal(¬ionDocument{
|
|
|
|
Children: []block{
|
2023-07-08 00:20:14 +02:00
|
|
|
{
|
|
|
|
Object: "block",
|
|
|
|
Type: "bookmark",
|
2023-08-14 06:58:45 +02:00
|
|
|
Bookmark: bookmarkObject{
|
|
|
|
Caption: []any{},
|
2023-07-08 00:20:14 +02:00
|
|
|
URL: entryURL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-14 06:58:45 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notion: unable to encode request body: %v", err)
|
2023-07-08 00:20:14 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPatch, apiEndpoint, bytes.NewReader(requestBody))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notion: unable to create request: %v", err)
|
2023-07-08 00:20:14 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
|
request.Header.Set("Notion-Version", "2022-06-28")
|
|
|
|
request.Header.Set("Authorization", "Bearer "+c.apiToken)
|
|
|
|
|
|
|
|
httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
|
response, err := httpClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notion: unable to send request: %v", err)
|
2023-07-08 00:20:14 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
defer response.Body.Close()
|
2023-07-08 00:20:14 +02:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("notion: unable to update document: url=%s status=%d", apiEndpoint, response.StatusCode)
|
2023-07-08 00:20:14 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
|
2023-07-08 00:20:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
|
|
|
|
type notionDocument struct {
|
|
|
|
Children []block `json:"children"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type block struct {
|
|
|
|
Object string `json:"object"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Bookmark bookmarkObject `json:"bookmark"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type bookmarkObject struct {
|
|
|
|
Caption []any `json:"caption"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|