-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocket.go
223 lines (180 loc) · 6.08 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package go_pocket_sdk
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/valyala/fastjson"
)
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
}
// 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, errors.New("empty consumer key")
}
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) {
var items []Item
req, err := input.generateRequest(c.consumerKey)
if err != nil {
return items, err
}
values, err := c.doHTTP(ctx, endpointRetrieving, req)
if err != nil {
return items, err
}
if values.GetObject("list") == nil {
return items, nil
}
var itemId string
var index int
newJsonStr := values.GetObject("list").String()
for index != -1 {
index = strings.Index(newJsonStr, "{\"item_id\":\"")
if index != -1 {
for i := index + 12; string(newJsonStr[i]) != "\""; i++ {
itemId += string(newJsonStr[i])
}
items = append(items, createItem(itemId, values))
itemId = ""
oldJsonStr := newJsonStr
newJsonStr = ``
for i := index + 12; i != len(oldJsonStr); i++ {
newJsonStr += string(oldJsonStr[i])
}
}
}
return items, nil
}
// Authorize returns the Authorization structure with the access token, user name and state obtained from the authorization request
func (c *Client) Authorize(ctx context.Context, requestToken string) (Authorization, error) {
if requestToken == "" {
return Authorization{}, errors.New("empty request token")
}
body := requestAuthorization{
ConsumerKey: c.consumerKey,
Code: requestToken,
}
values, err := c.doHTTP(ctx, endpointRequestAuthorize, body)
if err != nil {
return Authorization{}, err
}
accessToken := values.GetStringBytes("access_token")
username := values.GetStringBytes("username")
state := values.GetStringBytes("state")
if string(accessToken) == "" {
return Authorization{}, errors.New("empty access token in API response")
}
return Authorization{
AccessToken: string(accessToken),
Username: string(username),
State: string(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, redirectURL string) (string, error) {
if requestToken == "" {
return "", errors.New("empty request token")
}
if redirectURL == "" {
return "", errors.New("empty redirection URL")
}
return fmt.Sprintf(authorizeUrl, requestToken, 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 "", errors.New("empty redirect URL")
}
body := requestToken{
ConsumerKey: c.consumerKey,
RedirectURL: redirectURL,
State: state,
}
values, err := c.doHTTP(ctx, endpointRequestToken, body)
if err != nil {
return "", err
}
token := values.GetStringBytes("code")
if string(token) == "" {
return "", errors.New("empty request token in API response")
}
return string(token), nil
}
func (c *Client) doHTTP(ctx context.Context, endpoint string, body interface{}) (*fastjson.Value, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("an 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 nil, fmt.Errorf("an 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 nil, fmt.Errorf("an error occurred when sending a request to the Pocket server: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API error %s: %s", resp.Header.Get(xErrorCodeHeader), resp.Header.Get(xErrorHeader))
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("an error occurred when reading the request body: %s", err.Error())
}
values, err := fastjson.Parse(string(respBody))
if err != nil {
return nil, fmt.Errorf("an error occurred while parsing the response body: %s", err.Error())
}
return values, nil
}