aboutsummaryrefslogtreecommitdiffhomepage
path: root/integration/pocket/pocket.go
blob: 6f1b197c376ec4abcdba8f1fb04931e6f5c62f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2018 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 pocket // import "miniflux.app/integration/pocket"

import (
	"fmt"

	"miniflux.app/http/client"
)

// Client represents a Pocket client.
type Client struct {
	consumerKey string
	accessToken string
}

// 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")
	}

	type body struct {
		AccessToken string `json:"access_token"`
		ConsumerKey string `json:"consumer_key"`
		Title       string `json:"title,omitempty"`
		URL         string `json:"url"`
	}

	data := &body{
		AccessToken: c.accessToken,
		ConsumerKey: c.consumerKey,
		Title:       title,
		URL:         link,
	}

	clt := client.New("https://getpocket.com/v3/add")
	response, err := clt.PostJSON(data)
	if err != nil {
		return fmt.Errorf("pocket: unable to send url: %v", err)
	}

	if response.HasServerFailure() {
		return fmt.Errorf("pocket: unable to send url, status=%d", response.StatusCode)
	}

	return nil
}

// NewClient returns a new Pocket client.
func NewClient(consumerKey, accessToken string) *Client {
	return &Client{consumerKey, accessToken}
}