2
0
mirror of https://github.com/ViViDboarder/gopush.git synced 2024-11-22 06:06:35 +00:00
gopush/pushbullet/pushbullet.go

205 lines
4.0 KiB
Go
Raw Normal View History

2014-09-20 01:32:10 +00:00
package pushbullet
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
host = "https://api.pushbullet.com/v2"
)
func New(apiKey string) PushBullet {
return PushBullet{apiKey: apiKey}
}
type PushBullet struct {
apiKey string
client *http.Client
ActiveDevice Device
Devices []Device
}
// Loads all devices for account into instance
func (pb *PushBullet) LoadDevices() []Device {
dl := new(DeviceList)
err := pb.pbRequest("GET", host+"/devices", nil, dl)
if err != nil {
fmt.Println("error on pbRequest")
fmt.Println(err)
}
pb.Devices = dl.Devices
return dl.Devices
}
// Lazy loaded version of LoadDevices
func (pb *PushBullet) GetDevices() []Device {
if len(pb.Devices) == 0 {
pb.LoadDevices()
}
return pb.Devices
}
func (pb PushBullet) PushNote(message string) {
body := map[string]interface{}{
"type": "note",
"body": message,
}
pb.Push(body)
}
func (pb PushBullet) PushLink(title string, url string) {
pb.PushLinkWithBody(title, url, "")
}
func (pb PushBullet) PushLinkWithBody(title string, url string, messBody string) {
body := map[string]interface{}{
"type": "link",
"title": title,
"url": url,
"body": messBody,
}
pb.Push(body)
}
// Pushes a text message to active device
// TODO: handle response
func (pb PushBullet) Push(body map[string]interface{}) {
body["device_iden"] = pb.ActiveDevice.Iden
result := new(interface{})
pb.pbRequest("POST", host+"/pushes", body, result)
}
// Sets active device by Nickname or Iden
func (pb *PushBullet) SetActiveDevice(key string) {
if len(pb.Devices) == 0 {
pb.LoadDevices()
}
for _, d := range pb.Devices {
if d.Match(key) && d.CanPush() {
pb.ActiveDevice = d
}
}
}
// Method for communicating with PushBullet
2017-05-12 23:13:50 +00:00
func (pb *PushBullet) pbRequest(method string, url string, body map[string]interface{}, result interface{}) (err error) {
2014-09-20 01:32:10 +00:00
if pb.client == nil {
pb.client = &http.Client{}
}
2017-05-12 23:13:50 +00:00
var r *http.Request
if body == nil {
r, err = http.NewRequest(method, url, nil)
} else {
jsonBody, jsonError := json.Marshal(body)
if jsonError != nil {
fmt.Println("Error marshalling body: " + url)
return jsonError
}
r, err = http.NewRequest(method, url, bytes.NewBuffer(jsonBody))
2014-09-20 01:32:10 +00:00
}
if err != nil {
2017-05-12 23:13:50 +00:00
fmt.Println("Error on request create: " + url)
return
2014-09-20 01:32:10 +00:00
}
r.Header.Add("Content-Type", "application/json")
r.Header.Add("Application-Type", "application/json")
r.Header.Add("User-Agent", "gopush")
r.SetBasicAuth(pb.apiKey, "")
resp, err := pb.client.Do(r)
if err != nil {
2017-05-12 23:13:50 +00:00
fmt.Println("Error on request do: ")
2014-09-20 01:32:10 +00:00
fmt.Println(err)
2017-05-12 23:13:50 +00:00
return
2014-09-20 01:32:10 +00:00
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
2017-05-12 23:13:50 +00:00
fmt.Println("Error on response read: " + url)
return
2014-09-20 01:32:10 +00:00
}
2017-05-12 23:13:50 +00:00
2014-09-20 01:32:10 +00:00
err = json.Unmarshal(responseBody, result)
2017-05-12 23:13:50 +00:00
if err != nil {
fmt.Println("Error on response parse: " + url)
fmt.Println("json: " + string(responseBody))
return
}
2014-09-20 01:32:10 +00:00
return err
}
// Response types
type DeviceList struct {
Aliases []interface{}
Clients []interface{}
Devices []Device
Grants []interface{}
Pushes []interface{}
}
type DeviceFingerprint struct {
mac_address string
android_id string
}
type Device struct {
Iden string
Created float32
Nickname string
Modified float32
Push_token string
Fingerprint string
Active bool
Model string
App_version int32
Type string
Kind string
Pushable bool
Manufacturer string
}
// Returns Android Fingerprint
func (d Device) AndroidFingerprint() (DeviceFingerprint, error) {
fp := new(DeviceFingerprint)
err := json.Unmarshal([]byte(d.Fingerprint), fp)
return *fp, err
}
// Returns if the device is available for pushing
func (d Device) CanPush() bool {
return d.Active && d.Pushable
}
// Outputs the device in a friendly string format
func (d Device) Format() string {
return fmt.Sprintf("%s (%s)", d.Nickname, d.Kind)
}
// Returns true if key matches Nickname or Iden
func (d Device) Match(key string) bool {
if d.Nickname == key || d.Iden == key {
return true
}
return false
}