Skip to content

0106hyh/API-Integration-Pagination

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Integration Pagination

A robust, lightweight Go package designed to fetch issues from the GitHub API using go-github with correct, Link-header-based pagination.

This library ensures filters like since are properly propagated and preserved across all pages, handling edge cases such as empty pages with a Next link correctly.

🚀 Features

  • Link-Header Pagination: Relies on GitHub's API response metadata (resp.NextPage) rather than relying on slice lengths, preventing early loop termination.
  • Filter Preservation: Ensures parameter states (like the Since timestamp) are preserved consistently across all paginated requests.
  • Edge Case Protection: Correctly handles intermediate empty pages and API rate-limiting feedback.
  • Robust Mock Testing: Covered by a comprehensive unit-test suite utilizing Go's net/http/httptest to mock paginated API responses.

📦 Installation

To install the package, run:

go get github.com/alisteuber4ee1/API-Integration-Pagination

🛠️ Usage

Here is a quick example of how to fetch all issues updated within the last 7 days:

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"time"

	"github.com/alisteuber4ee1/API-Integration-Pagination/pkg/github"
	google_github "github.com/google/go-github/v57/github"
)

func main() {
	// 1. Initialize go-github client (optional: with authentication token)
	client := google_github.NewClient(nil)
	if token := os.Getenv("GITHUB_TOKEN"); token != "" {
		client = google_github.NewClient(nil).WithAuthToken(token)
	}

	// 2. Create IssueFetcher
	fetcher := github.NewIssueFetcher(client)

	// 3. Fetch issues updated in the last 7 days
	since := time.Now().Add(-7 * 24 * time.Hour)
	issues, err := fetcher.FetchIssues(context.Background(), "google", "go-github", since, 100)
	if err != nil {
		log.Fatalf("Error fetching issues: %v", err)
	}

	fmt.Printf("Successfully fetched %d issues.\n", len(issues))
	for _, issue := range issues {
		fmt.Printf("- [#%d] %s\n", issue.GetNumber(), issue.GetTitle())
	}
}

🧪 Testing

Run the unit test suite to verify the pagination logic and mock integration:

go test -v ./...

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Go 100.0%