forked from mitsuse/pushbullet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushes.go
120 lines (96 loc) · 2.72 KB
/
pushes.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
package pushbullet
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"github.com/Cruiser79/pushbullet-go/requests"
"github.com/Cruiser79/pushbullet-go/responses"
)
// Push a note, which consists of "title" and "message" strings.
func (pb *Pushbullet) PostPushesNote(n *requests.Note) (*responses.Note, error) {
res, err := pb.postPushes(n)
if err != nil {
return nil, err
}
var noteRes *responses.Note
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(¬eRes); err != nil {
return nil, err
}
return noteRes, err
}
// Push a link, which consists of "title", "message" and "url" strings.
func (pb *Pushbullet) PostPushesLink(l *requests.Link) (*responses.Link, error) {
res, err := pb.postPushes(l)
if err != nil {
return nil, err
}
var linkRes *responses.Link
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&linkRes); err != nil {
return nil, err
}
return linkRes, err
}
// Push an address, which consists of the place "name" and "address (searchquery)" for map.
func (pb *Pushbullet) PostPushesAddress(a *requests.Address) (*responses.Address, error) {
res, err := pb.postPushes(a)
if err != nil {
return nil, err
}
var addressRes *responses.Address
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&addressRes); err != nil {
return nil, err
}
return addressRes, err
}
// Push a checklist, which consists of "title" and the list of items.
func (pb *Pushbullet) PostPushesChecklist(c *requests.Checklist) (*responses.Checklist, error) {
res, err := pb.postPushes(c)
if err != nil {
return nil, err
}
var checkRes *responses.Checklist
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&checkRes); err != nil {
return nil, err
}
return checkRes, err
}
// Push a file, which consists of "title", "message" and the information of uploaded file.
func (pb *Pushbullet) PostPushesFile(f *requests.File) (*responses.File, error) {
res, err := pb.postPushes(f)
if err != nil {
return nil, err
}
var fileRes *responses.File
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&fileRes); err != nil {
return nil, err
}
return fileRes, err
}
func (pb *Pushbullet) postPushes(p interface{}) (*http.Response, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
if err := encoder.Encode(p); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", ENDPOINT_PUSHES, buffer)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(pb.token, "")
res, err := pb.client.Do(req)
if err != nil {
return nil, err
}
// TODO: Return an error value with human friendly message.
if res.StatusCode != 200 {
return nil, errors.New(res.Status)
}
return res, nil
}