Commit 0c5e556922b4c7ff71c6af6255a0f6783e25ca0c

Authored by Dmitriy Zaporozhets
2 parents cc03600b 76e4d94d

Merge pull request #1360 from NARKOZ/api

API pagination
Gemfile.lock
... ... @@ -198,7 +198,7 @@ GEM
198 198 httparty (0.8.3)
199 199 multi_json (~> 1.0)
200 200 multi_xml
201   - i18n (0.6.0)
  201 + i18n (0.6.1)
202 202 journey (1.0.4)
203 203 jquery-rails (2.0.2)
204 204 railties (>= 3.2.0, < 5.0)
... ... @@ -206,11 +206,10 @@ GEM
206 206 jquery-ui-rails (0.5.0)
207 207 jquery-rails
208 208 railties (>= 3.1.0)
209   - json (1.7.4)
210   - kaminari (0.13.0)
  209 + json (1.7.5)
  210 + kaminari (0.14.0)
211 211 actionpack (>= 3.0.0)
212 212 activesupport (>= 3.0.0)
213   - railties (>= 3.0.0)
214 213 kgio (2.7.4)
215 214 launchy (2.1.0)
216 215 addressable (~> 2.2.6)
... ... @@ -348,7 +347,7 @@ GEM
348 347 daemons (>= 1.0.9)
349 348 eventmachine (>= 0.12.6)
350 349 rack (>= 1.0.0)
351   - thor (0.15.4)
  350 + thor (0.16.0)
352 351 tilt (1.3.3)
353 352 treetop (1.4.10)
354 353 polyglot
... ...
config/initializers/kaminari_config.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +Kaminari.configure do |config|
  2 + config.default_per_page = 20
  3 + config.max_per_page = 100
  4 + # config.window = 4
  5 + # config.outer_window = 0
  6 + # config.left = 0
  7 + # config.right = 0
  8 + # config.page_method_name = :page
  9 + # config.param_name = :page
  10 +end
... ...
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  
... ...