@@ -25,7 +25,7 @@ It is best to stack the pagination round-tripper on top of the rate limit round-
2525## Usage Example (with [ go-github] ( https://github.com/google/go-github ) )
2626
2727``` go
28- import " github.com/google/go-github/v68 /github"
28+ import " github.com/google/go-github/v69 /github"
2929import " github.com/gofri/go-github-ratelimit/v2/github_ratelimit"
3030
3131func main () {
@@ -73,31 +73,41 @@ as well as fine-grained policy control (e.g., for a sophisticated pagination mec
7373## Advanced Example
7474
7575``` go
76- import " github.com/google/go-github/v68 /github"
76+ import " github.com/google/go-github/v69 /github"
7777import " github.com/gofri/go-github-ratelimit/v2/github_ratelimit"
78+ import " github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_primary_ratelimit"
79+ import " github.com/gofri/go-github-ratelimit/v2/github_ratelimit/github_secondary_ratelimit"
7880import " github.com/gofri/go-github-pagination/githubpagination"
7981
8082func main () {
81- // Set up the rate limiter with some options.
82- rateLimiter , err := github_ratelimit.New (nil ,
83- github_ratelimit.WithLimitDetectedCallback (func (category string , limit int , remaining int , reset int ) {
84- fmt.Printf (" Primary rate limit detected: %s , %d /%d , reset in %d seconds\n " , category, remaining, limit, reset)
85- }),
86- github_ratelimit.WithRequestPreventedCallback (func (category string , limit int , remaining int , reset int ) {
87- fmt.Printf (" Request prevented due to primary rate limit: %s , %d /%d , reset in %d seconds\n " , category, remaining, limit, reset)
88- }),
89- )
90- if err != nil {
91- panic (err)
92- }
93-
94- paginator := githubpagination.NewClient (nil ,
95- githubpagination.WithPerPage (100 ), // default to 100 results per page
96- )
97-
98- client := github.NewClient (rateLimiter).WithAuthToken (" your personal access token" )
99-
100- // now use the client as you please
83+ var username string
84+ fmt.Print (" Enter GitHub username: " )
85+ fmt.Scanf (" %s " , &username)
86+
87+ rateLimiter := github_ratelimit.New (nil ,
88+ github_primary_ratelimit.WithLimitDetectedCallback (func (ctx *github_primary_ratelimit.CallbackContext ) {
89+ fmt.Printf (" Primary rate limit detected: category %s , reset time: %v \n " , ctx.Category , ctx.ResetTime )
90+ }),
91+ github_secondary_ratelimit.WithLimitDetectedCallback (func (ctx *github_secondary_ratelimit.CallbackContext ) {
92+ fmt.Printf (" Secondary rate limit detected: reset time: %v , total sleep time: %v \n " , ctx.ResetTime , ctx.TotalSleepTime )
93+ }),
94+ )
95+
96+ paginator := githubpagination.NewClient (rateLimiter,
97+ githubpagination.WithPerPage (100 ), // default to 100 results per page
98+ )
99+ client := github.NewClient (paginator)
100+
101+ // arbitrary usage of the client
102+ repos , _ , err := client.Repositories .ListByUser (context.Background (), username, nil )
103+ if err != nil {
104+ fmt.Printf (" Error: %v \n " , err)
105+ return
106+ }
107+
108+ for i , repo := range repos {
109+ fmt.Printf (" %v . %v \n " , i+1 , repo.GetName ())
110+ }
101111}
102112```
103113
0 commit comments