Class: SplitIoClient::Api::Splits

Inherits:
Client
  • Object
show all
Defined in:
lib/splitclient-rb/engine/api/splits.rb

Overview

Retrieves split definitions from the Split Backend

Constant Summary collapse

PROXY_CHECK_INTERVAL_SECONDS =
24 * 60 * 60
SPEC_1_1 =
"1.1"

Instance Method Summary collapse

Methods inherited from Client

#get_api, #post_api, #sdk_url_overriden?

Constructor Details

#initialize(api_key, config, telemetry_runtime_producer) ⇒ Splits

Returns a new instance of Splits.



11
12
13
14
15
16
17
18
19
20
# File 'lib/splitclient-rb/engine/api/splits.rb', line 11

def initialize(api_key, config, telemetry_runtime_producer)
  super(config)
  @api_key = api_key
  @telemetry_runtime_producer = telemetry_runtime_producer
  @flag_sets_filter = @config.flag_sets_filter
  @spec_version = SplitIoClient::Spec::FeatureFlags::SPEC_VERSION
  @last_proxy_check_timestamp = 0
  @clear_storage = false
  @old_spec_since = nil
end

Instance Method Details

#clear_storageObject



71
72
73
# File 'lib/splitclient-rb/engine/api/splits.rb', line 71

def clear_storage
  @clear_storage
end

#since(since, since_rbs, fetch_options = { cache_control_headers: false, till: nil, sets: nil}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/splitclient-rb/engine/api/splits.rb', line 22

def since(since, since_rbs, fetch_options = { cache_control_headers: false, till: nil, sets: nil})
  start = Time.now
  
  if check_last_proxy_check_timestamp
      @spec_version = SplitIoClient::Spec::FeatureFlags::SPEC_VERSION
      @config.logger.debug("Switching to new Feature flag spec #{@spec_version} and fetching.")
      @old_spec_since = since
      since = -1
      since_rbs = -1
      fetch_options = { cache_control_headers: false, till: nil, sets: nil}
  end

  if @spec_version == Splits::SPEC_1_1
    since = @old_spec_since unless @old_spec_since.nil?
    params = { s: @spec_version, since: since }
    @old_spec_since = nil
  else
    params = { s: @spec_version, since: since, rbSince: since_rbs }
  end

  params[:sets] = @flag_sets_filter.join(",") unless @flag_sets_filter.empty?
  params[:till] = fetch_options[:till] unless fetch_options[:till].nil?
  @config.logger.debug("Fetching from splitChanges with #{params}: ")
  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.status == 400 and sdk_url_overriden? and @spec_version == SplitIoClient::Spec::FeatureFlags::SPEC_VERSION
      @config.logger.warn("Detected proxy response error, changing spec version from #{@spec_version} to #{Splits::SPEC_1_1} and re-fetching.")
      @spec_version = Splits::SPEC_1_1
      @last_proxy_check_timestamp = Time.now
      return since(since, 0, fetch_options = {cache_control_headers: fetch_options[:cache_control_headers], till: fetch_options[:till],
          sets: fetch_options[:sets]})          
  end
  if response.success?
    result = JSON.parse(response.body, symbolize_names: true)

    return process_result(result, since, since_rbs, start)
  end
  @telemetry_runtime_producer.record_sync_error(Telemetry::Domain::Constants::SPLIT_SYNC, response.status)

  @config.logger.error("Unexpected status code while fetching feature flags: #{response.status}. " \
  'Check your API key and base URI')

  raise 'Split SDK failed to connect to backend to fetch feature flags definitions'
end