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
Boxiang Sun
gitlab-ce
Commits
7870614f
Commit
7870614f
authored
Dec 07, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract API pagination code to a separete module
parent
840ee74e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
38 deletions
+46
-38
lib/api/helpers.rb
lib/api/helpers.rb
+1
-38
lib/api/helpers/pagination.rb
lib/api/helpers/pagination.rb
+45
-0
No files found.
lib/api/helpers.rb
View file @
7870614f
module
API
module
API
module
Helpers
module
Helpers
include
Gitlab
::
Utils
include
Gitlab
::
Utils
include
Helpers
::
Pagination
PRIVATE_TOKEN_HEADER
=
"HTTP_PRIVATE_TOKEN"
PRIVATE_TOKEN_HEADER
=
"HTTP_PRIVATE_TOKEN"
PRIVATE_TOKEN_PARAM
=
:private_token
PRIVATE_TOKEN_PARAM
=
:private_token
...
@@ -131,12 +132,6 @@ module API
...
@@ -131,12 +132,6 @@ module API
IssuesFinder
.
new
(
current_user
,
project_id:
user_project
.
id
).
find
(
id
)
IssuesFinder
.
new
(
current_user
,
project_id:
user_project
.
id
).
find
(
id
)
end
end
def
paginate
(
relation
)
relation
.
page
(
params
[
:page
]).
per
(
params
[
:per_page
].
to_i
).
tap
do
|
data
|
add_pagination_headers
(
data
)
end
end
def
authenticate!
def
authenticate!
unauthorized!
unless
current_user
unauthorized!
unless
current_user
end
end
...
@@ -367,38 +362,6 @@ module API
...
@@ -367,38 +362,6 @@ module API
private
private
def
add_pagination_headers
(
paginated_data
)
header
'X-Total'
,
paginated_data
.
total_count
.
to_s
header
'X-Total-Pages'
,
paginated_data
.
total_pages
.
to_s
header
'X-Per-Page'
,
paginated_data
.
limit_value
.
to_s
header
'X-Page'
,
paginated_data
.
current_page
.
to_s
header
'X-Next-Page'
,
paginated_data
.
next_page
.
to_s
header
'X-Prev-Page'
,
paginated_data
.
prev_page
.
to_s
header
'Link'
,
pagination_links
(
paginated_data
)
end
def
pagination_links
(
paginated_data
)
request_url
=
request
.
url
.
split
(
'?'
).
first
request_params
=
params
.
clone
request_params
[
:per_page
]
=
paginated_data
.
limit_value
links
=
[]
request_params
[
:page
]
=
paginated_data
.
current_page
-
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="prev")
unless
paginated_data
.
first_page?
request_params
[
:page
]
=
paginated_data
.
current_page
+
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="next")
unless
paginated_data
.
last_page?
request_params
[
:page
]
=
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="first")
request_params
[
:page
]
=
paginated_data
.
total_pages
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="last")
links
.
join
(
', '
)
end
def
secret_token
def
secret_token
Gitlab
::
Shell
.
secret_token
Gitlab
::
Shell
.
secret_token
end
end
...
...
lib/api/helpers/pagination.rb
0 → 100644
View file @
7870614f
module
API
module
Helpers
module
Pagination
def
paginate
(
relation
)
relation
.
page
(
params
[
:page
]).
per
(
params
[
:per_page
].
to_i
).
tap
do
|
data
|
add_pagination_headers
(
data
)
end
end
private
def
add_pagination_headers
(
paginated_data
)
header
'X-Total'
,
paginated_data
.
total_count
.
to_s
header
'X-Total-Pages'
,
paginated_data
.
total_pages
.
to_s
header
'X-Per-Page'
,
paginated_data
.
limit_value
.
to_s
header
'X-Page'
,
paginated_data
.
current_page
.
to_s
header
'X-Next-Page'
,
paginated_data
.
next_page
.
to_s
header
'X-Prev-Page'
,
paginated_data
.
prev_page
.
to_s
header
'Link'
,
pagination_links
(
paginated_data
)
end
def
pagination_links
(
paginated_data
)
request_url
=
request
.
url
.
split
(
'?'
).
first
request_params
=
params
.
clone
request_params
[
:per_page
]
=
paginated_data
.
limit_value
links
=
[]
request_params
[
:page
]
=
paginated_data
.
current_page
-
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="prev")
unless
paginated_data
.
first_page?
request_params
[
:page
]
=
paginated_data
.
current_page
+
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="next")
unless
paginated_data
.
last_page?
request_params
[
:page
]
=
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="first")
request_params
[
:page
]
=
paginated_data
.
total_pages
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="last")
links
.
join
(
', '
)
end
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