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

Parse Before/After Links into Response #2154

Merged
merged 2 commits into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion github/github.go
Expand Up @@ -462,6 +462,10 @@ type Response struct {
// Set ListCursorOptions.Cursor to this value when calling the endpoint again.
Cursor string

// For APIs that support before/after pagniation, such as OrganizationsService.AuditLog.
gmlewis marked this conversation as resolved.
Show resolved Hide resolved
Before string
After string

// Explicitly specify the Rate type so Rate's String() receiver doesn't
// propagate to Response.
Rate Rate
Expand Down Expand Up @@ -517,7 +521,9 @@ func (r *Response) populatePageValues() {
}

page := q.Get("page")
if page == "" {
before := q.Get("before")
after := q.Get("after")
if page == "" && before == "" && after == "" {
continue
}

Expand All @@ -527,8 +533,10 @@ func (r *Response) populatePageValues() {
if r.NextPage, err = strconv.Atoi(page); err != nil {
r.NextPageToken = page
}
r.After = after
case `rel="prev"`:
r.PrevPage, _ = strconv.Atoi(page)
r.Before = before
case `rel="first"`:
r.FirstPage, _ = strconv.Atoi(page)
case `rel="last"`:
Expand Down
34 changes: 34 additions & 0 deletions github/github_test.go
Expand Up @@ -688,6 +688,40 @@ func TestResponse_cursorPagination(t *testing.T) {
}
}

func TestResponse_beforeAfterPagination(t *testing.T) {
r := http.Response{
Header: http.Header{
"Link": {`<https://api.github.com/?after=a1b2c3&before=>; rel="next",` +
` <https://api.github.com/?after=&before=>; rel="first",` +
` <https://api.github.com/?after=&before=d4e5f6>; rel="prev",`,
},
},
}

response := newResponse(&r)
if got, want := response.Before, "d4e5f6"; got != want {
t.Errorf("response.Before: %v, want %v", got, want)
}
if got, want := response.After, "a1b2c3"; got != want {
t.Errorf("response.After: %v, want %v", got, want)
}
if got, want := response.FirstPage, 0; got != want {
t.Errorf("response.FirstPage: %v, want %v", got, want)
}
if got, want := response.PrevPage, 0; want != got {
t.Errorf("response.PrevPage: %v, want %v", got, want)
}
if got, want := response.NextPage, 0; want != got {
t.Errorf("response.NextPage: %v, want %v", got, want)
}
if got, want := response.LastPage, 0; want != got {
t.Errorf("response.LastPage: %v, want %v", got, want)
}
if got, want := response.NextPageToken, ""; want != got {
t.Errorf("response.NextPageToken: %v, want %v", got, want)
}
}

func TestResponse_populatePageValues_invalid(t *testing.T) {
r := http.Response{
Header: http.Header{
Expand Down