Skip to content

Commit

Permalink
Merge pull request #483 from splitio/flagsets-engine-api
Browse files Browse the repository at this point in the history
updated engine.api.splits class
  • Loading branch information
chillaq committed Oct 24, 2023
2 parents 2c32af6 + 70b3436 commit 3908a97
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/splitclient-rb/engine/api/splits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ 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.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, 414
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}")
end
Expand Down
11 changes: 11 additions & 0 deletions lib/splitclient-rb/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
70 changes: 68 additions & 2 deletions spec/engine/api/splits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,72 @@
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

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
it 'returns the splits - checking headers when cache_control_headers is false' do
stub_request(:get, 'https://sdk.split.io/api/splitChanges?since=-1')
Expand Down Expand Up @@ -59,7 +125,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]))

Expand All @@ -80,7 +146,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]))

Expand Down
6 changes: 4 additions & 2 deletions spec/test_data/splits/splits.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
}
]
}
]
],
"sets":["set_1"]
},
{
"trafficTypeName":"user",
Expand Down Expand Up @@ -256,7 +257,8 @@
}
]
}
]
],
"sets":["set_1","set_2"]
}
],
"since":-1,
Expand Down

0 comments on commit 3908a97

Please sign in to comment.