2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package espial // import "miniflux.app/v2/internal/integration/espial"
|
2022-04-21 04:44:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/http/client"
|
2023-08-13 04:01:22 +02:00
|
|
|
"miniflux.app/v2/internal/url"
|
2022-04-21 04:44:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Document structure of an Espial document
|
|
|
|
type Document struct {
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
ToRead bool `json:"toread,omitempty"`
|
|
|
|
Tags string `json:"tags,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client represents an Espial client.
|
|
|
|
type Client struct {
|
|
|
|
baseURL string
|
|
|
|
apiKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Espial client.
|
|
|
|
func NewClient(baseURL, apiKey string) *Client {
|
|
|
|
return &Client{baseURL: baseURL, apiKey: apiKey}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddEntry sends an entry to Espial.
|
|
|
|
func (c *Client) AddEntry(link, title, content, tags string) error {
|
|
|
|
if c.baseURL == "" || c.apiKey == "" {
|
2023-08-13 04:01:22 +02:00
|
|
|
return fmt.Errorf("espial: missing base URL or API key")
|
2022-04-21 04:44:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
doc := &Document{
|
|
|
|
Title: title,
|
|
|
|
Url: link,
|
|
|
|
ToRead: true,
|
|
|
|
Tags: tags,
|
|
|
|
}
|
|
|
|
|
2023-08-13 04:01:22 +02:00
|
|
|
apiEndpoint, err := url.JoinBaseURLAndPath(c.baseURL, "/api/add")
|
2022-04-21 04:44:47 +02:00
|
|
|
if err != nil {
|
2023-08-13 04:01:22 +02:00
|
|
|
return fmt.Errorf(`espial: invalid API endpoint: %v`, err)
|
2022-04-21 04:44:47 +02:00
|
|
|
}
|
|
|
|
|
2023-08-13 04:01:22 +02:00
|
|
|
clt := client.New(apiEndpoint)
|
2022-04-21 04:44:47 +02:00
|
|
|
clt.WithAuthorization("ApiKey " + c.apiKey)
|
|
|
|
response, err := clt.PostJSON(doc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("espial: unable to send entry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
|
|
|
return fmt.Errorf("espial: unable to send entry, status=%d", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|