Skip to content

Commit

Permalink
docs: Add an example demonstrating correct usage of GetUsersPaginated (
Browse files Browse the repository at this point in the history
…#1201)

* Add an example demonstrating correct usage of GetUsersPaginated

* Requeue GitHub Actions

---------

Co-authored-by: Lorenzo Aiello <[email protected]>
  • Loading branch information
adamrothman and lorenzoaiello authored Jul 15, 2024
1 parent 0a5c9e1 commit ecfe504
Showing 1 changed file with 59 additions and 0 deletions.
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))
}

0 comments on commit ecfe504

Please sign in to comment.