Utilize ApplicationConfig["API_PER_PAGE_MAX"] (#18394)

* add ENV["API_PER_PAGE_LIMIT"]

* cast to int

* add private_constant PER_PAGE_MAX

* use 1000 int

* typo

* use reduced count for ListingsController

* typo

* set max/default in ArticleApiIndexService

* update initializer in ArticleApiIndexService

* Update app/controllers/concerns/api/articles_controller.rb

Co-authored-by: Fernando Valverde <fernando@visualcosita.com>

* Apply suggestions from code review

Co-authored-by: Fernando Valverde <fernando@visualcosita.com>

* update API docs

Co-authored-by: Fernando Valverde <fernando@visualcosita.com>
This commit is contained in:
Andy George 2022-08-30 13:46:39 -05:00 committed by GitHub
parent de4acd4fa5
commit be27fe74ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 16 deletions

View file

@ -23,6 +23,9 @@ module Api
].freeze
private_constant :ME_ATTRIBUTES_FOR_SERIALIZATION
PER_PAGE_MAX = (ApplicationConfig["API_PER_PAGE_MAX"] || 1000).to_i
private_constant :PER_PAGE_MAX
def index
@articles = ArticleApiIndexService.new(params).get
@articles = @articles.select(INDEX_ATTRIBUTES_FOR_SERIALIZATION).decorate
@ -81,7 +84,7 @@ module Api
def me
per_page = (params[:per_page] || 30).to_i
num = [per_page, 1000].min
num = [per_page, PER_PAGE_MAX].min
@articles = case params[:status]
when "published"

View file

@ -13,6 +13,9 @@ module Api
].freeze
private_constant :ATTRIBUTES_FOR_SERIALIZATION
PER_PAGE_MAX = (ApplicationConfig["API_PER_PAGE_MAX"] || 100).to_i
private_constant :PER_PAGE_MAX
def index
@listings = Listing.published
.select(ATTRIBUTES_FOR_SERIALIZATION)
@ -24,7 +27,7 @@ module Api
@listings = @listings.order(bumped_at: :desc)
per_page = (params[:per_page] || 30).to_i
num = [per_page, 100].min
num = [per_page, PER_PAGE_MAX].min
page = params[:page] || 1
@listings = @listings.page(page).per(num)

View file

@ -20,6 +20,9 @@ module Api
].freeze
private_constant :LISTINGS_FOR_SERIALIZATION
PER_PAGE_MAX = (ApplicationConfig["API_PER_PAGE_MAX"] || 1000).to_i
private_constant :PER_PAGE_MAX
ARTICLES_FOR_SERIALIZATION = Api::V0::ArticlesController::INDEX_ATTRIBUTES_FOR_SERIALIZATION
def show
@ -29,7 +32,7 @@ module Api
def users
per_page = (params[:per_page] || 30).to_i
num = [per_page, 1000].min
num = [per_page, PER_PAGE_MAX].min
page = params[:page] || 1
@users = @organization.users.joins(:profile).select(USERS_FOR_SERIALIZATION).page(page).per(num)
@ -37,7 +40,7 @@ module Api
def listings
per_page = (params[:per_page] || 30).to_i
num = [per_page, 1000].min
num = [per_page, PER_PAGE_MAX].min
page = params[:page] || 1
@listings = @organization.listings.published
@ -50,7 +53,7 @@ module Api
def articles
per_page = (params[:per_page] || 30).to_i
num = [per_page, 1000].min
num = [per_page, PER_PAGE_MAX].min
page = params[:page] || 1
@articles = @organization.articles.published

View file

@ -2,10 +2,13 @@ module Api
module PodcastEpisodesController
extend ActiveSupport::Concern
PER_PAGE_MAX = (ApplicationConfig["API_PER_PAGE_MAX"] || 1000).to_i
private_constant :PER_PAGE_MAX
def index
page = params[:page]
per_page = (params[:per_page] || 30).to_i
num = [per_page, 1000].min
num = [per_page, PER_PAGE_MAX].min
if params[:username]
podcast = Podcast.available.find_by!(slug: params[:username])

View file

@ -5,10 +5,13 @@ module Api
ATTRIBUTES_FOR_SERIALIZATION = %i[id name bg_color_hex text_color_hex].freeze
private_constant :ATTRIBUTES_FOR_SERIALIZATION
PER_PAGE_MAX = (ApplicationConfig["API_PER_PAGE_MAX"] || 1000).to_i
private_constant :PER_PAGE_MAX
def index
page = params[:page]
per_page = (params[:per_page] || 10).to_i
num = [per_page, 1000].min
num = [per_page, PER_PAGE_MAX].min
@tags = Tag.select(ATTRIBUTES_FOR_SERIALIZATION)
.order(taggings_count: :desc)

View file

@ -7,10 +7,13 @@ module Api
].freeze
private_constant :INDEX_ATTRIBUTES_FOR_SERIALIZATION
PER_PAGE_MAX = (ApplicationConfig["API_PER_PAGE_MAX"] || 1000).to_i
private_constant :PER_PAGE_MAX
def index
page = params[:page]
per_page = (params[:per_page] || 24).to_i
num = [per_page, 1000].min
num = [per_page, PER_PAGE_MAX].min
@video_articles = Article.with_video
.includes([:user])

View file

@ -1,6 +1,6 @@
class ArticleApiIndexService
DEFAULT_PER_PAGE = 30
MAX_PER_PAGE = 1000
MAX_PER_PAGE = (ApplicationConfig["API_PER_PAGE_MAX"] || 1000).to_i
DEFAULT_PER_PAGE = [30, MAX_PER_PAGE].min
def initialize(params)
@page = params[:page]
@ -12,7 +12,7 @@ class ArticleApiIndexService
@sort = params[:sort]
@top = params[:top]
@collection_id = params[:collection_id]
@per_page = params[:per_page]
@per_page = [(params[:per_page] || DEFAULT_PER_PAGE).to_i, MAX_PER_PAGE].min
end
def get

View file

@ -473,7 +473,7 @@
"in": "query",
"name": "per_page",
"required": false,
"description": "Page size (the number of items to return per page)",
"description": "Page size (the number of items to return per page). The default maximum value can be overridden by setting `ApplicationConfig[\"API_PER_PAGE_MAX\"]`.",
"schema": {
"type": "integer",
"format": "int32",
@ -486,7 +486,7 @@
"in": "query",
"name": "per_page",
"required": false,
"description": "Page size (the number of items to return per page)",
"description": "Page size (the number of items to return per page). The default maximum value can be overridden by setting `ApplicationConfig[\"API_PER_PAGE_MAX\"]`.",
"schema": {
"type": "integer",
"format": "int32",
@ -499,7 +499,7 @@
"in": "query",
"name": "per_page",
"required": false,
"description": "Page size (the number of items to return per page)",
"description": "Page size (the number of items to return per page). The default maximum value can be overridden by setting `ApplicationConfig[\"API_PER_PAGE_MAX\"]`.",
"schema": {
"type": "integer",
"format": "int32",
@ -512,7 +512,7 @@
"in": "query",
"name": "per_page",
"required": false,
"description": "Page size (the number of items to return per page)",
"description": "Page size (the number of items to return per page). The default maximum value can be overridden by setting `ApplicationConfig[\"API_PER_PAGE_MAX\"]`.",
"schema": {
"type": "integer",
"format": "int32",
@ -525,7 +525,7 @@
"in": "query",
"name": "per_page",
"required": false,
"description": "Page size (the number of items to return per page)",
"description": "Page size (the number of items to return per page). The default maximum value can be overridden by setting `ApplicationConfig[\"API_PER_PAGE_MAX\"]`.",
"schema": {
"type": "integer",
"format": "int32",