Define common helper for describe pagination params in api

This commit is contained in:
Semyon Pupkov 2016-11-22 00:15:46 +05:00
parent 53714ddf2b
commit 77cf855bb9
8 changed files with 46 additions and 12 deletions

View File

@ -0,0 +1,4 @@
---
title: Define common helper for describe pagination params in api
merge_request: 7646
author: Semyon Pupkov

View File

@ -1,5 +1,7 @@
module API
class BroadcastMessages < Grape::API
include PaginationParams
before { authenticate! }
before { authenticated_as_admin! }
@ -15,8 +17,7 @@ module API
success Entities::BroadcastMessage
end
params do
optional :page, type: Integer, desc: 'Current page number'
optional :per_page, type: Integer, desc: 'Number of messages per page'
use :pagination
end
get do
messages = BroadcastMessage.all

View File

@ -3,6 +3,8 @@ require 'mime/types'
module API
# Projects commits API
class Commits < Grape::API
include PaginationParams
before { authenticate! }
before { authorize! :download_code, user_project }
@ -107,9 +109,8 @@ module API
failure [[404, 'Not Found']]
end
params do
use :pagination
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
optional :per_page, type: Integer, desc: 'The amount of items per page for paginaion'
optional :page, type: Integer, desc: 'The page number for pagination'
end
get ':id/repository/commits/:sha/comments' do
commit = user_project.commit(params[:sha])

View File

@ -1,6 +1,8 @@
module API
# Deployments RESTfull API endpoints
class Deployments < Grape::API
include PaginationParams
before { authenticate! }
params do
@ -12,8 +14,7 @@ module API
success Entities::Deployment
end
params do
optional :page, type: Integer, desc: 'Page number of the current request'
optional :per_page, type: Integer, desc: 'Number of items per page'
use :pagination
end
get ':id/deployments' do
authorize! :read_deployment, user_project

View File

@ -1,6 +1,8 @@
module API
# Environments RESTfull API endpoints
class Environments < Grape::API
include PaginationParams
before { authenticate! }
params do
@ -12,8 +14,7 @@ module API
success Entities::Environment
end
params do
optional :page, type: Integer, desc: 'Page number of the current request'
optional :per_page, type: Integer, desc: 'Number of items per page'
use :pagination
end
get ':id/environments' do
authorize! :read_environment, user_project

View File

@ -0,0 +1,24 @@
module API
# Concern for declare pagination params.
#
# @example
# class CustomApiResource < Grape::API
# include PaginationParams
#
# params do
# use :pagination
# end
# end
module PaginationParams
extend ActiveSupport::Concern
included do
helpers do
params :pagination do
optional :page, type: Integer, desc: 'Current page number'
optional :per_page, type: Integer, desc: 'Number of items per page'
end
end
end
end
end

View File

@ -1,5 +1,7 @@
module API
class Pipelines < Grape::API
include PaginationParams
before { authenticate! }
params do
@ -11,8 +13,7 @@ module API
success Entities::Pipeline
end
params do
optional :page, type: Integer, desc: 'Page number of the current request'
optional :per_page, type: Integer, desc: 'Number of items per page'
use :pagination
optional :scope, type: String, values: ['running', 'branches', 'tags'],
desc: 'Either running, branches, or tags'
end

View File

@ -1,6 +1,8 @@
module API
# Projects variables API
class Variables < Grape::API
include PaginationParams
before { authenticate! }
before { authorize! :admin_build, user_project }
@ -13,8 +15,7 @@ module API
success Entities::Variable
end
params do
optional :page, type: Integer, desc: 'The page number for pagination'
optional :per_page, type: Integer, desc: 'The value of items per page to show'
use :pagination
end
get ':id/variables' do
variables = user_project.variables