Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add an example demonstrating correct usage of GetUsersPaginated #1201

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions examples/pagination/pagination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"context"
"errors"
"fmt"
"time"

"github.com/slack-go/slack"
)

func getAllUserUIDs(ctx context.Context, client *slack.Client, pageSize int) ([]string, error) {
var uids []string
var err error

pages := 0
pager := client.GetUsersPaginated(slack.GetUsersOptionLimit(pageSize))
for {
// Note reassignment of pager to the value returned by Next()
pager, err = pager.Next(ctx)
if failedErr := pager.Failure(err); failedErr != nil {
var rateLimited *slack.RateLimitedError
if errors.As(failedErr, &rateLimited) && rateLimited.Retryable() {
fmt.Println("Rate limited by Slack API; sleeping", rateLimited.RetryAfter)
select {
case <-ctx.Done():
return uids, ctx.Err()
case <-time.After(rateLimited.RetryAfter):
continue
}
}
return uids, fmt.Errorf("paginating users: %w", failedErr)
}
if pager.Done(err) {
break
}

for _, user := range pager.Users {
uids = append(uids, user.ID)
}

pages++
}

fmt.Printf("Pagination complete after %d pages\n", pages)

return uids, nil
}

func main() {
client := slack.New("YOUR_TOKEN_HERE")

uids, err := getAllUserUIDs(context.Background(), client, 1000)
if err != nil {
panic(err)
}

fmt.Printf("Collected %d UIDs\n", len(uids))
}
Loading