2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-02-25 20:49:08 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package nunuxkeeper // import "miniflux.app/v2/internal/integration/nunuxkeeper"
|
2018-02-25 20:49:08 +01:00
|
|
|
|
|
|
|
import (
|
2023-08-14 06:58:45 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-02-25 20:49:08 +01:00
|
|
|
"fmt"
|
2023-08-14 06:58:45 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2018-02-25 20:49:08 +01:00
|
|
|
|
2023-08-14 04:09:01 +02:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2023-08-14 06:58:45 +02:00
|
|
|
"miniflux.app/v2/internal/version"
|
2018-02-25 20:49:08 +01:00
|
|
|
)
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
const defaultClientTimeout = 10 * time.Second
|
2018-02-25 20:49:08 +01:00
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
baseURL string
|
|
|
|
apiKey string
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
func NewClient(baseURL, apiKey string) *Client {
|
|
|
|
return &Client{baseURL: baseURL, apiKey: apiKey}
|
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
func (c *Client) AddEntry(entryURL, entryTitle, entryContent string) error {
|
2018-04-30 02:58:09 +02:00
|
|
|
if c.baseURL == "" || c.apiKey == "" {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("nunux-keeper: missing base URL or API key")
|
|
|
|
}
|
|
|
|
|
|
|
|
apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/v2/documents")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`nunux-keeper: invalid API endpoint: %v`, err)
|
2018-04-30 02:58:09 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
requestBody, err := json.Marshal(&nunuxKeeperDocument{
|
|
|
|
Title: entryTitle,
|
|
|
|
Origin: entryURL,
|
|
|
|
Content: entryContent,
|
2018-02-25 20:49:08 +01:00
|
|
|
ContentType: "text/html",
|
2023-08-14 06:58:45 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notion: unable to encode request body: %v", err)
|
2018-02-25 20:49:08 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
|
2018-02-25 20:49:08 +01:00
|
|
|
if err != nil {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("nunux-keeper: unable to create request: %v", err)
|
2018-02-25 20:49:08 +01:00
|
|
|
}
|
2018-04-28 19:51:07 +02:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
request.SetBasicAuth("api", c.apiKey)
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
request.Header.Set("User-Agent", "Miniflux/"+version.Version)
|
|
|
|
|
|
|
|
httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
|
response, err := httpClient.Do(request)
|
2018-05-21 21:24:48 +02:00
|
|
|
if err != nil {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("nunux-keeper: unable to send request: %v", err)
|
2018-05-21 21:24:48 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
defer response.Body.Close()
|
2018-05-21 21:24:48 +02:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("nunux-keeper: unable to create document: url=%s status=%d", apiEndpoint, response.StatusCode)
|
2018-02-25 20:49:08 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:24:48 +02:00
|
|
|
return nil
|
2018-02-25 20:49:08 +01:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
|
|
|
|
type nunuxKeeperDocument struct {
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Origin string `json:"origin,omitempty"`
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
ContentType string `json:"contentType,omitempty"`
|
|
|
|
}
|