Skip to content

Commit

Permalink
Paginate users before making the request to avoid 414 (#507)
Browse files Browse the repository at this point in the history
Making a single request with 1000 user ids, for example, is a large URL
repeated many times in validation, which lead to 414 errors. Paginating
the users and breaking this request down into many makes more sense
because the number of requests won't change, and the URL won't be as
big.
  • Loading branch information
xjunior authored Feb 7, 2025
1 parent 57f0f4f commit 8699837
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 11 deletions.
10 changes: 6 additions & 4 deletions audiences/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
audiences (1.5.3)
audiences (1.5.4)
rails (>= 6.0)

GEM
Expand Down Expand Up @@ -121,7 +121,6 @@ GEM
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.8)
minitest (5.25.4)
mysql2 (0.5.6)
net-imap (0.4.17)
Expand All @@ -134,8 +133,11 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.4)
nokogiri (1.16.8)
mini_portile2 (~> 2.8.2)
nokogiri (1.16.8-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.8-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.8-x86_64-linux)
racc (~> 1.4)
parallel (1.26.3)
parser (3.3.6.0)
Expand Down
8 changes: 5 additions & 3 deletions audiences/app/models/audiences/external_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class ExternalUser < ApplicationRecord
inverse_of: false
end

def self.fetch(external_ids)
def self.fetch(external_ids, count: 100)
return [] unless external_ids.any?

filter = Array(external_ids).map { "externalId eq #{_1}" }.join(" OR ")
Audiences::Scim.resource(:Users).all(filter: filter)
Array(external_ids).in_groups_of(count, false).flat_map do |ids|
filter = Array(ids).map { "externalId eq #{_1}" }.join(" OR ")
Audiences::Scim.resource(:Users).all(count: count, filter: filter).to_a
end
end

def self.wrap(resources)
Expand Down
3 changes: 3 additions & 0 deletions audiences/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Unreleased

# Version 1.5.4 (2024-12-19)

- Fix `authenticate` / `authentication` configuration [#481](https://github.com/powerhome/audiences/pull/481)
- Paginage user requests in audiences instead of SCIM [#507](https://github.com/powerhome/audiences/pull/507)

# Version 1.5.3 (2024-12-19)

Expand Down
2 changes: 1 addition & 1 deletion audiences/gemfiles/rails_6_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
audiences (1.5.3)
audiences (1.5.4)
rails (>= 6.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion audiences/gemfiles/rails_7_0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
audiences (1.5.3)
audiences (1.5.4)
rails (>= 6.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion audiences/gemfiles/rails_7_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
audiences (1.5.3)
audiences (1.5.4)
rails (>= 6.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion audiences/lib/audiences/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Audiences
VERSION = "1.5.3"
VERSION = "1.5.4"
end
1 change: 1 addition & 0 deletions audiences/spec/controllers/contexts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
stub_request(:get, "http://example.com/scim/v2/Users")
.with(query: {
attributes: "id,externalId,displayName,active,photos.type,photos.value",
count: 100,
filter: "(active eq true) and (externalId eq 123)",
})
.to_return(status: 200, body: { "Resources" => [{ "displayName" => "John Doe", "confidential" => "data",
Expand Down
1 change: 1 addition & 0 deletions audiences/spec/lib/audiences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
stub_request(:get, "http://example.com/scim/v2/Users")
.with(query: {
attributes: "id,externalId,displayName,active,photos.type,photos.value",
count: 100,
filter: "(active eq true) and (externalId eq 678 OR externalId eq 321)",
})
.to_return(status: 200, body: { "Resources" => [{ "displayName" => "John", "externalId" => 678 },
Expand Down

0 comments on commit 8699837

Please sign in to comment.