Skip to content

Commit

Permalink
Add GetDevices, and test
Browse files Browse the repository at this point in the history
  • Loading branch information
kariudo committed Feb 4, 2015
1 parent 6d5bba2 commit 0e3977b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ primary feature development still in progress, see below for current status:

### Done :
* Get User
* Pushes
* Send Pushes
* Note
* Link
* Address
* Checklist
* File
* Get Devices

### Todo :
* Set User preferences
* Devices
* Contacts
* Subscriptions
* Web Sockets
Expand Down
22 changes: 21 additions & 1 deletion gopushbullet.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ type Device struct {
Pushable bool `json:"pushable"`
}

//DeviceList describes an array of devices
type DeviceList struct {
Devices []Device `json:"devices"`
}

//Contact describes a contact entry.
type Contact struct {
ID string `json:"iden"`
Expand Down Expand Up @@ -337,12 +342,27 @@ func (c *Client) SendFileToTarget(targetType, target, fileName, fileType, fileUR

_, apiError, err := c.makeCall("POST", "pushes", p)
if err != nil {
log.Println("Failed to send file:", err, apiError.String())
log.Println("Failed to send file: ", err, apiError.String())
return err
}
return nil
}

//GetDevices obtains a list of registered devices from Pushbullet
func (c *Client) GetDevices() (DeviceList, error) {
var d DeviceList
res, apiError, err := c.makeCall("GET", "devices", nil)
if err != nil {
log.Println("Failed to get devices: ", err, apiError.String())
return d, err
}
err = json.Unmarshal(res, &d)
if err != nil {
return d, err
}
return d, nil
}

func (c *Client) makeCall(method string, call string, data interface{}) (responseBody []byte, apiError *Error, err error) {
// make sure API key seems ok
if len(c.APIKey) == 0 {
Expand Down
19 changes: 16 additions & 3 deletions gopushbullet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestSendNoteToAll(t *testing.T) {
// t.Fatal(err)
// }
//c := ClientWithKey(k)
mockServer, c := mockHTTP(200, "")
mockServer, c := mockHTTP(200, "{}")
defer mockServer.Close()

err := c.SendNote("Build Test", "This is a test of gopushbullet's SendNote() function.")
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestSendNoteToChannel(t *testing.T) {
}

func TestSendNoteToEmail(t *testing.T) {
mockServer, c := mockHTTP(200, "")
mockServer, c := mockHTTP(200, "{}")
defer mockServer.Close()

err := c.SendNoteToTarget("email", "kariudo@gmail.com", "Build Test", "This is a test of gopushbullet.")
Expand All @@ -147,11 +147,24 @@ func TestSendNoteToEmail(t *testing.T) {
}

func TestSendNoteToClientID(t *testing.T) {
mockServer, c := mockHTTP(200, "")
mockServer, c := mockHTTP(200, "{}")
defer mockServer.Close()

err := c.SendNoteToTarget("client", "_clientid_", "Build Test", "This is a test of gopushbullet's SendNote() function.")
if err != nil {
t.Error(err)
}
}

func TestGetDevices(t *testing.T) {
k, err := getKey()
if err != nil {
t.Error("Failed to get key")
}
c := ClientWithKey(k)
d, err := c.GetDevices()
if err != nil {
t.Error("Failed to get devices: ", err)
}
fmt.Println(d)
}

0 comments on commit 0e3977b

Please sign in to comment.