Verified Commit fc88ad8a authored by Florian Forster's avatar Florian Forster
Browse files

docs(pagination): Add an example function demonstrating how to use `Scan2`.

parent f821c08c
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -2,11 +2,46 @@ package gitlab_test

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	gitlab "gitlab.com/gitlab-org/api/client-go"
)

func ExampleScan2() {
	// Create a client (this would normally use your GitLab instance URL and token)
	client, err := gitlab.NewAuthSourceClient(
		gitlab.AccessTokenAuthSource{"your-token"},
		gitlab.WithBaseURL("https://gitlab.example.com/api/v4"),
	)
	if err != nil {
		// Handle the error
		panic(err)
	}

	opts := &gitlab.ListProjectsOptions{}

	pager := func(pageOpt gitlab.PaginationOptionFunc) ([]*gitlab.Project, *gitlab.Response, error) {
		// Call ListProjects with pageOpt to retrieve the next page
		return client.Projects.ListProjects(opts, pageOpt)
	}

	// Create a project iterator
	projects := gitlab.Scan2(pager)

	// Iterate over the project iterator
	for project, err := range projects {
		// Errors are delivered inline — check for them and break the loop before using the value
		if err != nil {
			log.Println("ERROR:", err)
			break
		}

		fmt.Printf("- %s (ID: %d)\n", project.PathWithNamespace, project.ID)
	}
}

func ExampleScanAndCollectN() {
	// Create a client (this would normally use your GitLab instance URL and token)
	client, err := gitlab.NewAuthSourceClient(