Sha256: 65c779fcb643a8e0d80c4396b65fb3e88028ff7be2a560c8c5f00010c1823982

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require "json"
require "net/http/persistent"
require "faraday/http_cache"

module LaunchDarkly

  class Requestor
    def initialize(sdk_key, config)
      @sdk_key = sdk_key
      @config = config
      @client = Faraday.new do |builder|
        builder.use :http_cache, store: @config.cache_store

        builder.adapter :net_http_persistent
      end
    end

    def request_all_flags()
      make_request("/sdk/latest-flags")
    end

    def request_flag(key)
      make_request("/sdk/latest-flags/" + key)
    end

    def make_request(path)
      uri = @config.base_uri + path
      res = @client.get (uri) do |req|
        req.headers["Authorization"] = @sdk_key
        req.headers["User-Agent"] = "RubyClient/" + LaunchDarkly::VERSION
        req.options.timeout = @config.read_timeout
        req.options.open_timeout = @config.connect_timeout
        if @config.proxy
          req.options.proxy = Faraday::ProxyOptions.from @config.proxy
        end
      end

      @config.logger.debug("[LDClient] Got response from uri: #{uri}\n\tstatus code: #{res.status}\n\theaders: #{res.headers}\n\tbody: #{res.body}")

      if res.status == 401
        @config.logger.error("[LDClient] Invalid SDK key")
        return nil
      end

      if res.status == 404
        @config.logger.error("[LDClient] Resource not found")
        return nil
      end

      if res.status < 200 || res.status >= 300
        @config.logger.error("[LDClient] Unexpected status code #{res.status}")
        return nil
      end

      JSON.parse(res.body, symbolize_names: true)
    end

    private :make_request

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ldclient-rb-2.2.5 lib/ldclient-rb/requestor.rb