Class: AmplitudeExperiment::LocalEvaluationFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/experiment/flag/flag_config_fetcher.rb

Overview

LocalEvaluationFetcher Fetch local evaluation mode flag configs from the Experiment API server. These flag configs can be used to perform local evaluation.

Constant Summary collapse

FLAG_CONFIG_TIMEOUT =
5000

Instance Method Summary collapse

Constructor Details

#initialize(api_key, logger, server_url = 'https://api.lab.amplitude.com') ⇒ LocalEvaluationFetcher



8
9
10
11
12
# File 'lib/experiment/flag/flag_config_fetcher.rb', line 8

def initialize(api_key, logger, server_url = 'https://api.lab.amplitude.com')
  @api_key = api_key
  @server_url = server_url
  @logger = logger
end

Instance Method Details

#fetchHash

Fetch local evaluation mode flag configs from the Experiment API server. These flag configs can be used to perform local evaluation.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/experiment/flag/flag_config_fetcher.rb', line 54

def fetch
  # fetch flag_configs
  headers = {
    'Authorization' => "Api-Key #{@api_key}",
    'Content-Type' => 'application/json;charset=utf-8',
    'X-Amp-Exp-Library' => "experiment-ruby-server/#{VERSION}"
  }
  request = Net::HTTP::Get.new("#{@server_url}/sdk/rules?eval_mode=local", headers)
  http = PersistentHttpClient.get(@server_url, { read_timeout: FLAG_CONFIG_TIMEOUT }, @api_key)
  response = http.request(request)
  raise "flagConfigs - received error response: #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPOK)

  flag_configs = parse(response.body)
  @logger.debug("[Experiment] Fetch flag configs: #{request.body}")
  flag_configs
end

#fetch_v1String

Fetch local evaluation mode flag configs from the Experiment API server. These flag configs can be used to perform local evaluation.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/experiment/flag/flag_config_fetcher.rb', line 18

def fetch_v1
  # fetch flag_configs
  headers = {
    'Authorization' => "Api-Key #{@api_key}",
    'Content-Type' => 'application/json;charset=utf-8',
    'X-Amp-Exp-Library' => "experiment-ruby-server/#{VERSION}"
  }
  request = Net::HTTP::Get.new("#{@server_url}/sdk/v1/flags", headers)
  http = PersistentHttpClient.get(@server_url, { read_timeout: FLAG_CONFIG_TIMEOUT }, @api_key)
  response = http.request(request)
  raise "flagConfigs - received error response: #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPOK)

  @logger.debug("[Experiment] Fetch flag configs: #{response.body}")
  response.body
end

#fetch_v2Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/experiment/flag/flag_config_fetcher.rb', line 34

def fetch_v2
  # fetch flag_configs
  headers = {
    'Authorization' => "Api-Key #{@api_key}",
    'Content-Type' => 'application/json;charset=utf-8',
    'X-Amp-Exp-Library' => "experiment-ruby-server/#{VERSION}"
  }
  request = Net::HTTP::Get.new("#{@server_url}/sdk/v2/flags?v=0", headers)
  http = PersistentHttpClient.get(@server_url, { read_timeout: FLAG_CONFIG_TIMEOUT }, @api_key)
  response = http.request(request)
  raise "flagConfigs - received error response: #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPOK)

  @logger.debug("[Experiment] Fetch flag configs: #{response.body}")
  JSON.parse(response.body).map { |f| Evaluation::Flag.from_hash(f) }
end