Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scopes): prevent unnecessary HTTP call despite caching #702

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions app/services/forest_liana/scope_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,15 @@ def self.invalidate_scope_cache(rendering_id)
end

def self.fetch_scopes(rendering_id)
response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}")

if response.is_a?(Net::HTTPOK)
Rails.cache.fetch('forest.scopes.' + rendering_id.to_s, expires_in: @@scope_cache_expiration_delta) do
data = {}
parse_response = JSON.parse(response.body)

data['scopes'] = decode_scope(parse_response['collections'])
data['team'] = parse_response['team']

data
end
else
raise 'Unable to fetch scopes'
Rails.cache.fetch("forest.scopes.#{rendering_id}", expires_in: @@scope_cache_expiration_delta) do
response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}")
raise 'Unable to fetch scopes' unless response.is_a?(Net::HTTPOK)
parsed_response = JSON.parse(response.body)

{
'scopes' => decode_scope(parsed_response['collections']),
'team' => parsed_response['team']
}
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/services/forest_liana/scope_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ module ForestLiana
'team' => {'id' => '1', 'name' => 'Operations'},
})
end

it 'calls the API only once, even with multiple fetch_scopes calls' do
described_class.fetch_scopes(rendering_id)
described_class.fetch_scopes(rendering_id)

expect(ForestLiana::ForestApiRequester).to have_received(:get).once
end

end

describe 'when scope contains dynamic values' do
Expand Down
Loading