2018-02-25 14:49:08 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package nunuxkeeper // import "miniflux.app/integration/nunuxkeeper"
|
2018-02-25 14:49:08 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/http/client"
|
2018-02-25 14:49:08 -05: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-07 23:28:41 -04:00
|
|
|
// NewClient returns a new Nunux Keeepr client.
|
|
|
|
func NewClient(baseURL, apiKey string) *Client {
|
|
|
|
return &Client{baseURL: baseURL, apiKey: apiKey}
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:49:08 -05:00
|
|
|
// AddEntry sends an entry to Nunux Keeper.
|
|
|
|
func (c *Client) AddEntry(link, title, content string) error {
|
2018-04-29 20:58:09 -04:00
|
|
|
if c.baseURL == "" || c.apiKey == "" {
|
|
|
|
return fmt.Errorf("nunux-keeper: missing credentials")
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:49:08 -05:00
|
|
|
doc := &Document{
|
|
|
|
Title: title,
|
|
|
|
Origin: link,
|
|
|
|
Content: content,
|
|
|
|
ContentType: "text/html",
|
|
|
|
}
|
|
|
|
|
|
|
|
apiURL, err := getAPIEndpoint(c.baseURL, "/v2/documents")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-28 13:51:07 -04:00
|
|
|
|
|
|
|
clt := client.New(apiURL)
|
|
|
|
clt.WithCredentials("api", c.apiKey)
|
|
|
|
response, err := clt.PostJSON(doc)
|
2018-05-21 15:24:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("nunux-keeper: unable to send entry: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:49:08 -05:00
|
|
|
if response.HasServerFailure() {
|
|
|
|
return fmt.Errorf("nunux-keeper: unable to send entry, status=%d", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:24:48 -04:00
|
|
|
return nil
|
2018-02-25 14:49:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIEndpoint(baseURL, pathURL string) (string, error) {
|
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("nunux-keeper: invalid API endpoint: %v", err)
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, pathURL)
|
|
|
|
return u.String(), nil
|
|
|
|
}
|