Skip to content

Commit 1d4d3cc

Browse files
committed
docs: update readme and examples for updated go-github-ratelimit and introduce go-github-pagination
1 parent 259cd22 commit 1d4d3cc

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ repos, _, err := client.Repositories.List(context.WithValue(ctx, github.SleepUnt
233233
```
234234

235235
You can use [gofri/go-github-ratelimit](https://github.com/gofri/go-github-ratelimit) to handle
236-
secondary rate limit sleep-and-retry for you.
236+
secondary rate limit sleep-and-retry for you, as well as primary rate limit abuse-prevention and callback trigerring.
237237

238238
Learn more about GitHub secondary rate limiting in
239239
["About secondary rate limits"](https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits).
@@ -347,6 +347,13 @@ for repo := range repos.All() {
347347

348348
For complete usage of `enrichman/gh-iter`, see the full [package docs](https://github.com/enrichman/gh-iter).
349349

350+
#### Middleware ####
351+
352+
You can use [gofri/go-github-pagination](https://github.com/gofri/go-github-pagination) to handle
353+
pagination for you. It supports both sync and async modes, as well as customizations.
354+
By default, the middleware automatically paginates through all pages, aggregates results, and returns them as an array.
355+
356+
350357
### Webhooks ###
351358

352359
`go-github` provides structs for almost all [GitHub webhook events][] as well as functions to validate them and unmarshal JSON payloads from `http.Request` structs.

example/go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module github.com/google/go-github/v69/example
22

3-
go 1.23.0
3+
go 1.23.1
4+
5+
toolchain go1.23.5
46

57
require (
68
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371
79
github.com/bradleyfalzon/ghinstallation/v2 v2.0.4
8-
github.com/gofri/go-github-ratelimit v1.0.3
10+
github.com/gofri/go-github-pagination v1.0.0
11+
github.com/gofri/go-github-ratelimit/v2 v2.0.1
912
github.com/google/go-github/v69 v69.2.0
1013
github.com/sigstore/sigstore-go v0.6.1
1114
golang.org/x/crypto v0.32.0

example/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
134134
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
135135
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
136136
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
137-
github.com/gofri/go-github-ratelimit v1.0.3 h1:Ocs2jaYokZDzgvqaajX+g04dqFyVqL0JQzoO7d2wmlk=
138-
github.com/gofri/go-github-ratelimit v1.0.3/go.mod h1:OnCi5gV+hAG/LMR7llGhU7yHt44se9sYgKPnafoL7RY=
137+
github.com/gofri/go-github-ratelimit/v2 v2.0.1 h1:jhd3iZ2XINscvYGL+kNXZosr70IZyDITZZC+MznthLI=
138+
github.com/gofri/go-github-ratelimit/v2 v2.0.1/go.mod h1:nqGxwHNwqXx5GNRaEEhegUOvMLAx51BOJtN8eFi8FEA=
139+
github.com/gofri/go-github-pagination v1.0.0 h1:nnCi+1xT5ybqY/plctISgiQPWZOtfSciVQlbx/hM/Yw=
140+
github.com/gofri/go-github-pagination v1.0.0/go.mod h1:Qij55Fb4fNPjam3SB+8cLnqp4pgR8RGMyIspYXcyHX0=
139141
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
140142
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
141143
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=

example/ratelimit/main.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
// Use of this source code is governed by a BSD-style
44
// license that can be found in the LICENSE file.
55

6-
// The ratelimit command demonstrates using the github_ratelimit.SecondaryRateLimitWaiter.
6+
// The ratelimit command demonstrates using the github_ratelimit as well as github_pagination.
77
// By using the waiter, the client automatically sleeps and retry requests
88
// when it hits secondary rate limits.
9+
// It also prevents the client from abusing the API in case of a primary rate limit.
910
package main
1011

1112
import (
1213
"context"
1314
"fmt"
1415

15-
"github.com/gofri/go-github-ratelimit/github_ratelimit"
16+
"github.com/gofri/go-github-pagination/githubpagination"
17+
"github.com/gofri/go-github-ratelimit/v2/github_ratelimit"
18+
"github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_primary_ratelimit"
19+
"github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_secondary_ratelimit"
1620
"github.com/google/go-github/v69/github"
1721
)
1822

@@ -21,22 +25,28 @@ func main() {
2125
fmt.Print("Enter GitHub username: ")
2226
fmt.Scanf("%s", &username)
2327

24-
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(nil)
25-
if err != nil {
26-
fmt.Printf("Error: %v\n", err)
27-
return
28-
}
28+
rateLimiter := github_ratelimit.New(nil,
29+
github_primary_ratelimit.WithLimitDetectedCallback(func(ctx *github_primary_ratelimit.CallbackContext) {
30+
fmt.Printf("Primary rate limit detected: category %s, reset time: %v\n", ctx.Category, ctx.ResetTime)
31+
}),
32+
github_secondary_ratelimit.WithLimitDetectedCallback(func(ctx *github_secondary_ratelimit.CallbackContext) {
33+
fmt.Printf("Secondary rate limit detected: reset time: %v, total sleep time: %v\n", ctx.ResetTime, ctx.TotalSleepTime)
34+
}),
35+
)
2936

30-
client := github.NewClient(rateLimiter)
37+
paginator := githubpagination.NewClient(rateLimiter,
38+
githubpagination.WithPerPage(100), // default to 100 results per page
39+
)
40+
client := github.NewClient(paginator)
3141

3242
// arbitrary usage of the client
33-
organizations, _, err := client.Organizations.List(context.Background(), username, nil)
43+
repos, _, err := client.Repositories.ListByUser(context.Background(), username, nil)
3444
if err != nil {
3545
fmt.Printf("Error: %v\n", err)
3646
return
3747
}
3848

39-
for i, organization := range organizations {
40-
fmt.Printf("%v. %v\n", i+1, organization.GetLogin())
49+
for i, repo := range repos {
50+
fmt.Printf("%v. %v\n", i+1, repo.GetName())
4151
}
4252
}

0 commit comments

Comments
 (0)