Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
go-gitlab
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
go-gitlab
Commits
f8137824
Commit
f8137824
authored
Feb 20, 2018
by
Maxime Roussin-Bélanger
Committed by
Sander van Harmelen
Feb 20, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add how many results and pages there are for a paginated api (#345)
parent
d2dc01ef
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
42 deletions
+35
-42
gitlab.go
gitlab.go
+35
-42
No files found.
gitlab.go
View file @
f8137824
...
...
@@ -38,6 +38,13 @@ const (
libraryVersion
=
"0.2.0"
defaultBaseURL
=
"https://gitlab.com/api/v4/"
userAgent
=
"go-gitlab/"
+
libraryVersion
headerNextPage
=
"X-Next-Page"
headerTotalPages
=
"X-Total-Pages"
headertotalResults
=
"X-Total"
headerCurrentPage
=
"X-Page"
headerPerPage
=
"X-Per-Page"
headerPrevPage
=
"X-Prev-Page"
)
// tokenType represents a token type within GitLab.
...
...
@@ -492,10 +499,12 @@ type Response struct {
// responses that are not part of a paginated set, or for which there
// are no additional pages.
NextPage
int
PrevPage
int
FirstPage
int
LastPage
int
NextPage
int
PrevPage
int
CurrentPage
int
PerPage
int
TotalPages
int
TotalResults
int
}
// newResponse creates a new Response for the provided http.Response.
...
...
@@ -508,44 +517,28 @@ func newResponse(r *http.Response) *Response {
// populatePageValues parses the HTTP Link response headers and populates the
// various pagination link values in the Response.
func
(
r
*
Response
)
populatePageValues
()
{
if
links
,
ok
:=
r
.
Response
.
Header
[
"Link"
];
ok
&&
len
(
links
)
>
0
{
for
_
,
link
:=
range
strings
.
Split
(
links
[
0
],
","
)
{
segments
:=
strings
.
Split
(
strings
.
TrimSpace
(
link
),
";"
)
// link must at least have href and rel
if
len
(
segments
)
<
2
{
continue
}
// ensure href is properly formatted
if
!
strings
.
HasPrefix
(
segments
[
0
],
"<"
)
||
!
strings
.
HasSuffix
(
segments
[
0
],
">"
)
{
continue
}
// try to pull out page parameter
url
,
err
:=
url
.
Parse
(
segments
[
0
][
1
:
len
(
segments
[
0
])
-
1
])
if
err
!=
nil
{
continue
}
page
:=
url
.
Query
()
.
Get
(
"page"
)
if
page
==
""
{
continue
}
for
_
,
segment
:=
range
segments
[
1
:
]
{
switch
strings
.
TrimSpace
(
segment
)
{
case
`rel="next"`
:
r
.
NextPage
,
_
=
strconv
.
Atoi
(
page
)
case
`rel="prev"`
:
r
.
PrevPage
,
_
=
strconv
.
Atoi
(
page
)
case
`rel="first"`
:
r
.
FirstPage
,
_
=
strconv
.
Atoi
(
page
)
case
`rel="last"`
:
r
.
LastPage
,
_
=
strconv
.
Atoi
(
page
)
}
}
}
if
totalPages
:=
r
.
Response
.
Header
.
Get
(
headerTotalPages
);
totalPages
!=
""
{
r
.
TotalPages
,
_
=
strconv
.
Atoi
(
totalPages
)
}
if
totalResults
:=
r
.
Response
.
Header
.
Get
(
headertotalResults
);
totalResults
!=
""
{
r
.
TotalResults
,
_
=
strconv
.
Atoi
(
totalResults
)
}
if
nextPage
:=
r
.
Response
.
Header
.
Get
(
headerNextPage
);
nextPage
!=
""
{
r
.
NextPage
,
_
=
strconv
.
Atoi
(
nextPage
)
}
if
currentPage
:=
r
.
Response
.
Header
.
Get
(
headerCurrentPage
);
currentPage
!=
""
{
r
.
CurrentPage
,
_
=
strconv
.
Atoi
(
currentPage
)
}
if
perPage
:=
r
.
Response
.
Header
.
Get
(
headerPerPage
);
perPage
!=
""
{
r
.
PerPage
,
_
=
strconv
.
Atoi
(
perPage
)
}
if
prevPage
:=
r
.
Response
.
Header
.
Get
(
headerPrevPage
);
prevPage
!=
""
{
r
.
PrevPage
,
_
=
strconv
.
Atoi
(
prevPage
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment