Skip to content

Commit

Permalink
Merge pull request #812 from KarolisKl/reminders_list_functionality
Browse files Browse the repository at this point in the history
reminders.list api call implemented
  • Loading branch information
kanata2 authored Oct 14, 2020
2 parents 84ef9d6 + 4375096 commit 2f3e31d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
30 changes: 30 additions & 0 deletions reminders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,6 +34,31 @@ 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
}

// create an array of pointers to reminders
var reminders = make([]*Reminder, 0, len(response.Reminders))
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) {
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
Expand Down
15 changes: 15 additions & 0 deletions reminders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,18 @@ 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
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)
}
}

0 comments on commit 2f3e31d

Please sign in to comment.