-
Notifications
You must be signed in to change notification settings - Fork 33
/
pocket.go
119 lines (103 loc) · 2.82 KB
/
pocket.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package pocket
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/ncarlier/readflow/pkg/config"
"github.com/ncarlier/readflow/pkg/constant"
"github.com/ncarlier/readflow/pkg/integration/webhook"
"github.com/ncarlier/readflow/pkg/model"
)
// pocketEntry is the structure definition of a Pocket entry
type pocketEntry struct {
Title string `json:"title,omitempty"`
URL *string `json:"url,omitempty"`
ConsumerKey string `json:"consumer_key"`
AccessToken string `json:"access_token"`
}
// pocketProviderConfig is the structure definition of a Pocket API configuration
type pocketProviderConfig struct {
Username string `json:"username"`
}
// pocketProvider is the structure definition of a Pocket webhook provider
type pocketProvider struct {
config pocketProviderConfig
consumerKey string
accessToken string
}
func newPocketProvider(srv model.OutgoingWebhook, conf config.Config) (webhook.Provider, error) {
cfg := pocketProviderConfig{}
if err := json.Unmarshal([]byte(srv.Config), &cfg); err != nil {
return nil, err
}
// Validate username
if cfg.Username == "" {
return nil, fmt.Errorf("missing username")
}
// Validate secrets
accessToken, ok := srv.Secrets["access_token"]
if !ok {
return nil, fmt.Errorf("missing access token")
}
provider := &pocketProvider{
config: cfg,
consumerKey: conf.Integration.Pocket.ConsumerKey,
accessToken: accessToken,
}
return provider, nil
}
// Send article to Pocket endpoint.
func (wp *pocketProvider) Send(ctx context.Context, article model.Article) (*webhook.Result, error) {
entry := pocketEntry{
Title: article.Title,
URL: article.URL,
ConsumerKey: wp.consumerKey,
AccessToken: wp.accessToken,
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(entry)
req, err := http.NewRequestWithContext(ctx, "POST", "https://getpocket.com/v3/add", b)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", constant.ContentTypeJSON)
client := constant.DefaultClient
if _, ok := ctx.Deadline(); ok {
client = &http.Client{}
}
resp, err := client.Do(req)
if err != nil || resp.StatusCode >= 300 {
if err == nil {
err = fmt.Errorf("bad status code: %d", resp.StatusCode)
}
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
obj := make(map[string]interface{})
if err := json.Unmarshal(body, &obj); err != nil {
return nil, nil
}
link := ""
if item, ok := obj["item"]; ok {
itemMap := item.(map[string]interface{})
link = fmt.Sprintf("https://getpocket.com/read/%s", itemMap["item_id"])
}
result := &webhook.Result{
URL: &link,
}
return result, nil
}
func init() {
webhook.Register("pocket", &webhook.Def{
Name: "Pocket",
Desc: "Send article(s) to Pocket instance.",
Create: newPocketProvider,
})
}