From da7a9e82c7918a876f8b12f3e51c6939c8e781ab Mon Sep 17 00:00:00 2001 From: karolisk Date: Thu, 1 Oct 2020 16:59:01 +0300 Subject: [PATCH 1/6] reminders_list_functionality --- go.mod | 3 ++- go.sum | 6 ++++++ reminders.go | 23 +++++++++++++++++++++++ reminders_test.go | 13 +++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 32fc4b939..28655e0bb 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,8 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/pkg/errors v0.8.0 github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.2.2 + github.com/stretchr/objx v0.3.0 // indirect + github.com/stretchr/testify v1.3.0 ) go 1.13 diff --git a/go.sum b/go.sum index d01bacbf1..6f1a38506 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= @@ -8,5 +9,10 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= +github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/reminders.go b/reminders.go index 9b905387e..1c9d53584 100644 --- a/reminders.go +++ b/reminders.go @@ -21,6 +21,11 @@ type reminderResp struct { Reminder Reminder `json:"reminder"` } +type remindersResp struct { + SlackResponse + Reminders []Reminder `json:"reminders"` +} + func (api *Client) doReminder(ctx context.Context, path string, values url.Values) (*Reminder, error) { response := &reminderResp{} if err := api.postMethod(ctx, path, values, response); err != nil { @@ -29,6 +34,24 @@ func (api *Client) doReminder(ctx context.Context, path string, values url.Value return &response.Reminder, response.Err() } +func (api *Client) doReminders(ctx context.Context, path string, values url.Values) (*[]Reminder, error) { + response := &remindersResp{} + if err := api.postMethod(ctx, path, values, response); err != nil { + return nil, err + } + return &response.Reminders, response.Err() +} + +// ListReminders lists all the reminders created by or for the authenticated user +// +// See https://api.slack.com/methods/reminders.list +func (api *Client) ListReminders() (*[]Reminder, error) { + values := url.Values{ + "token": {api.token}, + } + return api.doReminders(context.Background(), "reminders.list", values) +} + // AddChannelReminder adds a reminder for a channel. // // See https://api.slack.com/methods/reminders.add (NOTE: the ability to set diff --git a/reminders_test.go b/reminders_test.go index 1ced533a4..9688ec9cf 100644 --- a/reminders_test.go +++ b/reminders_test.go @@ -156,3 +156,16 @@ func TestSlack_DeleteReminder(t *testing.T) { } } } + +func TestSlack_ListReminders(t *testing.T) { + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + var rh *remindersHandler + http.HandleFunc("/reminders.list", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) + rh = newRemindersHandler() + + _, err := api.ListReminders() + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } +} From f21eda84fd281f6fcf050aba4da473c31478d97a Mon Sep 17 00:00:00 2001 From: karolisk Date: Thu, 1 Oct 2020 17:14:44 +0300 Subject: [PATCH 2/6] removing automatic go.mod changes done by my IDE --- go.mod | 3 +-- go.sum | 6 ------ reminders_test.go | 2 ++ 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 28655e0bb..32fc4b939 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,7 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/pkg/errors v0.8.0 github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.3.0 // indirect - github.com/stretchr/testify v1.3.0 + github.com/stretchr/testify v1.2.2 ) go 1.13 diff --git a/go.sum b/go.sum index 6f1a38506..d01bacbf1 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,3 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho= @@ -9,10 +8,5 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= -github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/reminders_test.go b/reminders_test.go index 9688ec9cf..bed758948 100644 --- a/reminders_test.go +++ b/reminders_test.go @@ -158,6 +158,8 @@ func TestSlack_DeleteReminder(t *testing.T) { } func TestSlack_ListReminders(t *testing.T) { + // as the ListReminders takes only token parameter + // we just test if the call doesn't return an error once.Do(startServer) api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) var rh *remindersHandler From 0ef31874a385b44e48e86eab51bc858046907573 Mon Sep 17 00:00:00 2001 From: Karolis <31440123+KarolisKl@users.noreply.github.com> Date: Fri, 2 Oct 2020 15:09:08 +0300 Subject: [PATCH 3/6] Update reminders.go Co-authored-by: Naoki Kanatani --- reminders.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reminders.go b/reminders.go index 1c9d53584..8f8a33daf 100644 --- a/reminders.go +++ b/reminders.go @@ -34,7 +34,7 @@ func (api *Client) doReminder(ctx context.Context, path string, values url.Value return &response.Reminder, response.Err() } -func (api *Client) doReminders(ctx context.Context, path string, values url.Values) (*[]Reminder, error) { +func (api *Client) doReminders(ctx context.Context, path string, values url.Values) ([]*Reminder, error) { response := &remindersResp{} if err := api.postMethod(ctx, path, values, response); err != nil { return nil, err From ffcc49ba31873d7212df5c5fd0910795f59eedca Mon Sep 17 00:00:00 2001 From: karolisk Date: Fri, 2 Oct 2020 15:39:44 +0300 Subject: [PATCH 4/6] following fixes to previous change --- reminders.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/reminders.go b/reminders.go index 8f8a33daf..03404b9ce 100644 --- a/reminders.go +++ b/reminders.go @@ -39,13 +39,20 @@ func (api *Client) doReminders(ctx context.Context, path string, values url.Valu if err := api.postMethod(ctx, path, values, response); err != nil { return nil, err } - return &response.Reminders, response.Err() + + // create an array of pointers to reminders + var reminders = make([]*Reminder, 0) + for _, reminder := range response.Reminders { + reminders = append(reminders, &reminder) + } + + return reminders, response.Err() } // ListReminders lists all the reminders created by or for the authenticated user // // See https://api.slack.com/methods/reminders.list -func (api *Client) ListReminders() (*[]Reminder, error) { +func (api *Client) ListReminders() ([]*Reminder, error) { values := url.Values{ "token": {api.token}, } From dcc03859d2b387bcf17dcd5f9b9c051c8d0b2ffe Mon Sep 17 00:00:00 2001 From: Karolis <31440123+KarolisKl@users.noreply.github.com> Date: Tue, 6 Oct 2020 11:49:55 +0300 Subject: [PATCH 5/6] add capacity to reminders array allocation Co-authored-by: Naoki Kanatani --- reminders.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reminders.go b/reminders.go index 03404b9ce..5fd542e3e 100644 --- a/reminders.go +++ b/reminders.go @@ -41,7 +41,7 @@ func (api *Client) doReminders(ctx context.Context, path string, values url.Valu } // create an array of pointers to reminders - var reminders = make([]*Reminder, 0) + var reminders = make([]*Reminder, 0, len(response.Reminders) for _, reminder := range response.Reminders { reminders = append(reminders, &reminder) } From 4375096d5b06b30b240560e7bf7ca39cb5fd4c1c Mon Sep 17 00:00:00 2001 From: karolisk Date: Tue, 6 Oct 2020 11:59:06 +0300 Subject: [PATCH 6/6] missing ) --- reminders.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reminders.go b/reminders.go index 5fd542e3e..de1170a63 100644 --- a/reminders.go +++ b/reminders.go @@ -41,7 +41,7 @@ func (api *Client) doReminders(ctx context.Context, path string, values url.Valu } // create an array of pointers to reminders - var reminders = make([]*Reminder, 0, len(response.Reminders) + var reminders = make([]*Reminder, 0, len(response.Reminders)) for _, reminder := range response.Reminders { reminders = append(reminders, &reminder) }