class SearchFlip::HTTPClient

The SearchFlip::HTTPClient class wraps the http gem and responsible for the http request/response handling, ie communicating with Elasticsearch. You only need to use it directly if you need authentication to communicate with Elasticsearch or if you want to set some custom http settings.

@example

http_client = SearchFlip::HTTPClient.new

# Basic Auth
http_client = http_client.basic_auth(user: "username", pass: "password")

# Raw Auth Header
http_client = http_client.auth("Bearer VGhlIEhUVFAgR2VtLCBST0NLUw")

# Proxy Settings
http_client = http_client.via("proxy.host", 8080)

# Custom headers
http_client = http_client.headers(key: "value")

# Timeouts
http_client = http_client.timeout(20)

SearchFlip::Connection.new(base_url: "...", http_client: http_client)

Attributes

plugins[RW]
request[RW]

Public Class Methods

new(plugins: []) click to toggle source
# File lib/search_flip/http_client.rb, line 30
def initialize(plugins: [])
  self.request = HTTP
  self.plugins = plugins
end

Private Instance Methods

execute(method, uri, options = {}) click to toggle source
# File lib/search_flip/http_client.rb, line 60
def execute(method, uri, options = {})
  final_request = plugins.inject(self) { |res, cur| cur.call(res, method, uri, options) }
  response = final_request.request.send(method, uri, options)

  raise SearchFlip::ResponseError.new(code: response.code, body: response.body.to_s) unless response.status.success?

  response
rescue HTTP::ConnectionError => e
  raise SearchFlip::ConnectionError, e.message
rescue HTTP::TimeoutError => e
  raise SearchFlip::TimeoutError, e.message
rescue HTTP::Error => e
  raise SearchFlip::HttpError, e.message
end