Sha256: 015b4cb18c612c29298a0a72c59c4a91c1126d9bf5f2009a5217c1aeb4b7bd3c

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

require "uri"
require "http"

module LaunchDarkly
  # @private
  module Util
    def self.stringify_attrs(hash, attrs)
      return hash if hash.nil?
      ret = hash
      changed = false
      attrs.each do |attr|
        value = hash[attr]
        if !value.nil? && !value.is_a?(String)
          ret = hash.clone if !changed
          ret[attr] = value.to_s
          changed = true
        end
      end
      ret
    end

    def self.new_http_client(uri_s, config)
      http_client_options = {}
      if config.socket_factory
        http_client_options["socket_class"] = config.socket_factory
      end
      proxy = URI.parse(uri_s).find_proxy
      if !proxy.nil?
        http_client_options["proxy"] = {
          proxy_address: proxy.host,
          proxy_port: proxy.port,
          proxy_username: proxy.user,
          proxy_password: proxy.password
        }
      end
      return HTTP::Client.new(http_client_options)
        .timeout({
          read: config.read_timeout,
          connect: config.connect_timeout
        })
        .persistent(uri_s)
    end

    def self.log_exception(logger, message, exc)
      logger.error { "[LDClient] #{message}: #{exc.inspect}" }
      logger.debug { "[LDClient] Exception trace: #{exc.backtrace}" }
    end

    def self.http_error_recoverable?(status)
      if status >= 400 && status < 500
        status == 400 || status == 408 || status == 429
      else
        true
      end
    end

    def self.http_error_message(status, context, recoverable_message)
      desc = (status == 401 || status == 403) ? " (invalid SDK key)" : ""
      message = Util.http_error_recoverable?(status) ? recoverable_message : "giving up permanently"
      "HTTP error #{status}#{desc} for #{context} - #{message}"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
launchdarkly-server-sdk-6.4.0 lib/ldclient-rb/util.rb
launchdarkly-server-sdk-6.3.4 lib/ldclient-rb/util.rb
launchdarkly-server-sdk-6.3.3 lib/ldclient-rb/util.rb
launchdarkly-server-sdk-6.3.2 lib/ldclient-rb/util.rb