Commit 76e4d94d432b19d7dd58503c5eac46811630c04a

Authored by Nihad Abbasov
1 parent 6817a6a2

add pagination to API

doc/api/README.md
... ... @@ -23,6 +23,13 @@ GET http://example.com/api/v2/projects?private_token=QVy1PB7sTxfy4pqfZM1U
23 23  
24 24 The API uses JSON to serialize data. You don't need to specify `.json` at the end of API URL.
25 25  
  26 +#### Pagination
  27 +
  28 +When listing resources you can pass the following parameters:
  29 +
  30 ++ `page` (default: `1`) - page number
  31 ++ `per_page` (default: `20`, max: `100`) - how many items to list per page
  32 +
26 33 ## Contents
27 34  
28 35 + [Users](https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/users.md)
... ...
lib/api/helpers.rb
... ... @@ -14,6 +14,10 @@ module Gitlab
14 14 @project
15 15 end
16 16  
  17 + def paginate(object)
  18 + object.page(params[:page]).per(params[:per_page].to_i)
  19 + end
  20 +
17 21 def authenticate!
18 22 error!({'message' => '401 Unauthorized'}, 401) unless current_user
19 23 end
... ...
lib/api/issues.rb
... ... @@ -9,7 +9,7 @@ module Gitlab
9 9 # Example Request:
10 10 # GET /issues
11 11 get do
12   - present current_user.issues, with: Entities::Issue
  12 + present paginate(current_user.issues), with: Entities::Issue
13 13 end
14 14 end
15 15  
... ... @@ -21,7 +21,7 @@ module Gitlab
21 21 # Example Request:
22 22 # GET /projects/:id/issues
23 23 get ":id/issues" do
24   - present user_project.issues, with: Entities::Issue
  24 + present paginate(user_project.issues), with: Entities::Issue
25 25 end
26 26  
27 27 # Get a single project issue
... ...
lib/api/milestones.rb
... ... @@ -11,7 +11,7 @@ module Gitlab
11 11 # Example Request:
12 12 # GET /projects/:id/milestones
13 13 get ":id/milestones" do
14   - present user_project.milestones, with: Entities::Milestone
  14 + present paginate(user_project.milestones), with: Entities::Milestone
15 15 end
16 16  
17 17 # Get a single project milestone
... ...
lib/api/projects.rb
... ... @@ -9,7 +9,7 @@ module Gitlab
9 9 # Example Request:
10 10 # GET /projects
11 11 get do
12   - @projects = current_user.projects
  12 + @projects = paginate current_user.projects
13 13 present @projects, with: Entities::Project
14 14 end
15 15  
... ...
lib/api/users.rb
... ... @@ -9,7 +9,7 @@ module Gitlab
9 9 # Example Request:
10 10 # GET /users
11 11 get do
12   - @users = User.all
  12 + @users = paginate User
13 13 present @users, with: Entities::User
14 14 end
15 15  
... ...