require 'net/http/persistent' module SplitIoClient module Api class Client RUBY_ENCODING = '1.9'.respond_to?(:force_encoding) 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} proxy: #{api_client.proxy}") if config.debug_enabled end rescue StandardError => e config.logger.warn("#{e}\nURL:#{url}\nparams:#{params}") raise 'Split SDK failed to connect to backend to retrieve information' end def post_api(url, config, api_key, data, headers = {}, params = {}) api_client.post(url) do |req| req.headers = common_headers(api_key, config) .merge('Content-Type' => 'application/json') .merge(headers) 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 rescue StandardError => e config.logger.warn("#{e}\nURL:#{url}\ndata:#{data}\nparams:#{params}") false end private def api_client @api_client ||= Faraday.new do |builder| builder.use SplitIoClient::FaradayMiddleware::Gzip builder.adapter :net_http_persistent end end def common_headers(api_key, config) { 'Authorization' => "Bearer #{api_key}", 'SplitSDKVersion' => "#{config.language}-#{config.version}", 'SplitSDKMachineName' => config.machine_name, 'SplitSDKMachineIP' => config.machine_ip, 'Referer' => referer(config) } end def referer(config) result = "#{config.language}-#{config.version}" result = "#{result}::#{SplitIoClient::SplitConfig.machine_hostname}" unless SplitIoClient::SplitConfig.machine_hostname == 'localhost' result end end end end