Skip to content

Commit a4ce810

Browse files
authored
Parse Before/After Links into Response (#2154)
1 parent ca2f3c2 commit a4ce810

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

github/github.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ type Response struct {
477477
// Set ListCursorOptions.Cursor to this value when calling the endpoint again.
478478
Cursor string
479479

480+
// For APIs that support before/after pagination, such as OrganizationsService.AuditLog.
481+
Before string
482+
After string
483+
480484
// Explicitly specify the Rate type so Rate's String() receiver doesn't
481485
// propagate to Response.
482486
Rate Rate
@@ -532,7 +536,9 @@ func (r *Response) populatePageValues() {
532536
}
533537

534538
page := q.Get("page")
535-
if page == "" {
539+
before := q.Get("before")
540+
after := q.Get("after")
541+
if page == "" && before == "" && after == "" {
536542
continue
537543
}
538544

@@ -542,8 +548,10 @@ func (r *Response) populatePageValues() {
542548
if r.NextPage, err = strconv.Atoi(page); err != nil {
543549
r.NextPageToken = page
544550
}
551+
r.After = after
545552
case `rel="prev"`:
546553
r.PrevPage, _ = strconv.Atoi(page)
554+
r.Before = before
547555
case `rel="first"`:
548556
r.FirstPage, _ = strconv.Atoi(page)
549557
case `rel="last"`:

github/github_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,40 @@ func TestResponse_cursorPagination(t *testing.T) {
688688
}
689689
}
690690

691+
func TestResponse_beforeAfterPagination(t *testing.T) {
692+
r := http.Response{
693+
Header: http.Header{
694+
"Link": {`<https://api.github.com/?after=a1b2c3&before=>; rel="next",` +
695+
` <https://api.github.com/?after=&before=>; rel="first",` +
696+
` <https://api.github.com/?after=&before=d4e5f6>; rel="prev",`,
697+
},
698+
},
699+
}
700+
701+
response := newResponse(&r)
702+
if got, want := response.Before, "d4e5f6"; got != want {
703+
t.Errorf("response.Before: %v, want %v", got, want)
704+
}
705+
if got, want := response.After, "a1b2c3"; got != want {
706+
t.Errorf("response.After: %v, want %v", got, want)
707+
}
708+
if got, want := response.FirstPage, 0; got != want {
709+
t.Errorf("response.FirstPage: %v, want %v", got, want)
710+
}
711+
if got, want := response.PrevPage, 0; want != got {
712+
t.Errorf("response.PrevPage: %v, want %v", got, want)
713+
}
714+
if got, want := response.NextPage, 0; want != got {
715+
t.Errorf("response.NextPage: %v, want %v", got, want)
716+
}
717+
if got, want := response.LastPage, 0; want != got {
718+
t.Errorf("response.LastPage: %v, want %v", got, want)
719+
}
720+
if got, want := response.NextPageToken, ""; want != got {
721+
t.Errorf("response.NextPageToken: %v, want %v", got, want)
722+
}
723+
}
724+
691725
func TestResponse_populatePageValues_invalid(t *testing.T) {
692726
r := http.Response{
693727
Header: http.Header{

0 commit comments

Comments
 (0)