
Security News
Astral Launches pyx: A Python-Native Package Registry
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
github.com/xorima/go-github-pagination
The pagination
package provides a simple and efficient way to handle pagination with the GitHub API. It offers interfaces and functions to list, process, and handle rate limits for any type of items you wish to paginate through.
To get started with the pagination
package, you can install it using the go get
command:
go get github.com/hsbc/go-api-pagination
After installation, you can import the package in your Go code:
import "github.com/hsbc/go-api-pagination/pagination"
ListFunc: This interface returns a list of items for the given type. It requires the implementation of the List
method which fetches the items.
ProcessFunc: This interface processes an item for the given type. It requires the implementation of the Process
method which processes each item.
RateLimitFunc: This interface handles rate limiting. It requires the implementation of the RateLimit
method which decides whether to continue pagination based on rate limits.
The main function provided by this package is Paginator
. It takes in the following parameters:
ctx
: The context for the API calls.listFunc
: An instance of a type that implements the ListFunc
interface.processFunc
: An instance of a type that implements the ProcessFunc
interface.rateLimitFunc
: An instance of a type that implements the RateLimitFunc
interface.Opts
: An instance of PaginatorOpts
which contains list options like PerPage
and Page
.The function paginates through the items, processes them, handles rate limits, and returns all the items.
Here are example implementations for the interfaces:
type listFunc struct {
client *github.Client
}
func (l *listFunc) List(ctx context.Context, opt *github.ListOptions) ([]*github.Repository, *github.Response, error) {
t, r, err := l.client.Apps.ListRepos(ctx, opt)
return t.Repositories, r, err
}
type processFunc struct {
client *github.Client
}
func (p *processFunc) Process(ctx context.Context, item *github.Repository) error {
fmt.Println(item.GetName())
return nil
}
type rateLimitFunc struct {
}
func (r *rateLimitFunc) RateLimit(ctx context.Context, resp *github.Response) (bool, error) {
if resp.Rate.Remaining <= 1 {
time.Sleep(time.Until(resp.Rate.Reset.Time))
}
return true, nil
}
// Initialize the GitHub client
githubClient := github.NewClient(nil)
// Define your list, process, and rate limit functions
listFuncInstance := &listFunc{client: githubClient}
processFuncInstance := &processFunc{client: githubClient}
rateLimitFuncInstance := &rateLimitFunc{}
// Call the Paginator function
items, err := pagination.Paginator(ctx, listFuncInstance, processFuncInstance, rateLimitFuncInstance, &pagination.PaginatorOpts{
ListOptions: &github.ListOptions{PerPage: 50, Page: 1},
})
if err != nil {
log.Fatalf("Error paginating: %v", err)
}
// Process the items
for _, item := range items {
// Your logic here
}
The pagination
package simplifies the process of paginating through items from the GitHub API. By providing clear interfaces, example implementations, and a main Paginator function, it abstracts away the complexities of pagination, rate limiting, and item processing.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.