Sha256: f9cc6f5bbeba834cb9d8083f0c78668268336913516bab1c5dab0dce7e65a1a1

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'logger'

module Flagsmith
  # Ruby client for flagsmith.com
  class ApiClient
    extend Forwardable

    HTTP_METHODS_ALLOW_LIST = %i[get post].freeze

    delegate HTTP_METHODS_ALLOW_LIST => :@conn

    def initialize(config)
      @conn = Faraday.new(url: config.api_url) do |f|
        build_headers(f, config)
        f.response :json, parser_options: { symbolize_names: true }
        f.adapter Faraday.default_adapter

        f.options.timeout = config.request_timeout_seconds
        configure_logger(f, config)
        configure_retries(f, config)
      end

      freeze
    end

    private

    def build_headers(faraday, config)
      faraday.headers['Accept'] = 'application/json'
      faraday.headers['Content-Type'] = 'application/json'
      faraday.headers['X-Environment-Key'] = config.environment_key
      faraday.headers.merge(config.custom_headers)
    end

    def configure_logger(faraday, config)
      faraday.response :logger, config.logger
    end

    def configure_retries(faraday, config)
      return unless config.retries

      faraday.request :retry, { max: config.retries }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
flagsmith-4.3.0 lib/flagsmith/sdk/api_client.rb
flagsmith-4.1.0 lib/flagsmith/sdk/api_client.rb
flagsmith-4.0.1 lib/flagsmith/sdk/api_client.rb
flagsmith-4.0.0 lib/flagsmith/sdk/api_client.rb
flagsmith-3.2.0 lib/flagsmith/sdk/api_client.rb
flagsmith-3.1.1 lib/flagsmith/sdk/api_client.rb
flagsmith-3.1.0 lib/flagsmith/sdk/api_client.rb
flagsmith-3.0.1 lib/flagsmith/sdk/api_client.rb
flagsmith-3.0.0 lib/flagsmith/sdk/api_client.rb