From e9129d2aa896da7eba1c2588ea3b91e635063b1c Mon Sep 17 00:00:00 2001 From: Bilal Al-Shahwany Date: Fri, 20 Oct 2023 16:32:17 -0700 Subject: [PATCH 1/3] updated engine.api.splits class --- lib/splitclient-rb/engine/api/splits.rb | 7 ++-- spec/engine/api/splits_spec.rb | 47 +++++++++++++++++++++++-- spec/test_data/splits/splits.json | 6 ++-- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/lib/splitclient-rb/engine/api/splits.rb b/lib/splitclient-rb/engine/api/splits.rb index 10d4544c..3f5c4d30 100644 --- a/lib/splitclient-rb/engine/api/splits.rb +++ b/lib/splitclient-rb/engine/api/splits.rb @@ -10,15 +10,16 @@ def initialize(api_key, config, telemetry_runtime_producer) @telemetry_runtime_producer = telemetry_runtime_producer end - def since(since, fetch_options = { cache_control_headers: false, till: nil }) + def since(since, fetch_options = { cache_control_headers: false, till: nil, sets: nil }) start = Time.now - + params = { since: since } params[:till] = fetch_options[:till] unless fetch_options[:till].nil? + params[:sets] = fetch_options[:sets].join(",") unless fetch_options[:sets].nil? response = get_api("#{@config.base_uri}/splitChanges", @api_key, params, fetch_options[:cache_control_headers]) if response.success? - result = splits_with_segment_names(response.body) + result = splits_with_segment_names(response.body) unless result[:splits].empty? @config.split_logger.log_if_debug("#{result[:splits].length} feature flags retrieved. since=#{since}") end diff --git a/spec/engine/api/splits_spec.rb b/spec/engine/api/splits_spec.rb index 73264a96..eb12be45 100644 --- a/spec/engine/api/splits_spec.rb +++ b/spec/engine/api/splits_spec.rb @@ -27,6 +27,49 @@ end end + context '#sets' do + it 'returns the splits - with sets param' do + stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1&sets=set_1') + .with(headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip', + 'Authorization' => 'Bearer', + 'Connection' => 'keep-alive', + 'Keep-Alive' => '30', + 'Splitsdkversion' => "#{config.language}-#{config.version}" + }) + .to_return(status: 200, body: splits) + + fetch_options = { cache_control_headers: false, till: nil, sets: ['set_1'] } + returned_splits = splits_api.since(-1, fetch_options) + expect(returned_splits[:segment_names]).to eq(Set.new(%w[demo employees])) + + expect(log.string).to include '2 feature flags retrieved. since=-1' + expect(log.string).to include returned_splits.to_s + end + + it 'returns the splits - with 2 sets param' do + stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1&sets=set_1,set_2') + .with(headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip', + 'Authorization' => 'Bearer', + 'Connection' => 'keep-alive', + 'Keep-Alive' => '30', + 'Splitsdkversion' => "#{config.language}-#{config.version}" + }) + .to_return(status: 200, body: splits) + + fetch_options = { cache_control_headers: false, till: nil, sets: ['set_1','set_2'] } + returned_splits = splits_api.since(-1, fetch_options) + expect(returned_splits[:segment_names]).to eq(Set.new(%w[demo employees])) + + expect(log.string).to include '2 feature flags retrieved. since=-1' + expect(log.string).to include returned_splits.to_s + end + + end + context '#since' do it 'returns the splits - checking headers when cache_control_headers is false' do stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1') @@ -59,7 +102,7 @@ }) .to_return(status: 200, body: splits) - fetch_options = { cache_control_headers: false, till: 123_123 } + fetch_options = { cache_control_headers: false, till: 123_123, sets: nil } returned_splits = splits_api.since(-1, fetch_options) expect(returned_splits[:segment_names]).to eq(Set.new(%w[demo employees])) @@ -80,7 +123,7 @@ }) .to_return(status: 200, body: splits) - fetch_options = { cache_control_headers: true, till: nil } + fetch_options = { cache_control_headers: true, till: nil, sets: nil } returned_splits = splits_api.since(-1, fetch_options) expect(returned_splits[:segment_names]).to eq(Set.new(%w[demo employees])) diff --git a/spec/test_data/splits/splits.json b/spec/test_data/splits/splits.json index a57dfe77..7bd101db 100644 --- a/spec/test_data/splits/splits.json +++ b/spec/test_data/splits/splits.json @@ -99,7 +99,8 @@ } ] } - ] + ], + "sets":["set_1"] }, { "trafficTypeName":"user", @@ -256,7 +257,8 @@ } ] } - ] + ], + "sets":["set_1","set_2"] } ], "since":-1, From fdc88cc70731226c353b489a6f426fd6a34670b2 Mon Sep 17 00:00:00 2001 From: Bilal Al-Shahwany Date: Fri, 20 Oct 2023 20:23:22 -0700 Subject: [PATCH 2/3] added exception for 409 status code --- lib/splitclient-rb/engine/api/splits.rb | 5 ++++- lib/splitclient-rb/exceptions.rb | 11 +++++++++++ spec/engine/api/splits_spec.rb | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/splitclient-rb/engine/api/splits.rb b/lib/splitclient-rb/engine/api/splits.rb index 3f5c4d30..a061fd97 100644 --- a/lib/splitclient-rb/engine/api/splits.rb +++ b/lib/splitclient-rb/engine/api/splits.rb @@ -17,8 +17,11 @@ def since(since, fetch_options = { cache_control_headers: false, till: nil, sets params[:till] = fetch_options[:till] unless fetch_options[:till].nil? params[:sets] = fetch_options[:sets].join(",") unless fetch_options[:sets].nil? response = get_api("#{@config.base_uri}/splitChanges", @api_key, params, fetch_options[:cache_control_headers]) + if response.status == 409 + @config.logger.error("Error fetching feature flags; the amount of flag sets provided are too big, causing uri length error.") + raise ApiException.new response.body, 409 + end if response.success? - result = splits_with_segment_names(response.body) unless result[:splits].empty? @config.split_logger.log_if_debug("#{result[:splits].length} feature flags retrieved. since=#{since}") diff --git a/lib/splitclient-rb/exceptions.rb b/lib/splitclient-rb/exceptions.rb index 493045a1..f8b9c62d 100644 --- a/lib/splitclient-rb/exceptions.rb +++ b/lib/splitclient-rb/exceptions.rb @@ -12,4 +12,15 @@ def initialize(event) @event = event end end + + class ApiException < SplitIoError + def initialize(msg, exception_code) + @@exception_code = exception_code + super(msg) + end + def exception_code + @@exception_code + end + end + end diff --git a/spec/engine/api/splits_spec.rb b/spec/engine/api/splits_spec.rb index eb12be45..9ae5ca6f 100644 --- a/spec/engine/api/splits_spec.rb +++ b/spec/engine/api/splits_spec.rb @@ -68,6 +68,29 @@ expect(log.string).to include returned_splits.to_s end + it 'raise api exception when status 409' do + stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1&sets=set_1,set_2') + .with(headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip', + 'Authorization' => 'Bearer', + 'Connection' => 'keep-alive', + 'Keep-Alive' => '30', + 'Splitsdkversion' => "#{config.language}-#{config.version}" + }) + .to_return(status: 409, body: splits) + + fetch_options = { cache_control_headers: false, till: nil, sets: ['set_1','set_2'] } + captured = 0 + begin + returned_splits = splits_api.since(-1, fetch_options) + rescue SplitIoClient::ApiException => e + captured = e.exception_code + end + expect(captured).to eq(409) + end + + end context '#since' do From 70b343617a68b81285e441acbcc30892ac219f4e Mon Sep 17 00:00:00 2001 From: Bilal Al-Shahwany Date: Tue, 24 Oct 2023 08:33:36 -0700 Subject: [PATCH 3/3] updated error code --- lib/splitclient-rb/engine/api/splits.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/splitclient-rb/engine/api/splits.rb b/lib/splitclient-rb/engine/api/splits.rb index a061fd97..4d01a1bb 100644 --- a/lib/splitclient-rb/engine/api/splits.rb +++ b/lib/splitclient-rb/engine/api/splits.rb @@ -17,9 +17,9 @@ def since(since, fetch_options = { cache_control_headers: false, till: nil, sets params[:till] = fetch_options[:till] unless fetch_options[:till].nil? params[:sets] = fetch_options[:sets].join(",") unless fetch_options[:sets].nil? response = get_api("#{@config.base_uri}/splitChanges", @api_key, params, fetch_options[:cache_control_headers]) - if response.status == 409 + if response.status == 414 @config.logger.error("Error fetching feature flags; the amount of flag sets provided are too big, causing uri length error.") - raise ApiException.new response.body, 409 + raise ApiException.new response.body, 414 end if response.success? result = splits_with_segment_names(response.body)