|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "forwardable" |
| 4 | +require "singleton" |
| 5 | + |
| 6 | +require_relative "configuration" |
| 7 | +require_relative "client" |
| 8 | +require_relative "metadata" |
| 9 | +require_relative "provider/no_op_provider" |
| 10 | + |
| 11 | +module OpenFeature |
| 12 | + module SDK |
| 13 | + # API Initialization and Configuration |
| 14 | + # |
| 15 | + # Represents the entry point to the API, including configuration of <tt>Provider</tt>,<tt>Hook</tt>, |
| 16 | + # and building the <tt>Client</tt> |
| 17 | + # |
| 18 | + # To use the SDK, you can optionally configure a <tt>Provider</tt>, with <tt>Hook</tt> |
| 19 | + # |
| 20 | + # OpenFeature::SDK::API.instance.configure do |config| |
| 21 | + # config.provider = NoOpProvider.new |
| 22 | + # end |
| 23 | + # |
| 24 | + # If no provider is specified, the <tt>NoOpProvider</tt> is set as the default <tt>Provider</tt>. |
| 25 | + # Once the SDK has been configured, a client can be built |
| 26 | + # |
| 27 | + # client = OpenFeature::SDK::API.instance.build_client(name: 'my-open-feature-client') |
| 28 | + class API |
| 29 | + include Singleton |
| 30 | + extend Forwardable |
| 31 | + |
| 32 | + def_delegator :@configuration, :provider |
| 33 | + def_delegator :@configuration, :hooks |
| 34 | + def_delegator :@configuration, :context |
| 35 | + |
| 36 | + def configuration |
| 37 | + @configuration ||= Configuration.new |
| 38 | + end |
| 39 | + |
| 40 | + def configure(&block) |
| 41 | + return unless block_given? |
| 42 | + |
| 43 | + block.call(configuration) |
| 44 | + end |
| 45 | + |
| 46 | + def build_client(name: nil, version: nil) |
| 47 | + client_options = Metadata.new(name: name, version: version).freeze |
| 48 | + provider = Provider::NoOpProvider.new if provider.nil? |
| 49 | + Client.new(provider, client_options, context) |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | +end |
0 commit comments