/
add.go
83 lines (66 loc) · 1.48 KB
/
add.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
package commands
import (
"encoding/json"
"net/url"
"github.com/Shaked/getpocket/auth"
"github.com/Shaked/getpocket/utils"
)
//@see http://getpocket.com/developer/docs/v3/add
type Add struct {
command
URL string
title string
tags string
tweet_id string
}
type AddResponse struct {
Item Item
Status int
}
func NewAdd(consumerKey string, request utils.HttpRequest, targetURL string) *Add {
a := &Add{
URL: targetURL,
}
a.SetConsumerKey(consumerKey)
a.SetRequest(request)
return a
}
// This can be included for cases where an item does not have a title, which is typical for image or PDF URLs.
// If Pocket detects a title from the content of the page, this parameter will be ignored.
func (a *Add) SetTitle(title string) *Add {
a.title = title
return a
}
func (a *Add) SetTags(tags string) *Add {
a.tags = tags
return a
}
func (a *Add) SetTweetID(tweet_id string) *Add {
a.tweet_id = tweet_id
return a
}
func (a *Add) Exec(user auth.Authenticated) (*AddResponse, error) {
u := url.Values{}
u.Add("url", a.URL)
u.Add("consumer_key", a.consumerKey)
u.Add("access_token", user.AccessToken())
if "" != a.title {
u.Add("title", a.title)
}
if "" != a.tags {
u.Add("tags", a.tags)
}
if "" != a.tweet_id {
u.Add("tweet_id", a.tweet_id)
}
body, err := a.request.Post(URLs["Add"], u)
if nil != err {
return nil, err
}
resp := &AddResponse{}
e := json.Unmarshal(fixJSONArrayToObject(body), resp)
if nil != e {
return nil, e
}
return resp, nil
}