Skip to content

Commit

Permalink
Do not fetch twice the first page
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Castelein committed Jun 21, 2018
1 parent b9da648 commit 39b2664
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions pagination/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Pager struct {

createPage func(r PageResult) Page

firstPage Page

Err error

// Headers supplies additional HTTP headers to populate on each paged request.
Expand Down Expand Up @@ -89,9 +91,18 @@ func (p Pager) EachPage(handler func(Page) (bool, error)) error {
}
currentURL := p.initialURL
for {
currentPage, err := p.fetchNextPage(currentURL)
if err != nil {
return err
var currentPage Page

// if first page has already been fetched, no need to fetch it again
if p.firstPage != nil {
currentPage = p.firstPage
p.firstPage = nil
} else {
var err error
currentPage, err = p.fetchNextPage(currentURL)
if err != nil {
return err
}
}

empty, err := currentPage.IsEmpty()
Expand Down Expand Up @@ -128,23 +139,26 @@ func (p Pager) AllPages() (Page, error) {
// body will contain the final concatenated Page body.
var body reflect.Value

// Grab a test page to ascertain the page body type.
testPage, err := p.fetchNextPage(p.initialURL)
// Grab a first page to ascertain the page body type.
firstPage, err := p.fetchNextPage(p.initialURL)
if err != nil {
return nil, err
}
// Store the page type so we can use reflection to create a new mega-page of
// that type.
pageType := reflect.TypeOf(testPage)
pageType := reflect.TypeOf(firstPage)

// if it's a single page, just return the testPage (first page)
// if it's a single page, just return the firstPage (first page)
if _, found := pageType.FieldByName("SinglePageBase"); found {
return testPage, nil
return firstPage, nil
}

// store the first page to avoid getting it twice
p.firstPage = firstPage

// Switch on the page body type. Recognized types are `map[string]interface{}`,
// `[]byte`, and `[]interface{}`.
switch pb := testPage.GetBody().(type) {
switch pb := firstPage.GetBody().(type) {
case map[string]interface{}:
// key is the map key for the page body if the body type is `map[string]interface{}`.
var key string
Expand Down

0 comments on commit 39b2664

Please sign in to comment.