-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocket.go
208 lines (167 loc) · 5.59 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package go_pocket_sdk
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/tidwall/gjson"
)
const (
host = "https://getpocket.com/v3"
authorizeURL = "https://getpocket.com/auth/authorize?request_token=%s&redirect_uri=%s"
endpointAdd = "/add"
endpointModify = "/send"
endpointRetrieving = "/get"
endpointRequestToken = "/oauth/request"
endpointRequestAuthorize = "/oauth/authorize"
xErrorHeader = "X-Error"
xErrorCodeHeader = "X-Error-Code"
defaultTimeout = time.Second * 10
)
// Client is a getpocket API client
type Client struct {
client *http.Client
consumerKey string
redirectURL string
}
// NewClient creates a new client with your application key (to generate a key, create your application here: https://getpocket.com/developer/apps)
func NewClient(consumerKey string) (*Client, error) {
if consumerKey == "" {
return nil, ErrEmptyConsumerKey
}
return &Client{
client: &http.Client{
Timeout: defaultTimeout,
},
consumerKey: consumerKey,
}, nil
}
// Add creates a new item in the Pocket list
func (c *Client) Add(ctx context.Context, input AddInput) error {
req, err := input.generateRequest(c.consumerKey)
if err != nil {
return err
}
_, err = c.doHTTP(ctx, endpointAdd, req)
return err
}
// Modify modifies Pocket user data (archives items, adds tags to an item, marks an item as a favorite, etc).
func (c *Client) Modify(ctx context.Context, input ModifyInput) error {
req, err := input.generateRequest(c.consumerKey)
if err != nil {
return err
}
_, err = c.doHTTP(ctx, endpointModify, req)
return err
}
// Retrieving retrieves user data (items) Pocket, such as the item id, which is needed to modify items in the Modify function
func (c *Client) Retrieving(ctx context.Context, input RetrievingInput) ([]Item, error) {
req, err := input.generateRequest(c.consumerKey)
if err != nil {
return nil, err
}
result, err := c.doHTTP(ctx, endpointRetrieving, req)
if err != nil {
return nil, err
}
return c.parseItems(result), nil
}
func (c *Client) parseItems(result gjson.Result) []Item {
var items []Item
for itemID := range result.Get("list").Map() {
item := Item{ID: itemID}
item.fillAllFields(result)
items = append(items, item)
}
return items
}
// Authorize returns the Authorization structure with the access token, username and state obtained from the authorization request
func (c *Client) Authorize(ctx context.Context, requestToken string) (Authorization, error) {
if requestToken == "" {
return Authorization{}, ErrEmptyRequestToken
}
body := requestAuthorization{
ConsumerKey: c.consumerKey,
Code: requestToken,
}
result, err := c.doHTTP(ctx, endpointRequestAuthorize, body)
if err != nil {
return Authorization{}, err
}
accessToken := result.Get("access_token").String()
username := result.Get("username").String()
state := result.Get("state").String()
if accessToken == "" {
return Authorization{}, ErrEmptyAccessToken
}
return Authorization{
AccessToken: accessToken,
Username: username,
State: state,
}, nil
}
// GetAuthorizationURL returns the url string that is used to grant the user access rights to his Pocket account in your application
func (c Client) GetAuthorizationURL(requestToken string) (string, error) {
if requestToken == "" {
return "", ErrEmptyRequestToken
}
if c.redirectURL == "" {
return "", ErrEmptyRedirectURL
}
return fmt.Sprintf(authorizeURL, requestToken, c.redirectURL), nil
}
// GetRequestToken returns the request token (code), which will be used later to authenticate the user in your application.
// RedirectURL - where the user will be redirected after authorization (better to specify a link to your application),
// State - metadata string that will be returned at each subsequent authentication response (if you don't need it, specify an empty string).
func (c *Client) GetRequestToken(ctx context.Context, redirectURL string, state string) (string, error) {
if redirectURL == "" {
return "", ErrEmptyRedirectURL
}
c.redirectURL = redirectURL
body := requestToken{
ConsumerKey: c.consumerKey,
RedirectURL: redirectURL,
State: state,
}
result, err := c.doHTTP(ctx, endpointRequestToken, body)
if err != nil {
return "", err
}
token := result.Get("code").String()
if token == "" {
return "", ErrEmptyRequestTokenInResponse
}
return token, nil
}
func (c *Client) doHTTP(ctx context.Context, endpoint string, body interface{}) (gjson.Result, error) {
b, err := json.Marshal(body)
if err != nil {
return gjson.Result{}, fmt.Errorf("error occurred when marshal the input body: %s", err.Error())
}
req, err := http.NewRequestWithContext(ctx, "POST", host+endpoint, bytes.NewBufferString(string(b)))
if err != nil {
return gjson.Result{}, fmt.Errorf("error occurred when creating the query: %s", err.Error())
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req.Header.Set("X-Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return gjson.Result{}, fmt.Errorf("error occurred when sending a request to the Pocket server: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return gjson.Result{}, fmt.Errorf("API error %s: %s", resp.Header.Get(xErrorCodeHeader), resp.Header.Get(xErrorHeader))
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return gjson.Result{}, fmt.Errorf("error occurred when reading the request body: %s", err.Error())
}
result := gjson.Parse(string(respBody))
if result.String() == "" {
return gjson.Result{}, ErrFailedToParseInputBody
}
return result, nil
}