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 (
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/http/client"
|
2023-08-14 04:09:01 +02:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2018-02-25 20:49:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Document structure of a Nununx Keeper document
|
|
|
|
type Document struct {
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Origin string `json:"origin,omitempty"`
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
ContentType string `json:"contentType,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client represents an Nunux Keeper client.
|
|
|
|
type Client struct {
|
|
|
|
baseURL string
|
|
|
|
apiKey string
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
// NewClient returns a new Nunux Keeepr client.
|
|
|
|
func NewClient(baseURL, apiKey string) *Client {
|
|
|
|
return &Client{baseURL: baseURL, apiKey: apiKey}
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:49:08 +01:00
|
|
|
// AddEntry sends an entry to Nunux Keeper.
|
|
|
|
func (c *Client) AddEntry(link, title, content string) error {
|
2018-04-30 02:58:09 +02:00
|
|
|
if c.baseURL == "" || c.apiKey == "" {
|
|
|
|
return fmt.Errorf("nunux-keeper: missing credentials")
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:49:08 +01:00
|
|
|
doc := &Document{
|
|
|
|
Title: title,
|
|
|
|
Origin: link,
|
|
|
|
Content: content,
|
|
|
|
ContentType: "text/html",
|
|
|
|
}
|
|
|
|
|
2023-08-14 04:09:01 +02:00
|
|
|
apiEndpoint, err := urllib.JoinBaseURLAndPath(c.baseURL, "/v2/documents")
|
2018-02-25 20:49:08 +01:00
|
|
|
if err != nil {
|
2023-08-13 04:01:22 +02:00
|
|
|
return fmt.Errorf(`nunux-keeper: invalid API endpoint: %v`, err)
|
2018-02-25 20:49:08 +01:00
|
|
|
}
|
2018-04-28 19:51:07 +02:00
|
|
|
|
2023-08-13 04:01:22 +02:00
|
|
|
clt := client.New(apiEndpoint)
|
2018-04-28 19:51:07 +02:00
|
|
|
clt.WithCredentials("api", c.apiKey)
|
|
|
|
response, err := clt.PostJSON(doc)
|
2018-05-21 21:24:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("nunux-keeper: unable to send entry: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:49:08 +01:00
|
|
|
if response.HasServerFailure() {
|
|
|
|
return fmt.Errorf("nunux-keeper: unable to send entry, status=%d", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:24:48 +02:00
|
|
|
return nil
|
2018-02-25 20:49:08 +01:00
|
|
|
}
|