Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic pager implementation #17027

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions sdk/azcore/runtime/pager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//go:build go1.18
// +build go1.18

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package runtime
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"errors"
"net/http"
"reflect"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
)

// PageProcessor contains the required data for constructing a Pager.
type PageProcessor[T any] struct {
// Do sends the request to fetch the next page.
Do func(*policy.Request) (*http.Response, error)
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved

// More returns a boolean indicating if there are more pages to fetch.
// It uses the provided page to make the determination.
More func(T) bool

// Requester creates the request to fetch the first page.
Requester func(context.Context) (*policy.Request, error)
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved

// Advancer creates the request to fetch subsequent pages.
Advancer func(context.Context, T) (*policy.Request, error)

// Responder handles the responses when fetching pages.
Responder func(*http.Response) (T, error)
}

// Pager provides operations for iterating over paged responses.
type Pager[T any] struct {
current T
processor PageProcessor[T]
firstPage bool
}

// NewPager creates an instance of Pager using the specified PageProcessor.
func NewPager[T any](processor PageProcessor[T]) *Pager[T] {
return &Pager[T]{
processor: processor,
firstPage: true,
}
}

// More returns true if there are more pages to retrieve.
func (p *Pager[T]) More() bool {
if !reflect.ValueOf(p.current).IsZero() {
return p.processor.More(p.current)
}
return true
}

// NextPage advances the pager to the next page.
func (p *Pager[T]) NextPage(ctx context.Context) (T, error) {
var req *policy.Request
var err error
if !reflect.ValueOf(p.current).IsZero() && p.firstPage {
// we get here if it's an LRO-pager, we already have the first page
p.firstPage = false
return p.current, nil
} else if !reflect.ValueOf(p.current).IsZero() {
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved
if !p.processor.More(p.current) {
return *new(T), errors.New("no more pages")
}
req, err = p.processor.Advancer(ctx, p.current)
} else {
// non-LRO case
p.firstPage = false
req, err = p.processor.Requester(ctx)
}
if err != nil {
return *new(T), err
}
resp, err := p.processor.Do(req)
if err != nil {
return *new(T), err
}
if !shared.HasStatusCode(resp, http.StatusOK) {
return *new(T), shared.NewResponseError(resp)
}
result, err := p.processor.Responder(resp)
if err != nil {
return *new(T), err
}
p.current = result
return p.current, nil
}