Commit 0c5e556922b4c7ff71c6af6255a0f6783e25ca0c
Exists in
master
and in
4 other branches
Merge pull request #1360 from NARKOZ/api
API pagination
Showing
8 changed files
with
30 additions
and
10 deletions
Show diff stats
Gemfile.lock
| @@ -198,7 +198,7 @@ GEM | @@ -198,7 +198,7 @@ GEM | ||
| 198 | httparty (0.8.3) | 198 | httparty (0.8.3) |
| 199 | multi_json (~> 1.0) | 199 | multi_json (~> 1.0) |
| 200 | multi_xml | 200 | multi_xml |
| 201 | - i18n (0.6.0) | 201 | + i18n (0.6.1) |
| 202 | journey (1.0.4) | 202 | journey (1.0.4) |
| 203 | jquery-rails (2.0.2) | 203 | jquery-rails (2.0.2) |
| 204 | railties (>= 3.2.0, < 5.0) | 204 | railties (>= 3.2.0, < 5.0) |
| @@ -206,11 +206,10 @@ GEM | @@ -206,11 +206,10 @@ GEM | ||
| 206 | jquery-ui-rails (0.5.0) | 206 | jquery-ui-rails (0.5.0) |
| 207 | jquery-rails | 207 | jquery-rails |
| 208 | railties (>= 3.1.0) | 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 | actionpack (>= 3.0.0) | 211 | actionpack (>= 3.0.0) |
| 212 | activesupport (>= 3.0.0) | 212 | activesupport (>= 3.0.0) |
| 213 | - railties (>= 3.0.0) | ||
| 214 | kgio (2.7.4) | 213 | kgio (2.7.4) |
| 215 | launchy (2.1.0) | 214 | launchy (2.1.0) |
| 216 | addressable (~> 2.2.6) | 215 | addressable (~> 2.2.6) |
| @@ -348,7 +347,7 @@ GEM | @@ -348,7 +347,7 @@ GEM | ||
| 348 | daemons (>= 1.0.9) | 347 | daemons (>= 1.0.9) |
| 349 | eventmachine (>= 0.12.6) | 348 | eventmachine (>= 0.12.6) |
| 350 | rack (>= 1.0.0) | 349 | rack (>= 1.0.0) |
| 351 | - thor (0.15.4) | 350 | + thor (0.16.0) |
| 352 | tilt (1.3.3) | 351 | tilt (1.3.3) |
| 353 | treetop (1.4.10) | 352 | treetop (1.4.10) |
| 354 | polyglot | 353 | polyglot |
| @@ -0,0 +1,10 @@ | @@ -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,6 +23,13 @@ GET http://example.com/api/v2/projects?private_token=QVy1PB7sTxfy4pqfZM1U | ||
| 23 | 23 | ||
| 24 | The API uses JSON to serialize data. You don't need to specify `.json` at the end of API URL. | 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 | ## Contents | 33 | ## Contents |
| 27 | 34 | ||
| 28 | + [Users](https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/users.md) | 35 | + [Users](https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/users.md) |
lib/api/helpers.rb
| @@ -14,6 +14,10 @@ module Gitlab | @@ -14,6 +14,10 @@ module Gitlab | ||
| 14 | @project | 14 | @project |
| 15 | end | 15 | end |
| 16 | 16 | ||
| 17 | + def paginate(object) | ||
| 18 | + object.page(params[:page]).per(params[:per_page].to_i) | ||
| 19 | + end | ||
| 20 | + | ||
| 17 | def authenticate! | 21 | def authenticate! |
| 18 | error!({'message' => '401 Unauthorized'}, 401) unless current_user | 22 | error!({'message' => '401 Unauthorized'}, 401) unless current_user |
| 19 | end | 23 | end |
lib/api/issues.rb
| @@ -9,7 +9,7 @@ module Gitlab | @@ -9,7 +9,7 @@ module Gitlab | ||
| 9 | # Example Request: | 9 | # Example Request: |
| 10 | # GET /issues | 10 | # GET /issues |
| 11 | get do | 11 | get do |
| 12 | - present current_user.issues, with: Entities::Issue | 12 | + present paginate(current_user.issues), with: Entities::Issue |
| 13 | end | 13 | end |
| 14 | end | 14 | end |
| 15 | 15 | ||
| @@ -21,7 +21,7 @@ module Gitlab | @@ -21,7 +21,7 @@ module Gitlab | ||
| 21 | # Example Request: | 21 | # Example Request: |
| 22 | # GET /projects/:id/issues | 22 | # GET /projects/:id/issues |
| 23 | get ":id/issues" do | 23 | get ":id/issues" do |
| 24 | - present user_project.issues, with: Entities::Issue | 24 | + present paginate(user_project.issues), with: Entities::Issue |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | # Get a single project issue | 27 | # Get a single project issue |
lib/api/milestones.rb
| @@ -11,7 +11,7 @@ module Gitlab | @@ -11,7 +11,7 @@ module Gitlab | ||
| 11 | # Example Request: | 11 | # Example Request: |
| 12 | # GET /projects/:id/milestones | 12 | # GET /projects/:id/milestones |
| 13 | get ":id/milestones" do | 13 | get ":id/milestones" do |
| 14 | - present user_project.milestones, with: Entities::Milestone | 14 | + present paginate(user_project.milestones), with: Entities::Milestone |
| 15 | end | 15 | end |
| 16 | 16 | ||
| 17 | # Get a single project milestone | 17 | # Get a single project milestone |
lib/api/projects.rb
| @@ -9,7 +9,7 @@ module Gitlab | @@ -9,7 +9,7 @@ module Gitlab | ||
| 9 | # Example Request: | 9 | # Example Request: |
| 10 | # GET /projects | 10 | # GET /projects |
| 11 | get do | 11 | get do |
| 12 | - @projects = current_user.projects | 12 | + @projects = paginate current_user.projects |
| 13 | present @projects, with: Entities::Project | 13 | present @projects, with: Entities::Project |
| 14 | end | 14 | end |
| 15 | 15 |
lib/api/users.rb
| @@ -9,7 +9,7 @@ module Gitlab | @@ -9,7 +9,7 @@ module Gitlab | ||
| 9 | # Example Request: | 9 | # Example Request: |
| 10 | # GET /users | 10 | # GET /users |
| 11 | get do | 11 | get do |
| 12 | - @users = User.all | 12 | + @users = paginate User |
| 13 | present @users, with: Entities::User | 13 | present @users, with: Entities::User |
| 14 | end | 14 | end |
| 15 | 15 |