Skip to content

Commit

Permalink
fix(scopes): prevent unnecessary HTTP call despite caching (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthv authored Dec 4, 2024
1 parent 79b32fb commit 5013e2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
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

0 comments on commit 5013e2b

Please sign in to comment.