module Elastic::EnterpriseSearch::Request

Module included in Elastic::Enterprise::Client for http requests.

Public Instance Methods

basic_auth_header() click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 64
def basic_auth_header
  credentials = Base64.strict_encode64("#{http_auth[:user]}:#{http_auth[:password]}")
  "Basic #{credentials}"
end
delete(path, params = {}, headers = {}) click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 44
def delete(path, params = {}, headers = {})
  request(:delete, path, params, headers)
end
get(path, params = {}, headers = {}) click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 32
def get(path, params = {}, headers = {})
  request(:get, path, params, headers)
end
post(path, params = {}, body = {}, headers = {}) click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 36
def post(path, params = {}, body = {}, headers = {})
  request(:post, path, params, body, headers)
end
put(path, params = {}, body = {}, headers = {}) click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 40
def put(path, params = {}, body = {}, headers = {})
  request(:put, path, params, body, headers)
end
request(method, path, params = {}, body = {}, headers = {}) click to toggle source

Construct and send a request to the API.

# File lib/elastic/enterprise-search/request.rb, line 49
def request(method, path, params = {}, body = {}, headers = {})
  meta_headers = { authorization: decide_authorization(params), user_agent: request_user_agent }
  headers = if !headers.is_a?(Hash)
              meta_headers
            else
              headers.merge(meta_headers)
            end

  @transport.perform_request(method.to_s.upcase, path, params, body, headers)
end
setup_authentication_header() click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 60
def setup_authentication_header
  basic_auth_header
end

Private Instance Methods

decide_authorization(params) click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 82
def decide_authorization(params)
  if params[:grant_type] == 'authorization_code'
    "Bearer #{params[:code]}"
  elsif params[:access_token]
    "Bearer #{params.delete(:access_token)}"
  else
    setup_authentication_header
  end
end
request_user_agent() click to toggle source
# File lib/elastic/enterprise-search/request.rb, line 71
def request_user_agent
  ua = "#{CLIENT_NAME}/#{CLIENT_VERSION}"
  meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
  if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
    meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} " \
            "#{RbConfig::CONFIG['target_cpu']}"
  end
  meta << "elasticsearch-transport: #{Elasticsearch::Transport::VERSION}"
  "#{ua} (#{meta.join('; ')})"
end