Repository: slack-go/slack
Pull request #812 (2020-10-01 14:13:43)

reminders.list api call implemented
##### Pull Request Guidelines

These are recommendations for pull requests.
They are strictly guidelines to help manage expectations.

##### PR preparation
Run `make pr-prep` from the root of the repository to run formatting, linting and tests. (passed)

##### Should this be an issue instead
- [ ] is it a convenience method? (no new functionality, streamlines some use case)
- [ ] exposes a previously private type, const, method, etc.
- [ ] is it application specific (caching, retry logic, rate limiting, etc)
- [ ] is it performance related.

##### API changes

Since API changes have to be maintained they undergo a more detailed review and are more likely to require changes.

- no tests, if you're adding to the API include at least a single test of the happy case.
- If you can accomplish your goal without changing the API, then do so.
- dependency changes. updates are okay. adding/removing need justification.

###### Examples of API changes that do not meet guidelines:
- in library cache for users. caches are use case specific.
- Convenience methods for Sending Messages, update, post, ephemeral, etc. consider opening an issue instead.

Description:

Implementation for https://api.slack.com/methods/reminders.list

Some changes in reminders.go:
- remindersResp struct for reminders.list response
- doReminders function for sending API request expecting reminders response mentioned above
- ListReminders function which calls the api request and returns reminders for the user 
- One test added (since the call takes only the token parameter not much you can test there)



Patch:
diff --git a/reminders.go b/reminders.go
index 9b90538..de1170a 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,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


Test patch:
diff --git a/reminders_test.go b/reminders_test.go
index 1ced533..bed7589 100644
--- a/reminders_test.go
+++ b/reminders_test.go
@@ -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)
+	}
+}