From 34ce1142313b74e21a74c08c6dbd8d4312d7e336 Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Sun, 25 Feb 2018 19:49:08 +0000 Subject: Add Nunux Keeper integration --- integration/integration.go | 12 +++++++ integration/nunuxkeeper/nunuxkeeper.go | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 integration/nunuxkeeper/nunuxkeeper.go (limited to 'integration') diff --git a/integration/integration.go b/integration/integration.go index 1468a2b..05c7794 100644 --- a/integration/integration.go +++ b/integration/integration.go @@ -6,6 +6,7 @@ package integration import ( "github.com/miniflux/miniflux/integration/instapaper" + "github.com/miniflux/miniflux/integration/nunuxkeeper" "github.com/miniflux/miniflux/integration/pinboard" "github.com/miniflux/miniflux/integration/wallabag" "github.com/miniflux/miniflux/logger" @@ -48,4 +49,15 @@ func SendEntry(entry *model.Entry, integration *model.Integration) { logger.Error("[Integration] %v", err) } } + + if integration.NunuxKeeperEnabled { + client := nunuxkeeper.NewClient( + integration.NunuxKeeperURL, + integration.NunuxKeeperAPIKey, + ) + + if err := client.AddEntry(entry.URL, entry.Title, entry.Content); err != nil { + logger.Error("[Integration] %v", err) + } + } } diff --git a/integration/nunuxkeeper/nunuxkeeper.go b/integration/nunuxkeeper/nunuxkeeper.go new file mode 100644 index 0000000..4627e8d --- /dev/null +++ b/integration/nunuxkeeper/nunuxkeeper.go @@ -0,0 +1,63 @@ +// 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. + +package nunuxkeeper + +import ( + "fmt" + "net/url" + "path" + + "github.com/miniflux/miniflux/http" +) + +// 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 +} + +// AddEntry sends an entry to Nunux Keeper. +func (c *Client) AddEntry(link, title, content string) error { + doc := &Document{ + Title: title, + Origin: link, + Content: content, + ContentType: "text/html", + } + + apiURL, err := getAPIEndpoint(c.baseURL, "/v2/documents") + if err != nil { + return err + } + client := http.NewClientWithCredentials(apiURL, "api", c.apiKey) + response, err := client.PostJSON(doc) + if response.HasServerFailure() { + return fmt.Errorf("nunux-keeper: unable to send entry, status=%d", response.StatusCode) + } + + return err +} + +// NewClient returns a new Nunux Keeepr client. +func NewClient(baseURL, apiKey string) *Client { + return &Client{baseURL: baseURL, apiKey: apiKey} +} + +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 +} -- cgit v1.2.3