2018-05-21 00:29:14 +02:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
2018-05-20 22:31:56 +02:00
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package pocket // import "miniflux.app/integration/pocket"
|
2018-05-20 22:31:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/client"
|
2018-05-20 22:31:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client represents a Pocket client.
|
|
|
|
type Client struct {
|
|
|
|
consumerKey string
|
2018-05-21 21:19:56 +02:00
|
|
|
accessToken string
|
2018-05-20 22:31:56 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
// NewClient returns a new Pocket client.
|
|
|
|
func NewClient(consumerKey, accessToken string) *Client {
|
|
|
|
return &Client{consumerKey, accessToken}
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:31:56 +02:00
|
|
|
// AddURL sends a single link to Pocket.
|
|
|
|
func (c *Client) AddURL(link, title string) error {
|
|
|
|
if c.consumerKey == "" || c.accessToken == "" {
|
|
|
|
return fmt.Errorf("pocket: missing credentials")
|
|
|
|
}
|
|
|
|
|
2018-05-21 00:29:14 +02:00
|
|
|
type body struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
ConsumerKey string `json:"consumer_key"`
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &body{
|
2018-05-20 22:31:56 +02:00
|
|
|
AccessToken: c.accessToken,
|
|
|
|
ConsumerKey: c.consumerKey,
|
|
|
|
Title: title,
|
|
|
|
URL: link,
|
|
|
|
}
|
|
|
|
|
|
|
|
clt := client.New("https://getpocket.com/v3/add")
|
2018-05-21 00:29:14 +02:00
|
|
|
response, err := clt.PostJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pocket: unable to send url: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:31:56 +02:00
|
|
|
if response.HasServerFailure() {
|
|
|
|
return fmt.Errorf("pocket: unable to send url, status=%d", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
2018-05-21 00:29:14 +02:00
|
|
|
return nil
|
2018-05-20 22:31:56 +02:00
|
|
|
}
|