Browse Source

Added ListPushes

Piotr Czajkowski 3 years ago
parent
commit
1519ccf6d4
3 changed files with 110 additions and 0 deletions
  1. 45 0
      bullet.go
  2. 46 0
      bullet_test.go
  3. 19 0
      pushes.go

+ 45 - 0
bullet.go

@@ -6,6 +6,7 @@ import (
 	"io"
 	"mime"
 	"net/http"
+	"net/url"
 	"path/filepath"
 	"time"
 )
@@ -209,3 +210,47 @@ func (b Bullet) ListDevices() (*Devices, error) {
 
 	return &result, nil
 }
+
+// ListPushes returns Pushes structure which contains slice of pushes
+func (b Bullet) ListPushes(active bool, modifiedAfter *time.Time, limit int, cursor string) (*Pushes, error) {
+	params := url.Values{}
+
+	if active {
+		params.Add("active", "true")
+	} else {
+		params.Add("active", "false")
+	}
+
+	if modifiedAfter != nil {
+		params.Add("modified_after", fmt.Sprint(modifiedAfter.Unix()))
+	}
+
+	if limit > 0 {
+		params.Add("limit", fmt.Sprint(limit))
+	}
+
+	params.Add("cursor", cursor)
+	URL := fmt.Sprintf("%s/pushes?%s", b.baseURL, params.Encode())
+
+	request, err := http.NewRequest(http.MethodGet, URL, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	request.Header.Add("Access-Token", b.token)
+
+	response, errResponse := doRequest(request)
+	if errResponse != nil {
+		return nil, errResponse
+	}
+	defer response.Body.Close()
+
+	var result Pushes
+	decoder := json.NewDecoder(response.Body)
+	errJSON := decoder.Decode(&result)
+	if errJSON != nil {
+		return nil, errJSON
+	}
+
+	return &result, nil
+}

+ 46 - 0
bullet_test.go

@@ -169,3 +169,49 @@ func TestListDevices(t *testing.T) {
 		t.Errorf("Device ID should be %s, but is %s", expectedID, deviceID)
 	}
 }
+
+func TestListPushes(t *testing.T) {
+	pushesJSON := `{
+  "pushes": [
+    {
+      "active": true,
+      "body": "Space Elevator, Mars Hyperloop, Space Model S (Model Space?)",
+      "created": 1412047948.579029,
+      "direction": "self",
+      "dismissed": false,
+      "iden": "ujpah72o0sjAoRtnM0jc",
+      "modified": 1412047948.579031,
+      "receiver_email": "elon@teslamotors.com",
+      "receiver_email_normalized": "elon@teslamotors.com",
+      "receiver_iden": "ujpah72o0",
+      "sender_email": "elon@teslamotors.com",
+      "sender_email_normalized": "elon@teslamotors.com",
+      "sender_iden": "ujpah72o0",
+      "sender_name": "Elon Musk",
+      "title": "Space Travel Ideas",
+      "type": "note"
+    }
+  ]
+}`
+
+	server := fakeServer(http.StatusOK, pushesJSON)
+	server.Start()
+	defer server.Close()
+
+	b := Bullet{token: "", baseURL: server.URL}
+
+	pushes, err := b.ListPushes(true, nil, 10, "")
+	if err != nil {
+		t.Error(err)
+	}
+
+	if pushes == nil {
+		t.Fatal("Pushes shouldn't be nil!")
+	}
+
+	expectedID := "ujpah72o0sjAoRtnM0jc"
+	pushID := pushes.Items[0].ID
+	if pushID != expectedID {
+		t.Errorf("Push ID should be %s, but is %s", expectedID, pushID)
+	}
+}

+ 19 - 0
pushes.go

@@ -0,0 +1,19 @@
+package bullet
+
+type Push struct {
+	ID       string  `json:"iden"`
+	Active   bool    `json:"active"`
+	Created  float64 `json:"created"`
+	Type     string  `json:"type"`
+	Title    string  `json:"title"`
+	Body     string  `json:"body"`
+	URL      string  `json:"url"`
+	FileName string  `json:"file_name"`
+	FileType string  `json:"file_type"`
+	FileURL  string  `json:"file_url"`
+	ImageURL string  `json:"image_url"`
+}
+
+type Pushes struct {
+	Items []Push `json:"pushes"`
+}