forked from howeyc/pbullet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbullet.go
100 lines (86 loc) · 2.68 KB
/
pbullet.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
// Go library for the Push Bullet REST API
// More info: https://www.pushbullet.com/api
package pbullet
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
var pushUrl string
var getUrl string
// Extra device info returned by GetDevices API call.
type DeviceInfo struct {
Manufacturer string `json:"manufacturer"`
Model string `json:"model"`
AndroidVersion string `json:"android_version"`
SDKVersion string `json:"sdk_version"`
AppVersion int `json:"app_version"`
Nickname string `json:"nickname"`
}
// Device is the structure needed to push Notes/Addresses/Links/etc.
// Only need to populate Id field. Other fields are informational only.
type Device struct {
Id int64 `json:"id"`
DevInfo DeviceInfo `json:"extras"`
Owner string `json:"owner_name"`
}
// GetDevices returns two lists, owned devices and devices that are shared.
type DeviceList struct {
Devices []Device `json:"devices"`
SharedDevices []Device `json:"shared_devices"`
}
// Set the API key used for all Get and Push API calls.
func SetAPIKey(apiKey string) {
pUrl := url.URL{}
pUrl.Scheme = "https"
pUrl.User = url.UserPassword(apiKey, "")
pUrl.Host = "api.pushbullet.com"
pUrl.Path = "/api/pushes"
pushUrl = pUrl.String()
gUrl := url.URL{}
gUrl.Scheme = "https"
gUrl.User = url.UserPassword(apiKey, "")
gUrl.Host = "api.pushbullet.com"
gUrl.Path = "/api/devices"
getUrl = gUrl.String()
}
// Get devices configured on PushBullet
func GetDevices() (DeviceList, error) {
var devList DeviceList
resp, err := http.Get(getUrl)
if err != nil {
return devList, err
}
respBytes, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBytes, &devList)
return devList, err
}
// Push a note to a device.
func (pd *Device) PushNote(title, body string) (resp *http.Response, err error) {
pushVals := url.Values{}
pushVals.Set("device_id", strconv.FormatInt(pd.Id,10))
pushVals.Set("type", "note")
pushVals.Set("title", title)
pushVals.Set("body", body)
return http.PostForm(pushUrl, pushVals)
}
// Push an address to a device.
func (pd *Device) PushAddress(name, address string) (resp *http.Response, err error) {
pushVals := url.Values{}
pushVals.Set("device_id", strconv.FormatInt(pd.Id,10))
pushVals.Set("type", "note")
pushVals.Set("name", name)
pushVals.Set("address", address)
return http.PostForm(pushUrl, pushVals)
}
// Push a link to a device.
func (pd *Device) PushLink(title, urlAddress string) (resp *http.Response, err error) {
pushVals := url.Values{}
pushVals.Set("device_id", strconv.FormatInt(pd.Id,10))
pushVals.Set("type", "note")
pushVals.Set("title", title)
pushVals.Set("url", urlAddress)
return http.PostForm(pushUrl, pushVals)
}