diff options
author | 2023-08-13 21:58:45 -0700 | |
---|---|---|
committer | 2023-08-14 21:49:02 -0700 | |
commit | 5e520ca5bf06db39592728955d8e31773d5b7c4a (patch) | |
tree | b14a4ecca12d092b9e85d2d3ea17622aa158c56d /internal/integration/notion/notion.go | |
parent | e5d9f2f5a05453043ed29c79f3f1a8865f26d99d (diff) | |
download | v2-5e520ca5bf06db39592728955d8e31773d5b7c4a.tar.gz v2-5e520ca5bf06db39592728955d8e31773d5b7c4a.tar.zst v2-5e520ca5bf06db39592728955d8e31773d5b7c4a.zip |
Use stdlib HTTP client for third-party integrations
Diffstat (limited to 'internal/integration/notion/notion.go')
-rw-r--r-- | internal/integration/notion/notion.go | 80 |
1 files changed, 56 insertions, 24 deletions
diff --git a/internal/integration/notion/notion.go b/internal/integration/notion/notion.go index fe579928..7026877e 100644 --- a/internal/integration/notion/notion.go +++ b/internal/integration/notion/notion.go @@ -4,51 +4,83 @@ package notion import ( + "bytes" + "encoding/json" "fmt" + "net/http" + "time" - "miniflux.app/v2/internal/http/client" + "miniflux.app/v2/internal/version" ) -// Client represents a Notion client. +const defaultClientTimeout = 10 * time.Second + type Client struct { - token string - pageID string + apiToken string + pageID string } -// NewClient returns a new Notion client. -func NewClient(token, pageID string) *Client { - return &Client{token, pageID} +func NewClient(apiToken, pageID string) *Client { + return &Client{apiToken, pageID} } -func (c *Client) AddEntry(entryURL string, entryTitle string) error { - if c.token == "" || c.pageID == "" { - return fmt.Errorf("notion: missing credentials") +func (c *Client) UpdateDocument(entryURL string, entryTitle string) error { + if c.apiToken == "" || c.pageID == "" { + return fmt.Errorf("notion: missing API token or page ID") } - clt := client.New("https://api.notion.com/v1/blocks/" + c.pageID + "/children") - block := &Data{ - Children: []Block{ + + apiEndpoint := "https://api.notion.com/v1/blocks/" + c.pageID + "/children" + requestBody, err := json.Marshal(¬ionDocument{ + Children: []block{ { Object: "block", Type: "bookmark", - Bookmark: Bookmark{ - Caption: []interface{}{}, + Bookmark: bookmarkObject{ + Caption: []any{}, URL: entryURL, }, }, }, + }) + if err != nil { + return fmt.Errorf("notion: unable to encode request body: %v", err) } - clt.WithAuthorization("Bearer " + c.token) - customHeaders := map[string]string{ - "Notion-Version": "2022-06-28", + + request, err := http.NewRequest(http.MethodPatch, apiEndpoint, bytes.NewReader(requestBody)) + if err != nil { + return fmt.Errorf("notion: unable to create request: %v", err) } - clt.WithCustomHeaders(customHeaders) - response, error := clt.PatchJSON(block) - if error != nil { - return fmt.Errorf("notion: unable to patch entry: %v", error) + + 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) } + defer response.Body.Close() - if response.HasServerFailure() { - return fmt.Errorf("notion: request failed, status=%d", response.StatusCode) + if response.StatusCode != http.StatusOK { + return fmt.Errorf("notion: unable to update document: url=%s status=%d", apiEndpoint, response.StatusCode) } + return nil } + +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"` +} |