Class: OpenFeature::SDK::Contrib::Providers::HttpProvider

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/open_feature/sdk/contrib/providers/http_provider.rb

Overview

Read feature flags from a URL.

To use FileProvider, it can be set during the configuration of the SDK. The ‘extra_options` hash can be anything passed to `Faraday.new“

OpenFeature::SDK.configure do |config|
  config.provider = OpenFeature::SDK::Contrib::Providers::HttpProvider.new(source: http://path_to_flags, extra_options: { headers: { Auhorization: "token" }})
end

Within the FileProvider, the following methods exist

  • fetch_boolean_value - Retrieve feature flag boolean value from the file

  • fetch_string_value - Retrieve feature flag string value from the file

  • fetch_number_value - Retrieve feature flag number value from the file

  • fetch_object_value - Retrieve feature flag object value from the file

Constant Summary collapse

NAME =
"Http Provider"

Instance Attribute Summary

Attributes included from Common

#cache_duration, #custom_parser, #deep_keys, #extra_options, #format, #metadata, #source

Instance Method Summary collapse

Methods included from Common

#expire_cache!, #fetch_boolean_value, #fetch_float_value, #fetch_number_value, #fetch_object_value, #fetch_raw_key, #fetch_string_value, #initialize, #read_all_values_with_cache

Instance Method Details

#read_and_parse_flagsObject



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
# File 'lib/open_feature/sdk/contrib/providers/http_provider.rb', line 32

def read_and_parse_flags
  options                 = extra_options.clone
  headers                 = options.fetch(:headers, {})
  uri                     = URI(source)
  base_url                = "#{uri.scheme}://#{uri.host}:#{uri.port}"
  authentication_strategy = options.delete(:authentication_strategy)
  logger                  = options.delete(:logger)

  if format == :yaml
    headers["Content-Type"] ||= "application/yaml"
  elsif format == :json
    headers["Content-Type"] ||= "application/json"
  end

  client = Faraday.new(url: base_url, **options, headers: headers) do |conn|
    conn.request(*authentication_strategy) if authentication_strategy
    conn.response(:logger, logger) if logger
  end

  res = client.get(uri.request_uri)

  return custom_parser.call(res.body) if custom_parser

  begin
    @flag_contents = if format == :yaml
                       YAML.safe_load(res.body)
                     else
                       JSON.parse(res.body)
                     end
  rescue StandardError
    @flag_contents = {}
  end

  @flag_contents
end