2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-05-23 17:53:06 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package linkding // import "miniflux.app/v2/internal/integration/linkding"
|
2022-05-23 17:53:06 +02:00
|
|
|
|
|
|
|
import (
|
2023-08-14 06:58:45 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-05-23 17:53:06 +02:00
|
|
|
"fmt"
|
2023-08-14 06:58:45 +02:00
|
|
|
"net/http"
|
2023-05-21 16:29:51 +02:00
|
|
|
"strings"
|
2023-08-14 06:58:45 +02:00
|
|
|
"time"
|
2022-05-23 17:53:06 +02: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"
|
2022-05-23 17:53:06 +02:00
|
|
|
)
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
const defaultClientTimeout = 10 * time.Second
|
2022-05-23 17:53:06 +02:00
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
baseURL string
|
|
|
|
apiKey string
|
2023-05-21 16:29:51 +02:00
|
|
|
tags string
|
2023-06-24 17:08:58 +02:00
|
|
|
unread bool
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
|
|
|
|
2023-06-24 17:08:58 +02:00
|
|
|
func NewClient(baseURL, apiKey, tags string, unread bool) *Client {
|
|
|
|
return &Client{baseURL: baseURL, apiKey: apiKey, tags: tags, unread: unread}
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
|
2022-05-23 17:53:06 +02:00
|
|
|
if c.baseURL == "" || c.apiKey == "" {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("linkding: missing base URL or API key")
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 16:29:51 +02:00
|
|
|
tagsSplitFn := func(c rune) bool {
|
|
|
|
return c == ',' || c == ' '
|
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/api/bookmarks/")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`linkding: invalid API endpoint: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody, err := json.Marshal(&linkdingBookmark{
|
2023-08-13 04:01:22 +02:00
|
|
|
Url: entryURL,
|
2023-08-14 06:58:45 +02:00
|
|
|
Title: entryTitle,
|
2023-05-21 16:29:51 +02:00
|
|
|
TagNames: strings.FieldsFunc(c.tags, tagsSplitFn),
|
2023-06-24 17:08:58 +02:00
|
|
|
Unread: c.unread,
|
2023-08-14 06:58:45 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("linkding: unable to encode request body: %v", err)
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
request, err := http.NewRequest(http.MethodPost, apiEndpoint, bytes.NewReader(requestBody))
|
2022-05-23 17:53:06 +02:00
|
|
|
if err != nil {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("linkding: unable to create request: %v", err)
|
2022-05-23 17:53:06 +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("Authorization", "Token "+c.apiKey)
|
|
|
|
|
|
|
|
httpClient := &http.Client{Timeout: defaultClientTimeout}
|
|
|
|
response, err := httpClient.Do(request)
|
2022-05-23 17:53:06 +02:00
|
|
|
if err != nil {
|
2023-08-14 06:58:45 +02:00
|
|
|
return fmt.Errorf("linkding: unable to send request: %v", err)
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
defer response.Body.Close()
|
2022-05-23 17:53:06 +02:00
|
|
|
|
2023-08-14 06:58:45 +02:00
|
|
|
if response.StatusCode >= 400 {
|
|
|
|
return fmt.Errorf("linkding: unable to create bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-14 06:58:45 +02:00
|
|
|
|
|
|
|
type linkdingBookmark struct {
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
TagNames []string `json:"tag_names,omitempty"`
|
|
|
|
Unread bool `json:"unread,omitempty"`
|
|
|
|
}
|