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 (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2023-05-21 16:29:51 +02:00
|
|
|
"strings"
|
2022-05-23 17:53:06 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/http/client"
|
2022-05-23 17:53:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Document structure of a Linkding document
|
|
|
|
type Document struct {
|
2023-05-21 16:29:51 +02:00
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
TagNames []string `json:"tag_names,omitempty"`
|
2023-06-24 17:08:58 +02:00
|
|
|
Unread bool `json:"unread,omitempty"`
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client represents an Linkding client.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Linkding client.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// AddEntry sends an entry to Linkding.
|
|
|
|
func (c *Client) AddEntry(title, url string) error {
|
|
|
|
if c.baseURL == "" || c.apiKey == "" {
|
|
|
|
return fmt.Errorf("linkding: missing credentials")
|
|
|
|
}
|
|
|
|
|
2023-05-21 16:29:51 +02:00
|
|
|
tagsSplitFn := func(c rune) bool {
|
|
|
|
return c == ',' || c == ' '
|
|
|
|
}
|
|
|
|
|
2022-05-23 17:53:06 +02:00
|
|
|
doc := &Document{
|
2023-05-21 16:29:51 +02:00
|
|
|
Url: url,
|
|
|
|
Title: title,
|
|
|
|
TagNames: strings.FieldsFunc(c.tags, tagsSplitFn),
|
2023-06-24 17:08:58 +02:00
|
|
|
Unread: c.unread,
|
2022-05-23 17:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
apiURL, err := getAPIEndpoint(c.baseURL, "/api/bookmarks/")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clt := client.New(apiURL)
|
|
|
|
clt.WithAuthorization("Token " + c.apiKey)
|
|
|
|
response, err := clt.PostJSON(doc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("linkding: unable to send entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
|
|
|
return fmt.Errorf("linkding: unable to send entry, status=%d", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIEndpoint(baseURL, pathURL string) (string, error) {
|
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("linkding: invalid API endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
relative, err := url.Parse(pathURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("linkding: invalid API endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u = u.ResolveReference(relative)
|
|
|
|
return u.String(), nil
|
|
|
|
}
|