Sha256: de8db2c09ed661548176ac20790fa9cbb8ac514ccb14775903aaafb20c34dd35

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

module SplitIoClient
  module Api
    class Client
      def get_api(url, config, api_key, params = {})
        api_client.get(url, params) do |req|
          req.headers = common_headers(api_key, config).merge('Accept-Encoding' => 'gzip')

          req.options.timeout = config.read_timeout
          req.options.open_timeout = config.connection_timeout

          config.logger.debug("GET #{url}") if config.debug_enabled
        end
      end

      def post_api(url, config, api_key, data, params = {})
        api_client.post(url) do |req|
          req.headers = common_headers(api_key, config).merge('Content-Type' => 'application/json')

          req.body = data.to_json

          req.options.timeout = config.read_timeout
          req.options.open_timeout = config.connection_timeout

          if config.transport_debug_enabled
            config.logger.debug("POST #{url} #{req.body}")
          elsif config.debug_enabled
            config.logger.debug("POST #{url}")
          end
        end
      end

      private

      def api_client
        @api_client ||= Faraday.new do |builder|
          builder.use FaradayMiddleware::Gzip
          builder.adapter :net_http_persistent
        end
      end

      def common_headers(api_key, config)
        {
          'Authorization' => "Bearer #{api_key}",
          'SplitSDKVersion' => SplitIoClient::SplitFactory.sdk_version,
          'SplitSDKMachineName' => config.machine_name,
          'SplitSDKMachineIP' => config.machine_ip
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
splitclient-rb-3.1.2 lib/engine/api/client.rb
splitclient-rb-3.1.1 lib/engine/api/client.rb
splitclient-rb-3.1.0 lib/engine/api/client.rb