Class: Featureflip::Http::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/featureflip/http/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, config) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
# File 'lib/featureflip/http/client.rb', line 8

def initialize(sdk_key, config)
  @sdk_key = sdk_key
  @config = config
  @base_url = config.base_url
end

Instance Method Details

#closeObject



56
57
58
# File 'lib/featureflip/http/client.rb', line 56

def close
  # No persistent connection to close with net/http
end

#get_flag(key) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/featureflip/http/client.rb', line 32

def get_flag(key)
  response = request(:get, "/v1/sdk/flags/#{key}")
  flag = parse_flag(JSON.parse(response.body))

  # An unevaluable enum drops the flag rather than upserting it (#2402). For a
  # delta whose whole scope is one flag that means leaving the store's previous
  # copy alone: replacing it with one this build would mis-evaluate is the outcome
  # the drop exists to prevent, and FLAG_NOT_FOUND is the honest answer if there
  # was no previous copy.
  reason = unevaluable_flag_reason(flag)
  raise UnevaluableEntityError, "flag #{key.inspect}: #{reason}" if reason

  flag
end

#get_flagsObject



14
15
16
17
# File 'lib/featureflip/http/client.rb', line 14

def get_flags
  response = request(:get, "/v1/sdk/flags")
  parse_flags_response(JSON.parse(response.body))
end

#parse_flags_response(data) ⇒ Object

Parse a GET /v1/sdk/flags-shaped snapshot into models. Reused for the connect-time sync SSE snapshot, which carries the identical payload shape inline (no extra HTTP round-trip).



22
23
24
25
26
27
28
29
30
# File 'lib/featureflip/http/client.rb', line 22

def parse_flags_response(data)
  flags = drop_unevaluable((data["flags"] || []).map { |f| parse_flag(f) }, "flag") do |flag|
    unevaluable_flag_reason(flag)
  end
  segments = drop_unevaluable((data["segments"] || []).map { |s| parse_segment(s) }, "segment") do |segment|
    unevaluable_segment_reason(segment)
  end
  [flags, segments]
end

#post_events(events) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/featureflip/http/client.rb', line 47

def post_events(events)
  # The one caller that retries inline. EventProcessor#flush drains the queue before
  # sending, so a batch only survives a failure because the processor puts it back --
  # this absorbs a single transient 5xx before that machinery is needed, which keeps
  # the common blip off the re-queue path entirely. The processor's backoff gate is
  # measured from the moment this finally gives up, not from the first attempt.
  request(:post, "/v1/sdk/events", { events: events }, retry_server_errors: true)
end