forked from mitsuse/pushbullet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices.go
42 lines (32 loc) · 811 Bytes
/
devices.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
package pushbullet
import (
"encoding/json"
"errors"
"net/http"
"github.com/Cruiser79/pushbullet-go/responses"
)
// Get the devices thath can be pushed to.
func (pb *Pushbullet) GetDevices() ([]*responses.Device, error) {
req, err := http.NewRequest("GET", ENDPOINT_DEVICES, nil)
if err != nil {
return nil, err
}
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)
}
var devices *devicesResponse
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&devices); err != nil {
return nil, err
}
return devices.Devices, nil
}
type devicesResponse struct {
Devices []*responses.Device `json:"devices"`
}