Skip to content

Commit 5013e2b

Browse files
authored
fix(scopes): prevent unnecessary HTTP call despite caching (#702)
1 parent 79b32fb commit 5013e2b

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

app/services/forest_liana/scope_manager.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,15 @@ def self.invalidate_scope_cache(rendering_id)
4949
end
5050

5151
def self.fetch_scopes(rendering_id)
52-
response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}")
53-
54-
if response.is_a?(Net::HTTPOK)
55-
Rails.cache.fetch('forest.scopes.' + rendering_id.to_s, expires_in: @@scope_cache_expiration_delta) do
56-
data = {}
57-
parse_response = JSON.parse(response.body)
58-
59-
data['scopes'] = decode_scope(parse_response['collections'])
60-
data['team'] = parse_response['team']
61-
62-
data
63-
end
64-
else
65-
raise 'Unable to fetch scopes'
52+
Rails.cache.fetch("forest.scopes.#{rendering_id}", expires_in: @@scope_cache_expiration_delta) do
53+
response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}")
54+
raise 'Unable to fetch scopes' unless response.is_a?(Net::HTTPOK)
55+
parsed_response = JSON.parse(response.body)
56+
57+
{
58+
'scopes' => decode_scope(parsed_response['collections']),
59+
'team' => parsed_response['team']
60+
}
6661
end
6762
end
6863

spec/services/forest_liana/scope_manager_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ module ForestLiana
7373
'team' => {'id' => '1', 'name' => 'Operations'},
7474
})
7575
end
76+
77+
it 'calls the API only once, even with multiple fetch_scopes calls' do
78+
described_class.fetch_scopes(rendering_id)
79+
described_class.fetch_scopes(rendering_id)
80+
81+
expect(ForestLiana::ForestApiRequester).to have_received(:get).once
82+
end
83+
7684
end
7785

7886
describe 'when scope contains dynamic values' do

0 commit comments

Comments
 (0)