Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
046fa9bd
Commit
046fa9bd
authored
Mar 31, 2014
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5469 from NARKOZ/api-pagination-headers
add 'Link' header for API response
parents
8769b7ab
0678b8a4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
2 deletions
+24
-2
lib/api/helpers.rb
lib/api/helpers.rb
+18
-2
spec/requests/api/issues_spec.rb
spec/requests/api/issues_spec.rb
+6
-0
No files found.
lib/api/helpers.rb
View file @
046fa9bd
...
...
@@ -56,8 +56,12 @@ module API
end
end
def
paginate
(
object
)
object
.
page
(
params
[
:page
]).
per
(
params
[
:per_page
].
to_i
)
def
paginate
(
relation
)
per_page
=
params
[
:per_page
].
to_i
paginated
=
relation
.
page
(
params
[
:page
]).
per
(
per_page
)
add_pagination_headers
(
paginated
,
per_page
)
paginated
end
def
authenticate!
...
...
@@ -134,6 +138,18 @@ module API
private
def
add_pagination_headers
(
paginated
,
per_page
)
request_url
=
request
.
url
.
split
(
'?'
).
first
links
=
[]
links
<<
%(<#{request_url}?page=#{paginated.current_page - 1}&per_page=#{per_page}>; rel="prev")
unless
paginated
.
first_page?
links
<<
%(<#{request_url}?page=#{paginated.current_page + 1}&per_page=#{per_page}>; rel="next")
unless
paginated
.
last_page?
links
<<
%(<#{request_url}?page=1&per_page=#{per_page}>; rel="first")
links
<<
%(<#{request_url}?page=#{paginated.total_pages}&per_page=#{per_page}>; rel="last")
header
'Link'
,
links
.
join
(
', '
)
end
def
abilities
@abilities
||=
begin
abilities
=
Six
.
new
...
...
spec/requests/api/issues_spec.rb
View file @
046fa9bd
...
...
@@ -25,6 +25,12 @@ describe API::API do
json_response
.
should
be_an
Array
json_response
.
first
[
'title'
].
should
==
issue
.
title
end
it
"should add pagination headers"
do
get
api
(
"/issues?per_page=3"
,
user
)
response
.
headers
[
'Link'
].
should
==
'<http://www.example.com/api/v3/issues?page=1&per_page=3>; rel="first", <http://www.example.com/api/v3/issues?page=1&per_page=3>; rel="last"'
end
end
end
...
...
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