notion.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package notion
  4. import (
  5. "fmt"
  6. "miniflux.app/v2/http/client"
  7. )
  8. // Client represents a Notion client.
  9. type Client struct {
  10. token string
  11. pageID string
  12. }
  13. // NewClient returns a new Notion client.
  14. func NewClient(token, pageID string) *Client {
  15. return &Client{token, pageID}
  16. }
  17. func (c *Client) AddEntry(entryURL string, entryTitle string) error {
  18. if c.token == "" || c.pageID == "" {
  19. return fmt.Errorf("notion: missing credentials")
  20. }
  21. clt := client.New("https://api.notion.com/v1/blocks/" + c.pageID + "/children")
  22. block := &Data{
  23. Children: []Block{
  24. {
  25. Object: "block",
  26. Type: "bookmark",
  27. Bookmark: Bookmark{
  28. Caption: []interface{}{},
  29. URL: entryURL,
  30. },
  31. },
  32. },
  33. }
  34. clt.WithAuthorization("Bearer " + c.token)
  35. customHeaders := map[string]string{
  36. "Notion-Version": "2022-06-28",
  37. }
  38. clt.WithCustomHeaders(customHeaders)
  39. response, error := clt.PatchJSON(block)
  40. if error != nil {
  41. return fmt.Errorf("notion: unable to patch entry: %v", error)
  42. }
  43. if response.HasServerFailure() {
  44. return fmt.Errorf("notion: request failed, status=%d", response.StatusCode)
  45. }
  46. return nil
  47. }