notion.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package notion
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "miniflux.app/v2/internal/http/client"
  9. )
  10. type Client struct {
  11. apiToken string
  12. pageID string
  13. }
  14. func NewClient(apiToken, pageID string) *Client {
  15. return &Client{apiToken, pageID}
  16. }
  17. func (c *Client) UpdateDocument(entryURL string, entryTitle string) error {
  18. if c.apiToken == "" || c.pageID == "" {
  19. return errors.New("notion: missing API token or page ID")
  20. }
  21. apiEndpoint := "https://api.notion.com/v1/blocks/" + c.pageID + "/children"
  22. response, err := client.NewRequestBuilder(apiEndpoint).
  23. WithMethod(http.MethodPatch).
  24. WithJSON(&notionDocument{
  25. Children: []block{
  26. {
  27. Object: "block",
  28. Type: "bookmark",
  29. Bookmark: bookmarkObject{
  30. Caption: []any{},
  31. URL: entryURL,
  32. },
  33. },
  34. },
  35. }).
  36. WithHeader("Notion-Version", "2022-06-28").
  37. WithHeader("Authorization", "Bearer "+c.apiToken).
  38. Do()
  39. if err != nil {
  40. return fmt.Errorf("notion: %w", err)
  41. }
  42. defer response.Body.Close()
  43. if response.StatusCode != http.StatusOK {
  44. return fmt.Errorf("notion: unable to update document: url=%s status=%d", apiEndpoint, response.StatusCode)
  45. }
  46. return nil
  47. }
  48. type notionDocument struct {
  49. Children []block `json:"children"`
  50. }
  51. type block struct {
  52. Object string `json:"object"`
  53. Type string `json:"type"`
  54. Bookmark bookmarkObject `json:"bookmark"`
  55. }
  56. type bookmarkObject struct {
  57. Caption []any `json:"caption"`
  58. URL string `json:"url"`
  59. }