Class: EsClient::Client
- Inherits:
-
Object
- Object
- EsClient::Client
- Defined in:
- lib/es_client/client.rb
Constant Summary collapse
- RETRY_TIMES =
1
- HTTP_VERBS =
%i(get post put delete head)
- SUCCESS_HTTP_CODES =
[200, 201]
Instance Method Summary collapse
- #http ⇒ Object
-
#initialize(host, options) ⇒ Client
constructor
A new instance of Client.
- #log(message, level = :info) ⇒ Object
- #reconnect! ⇒ Object
- #request(options) ⇒ Object
Constructor Details
#initialize(host, options) ⇒ Client
Returns a new instance of Client.
9 10 11 12 |
# File 'lib/es_client/client.rb', line 9 def initialize(host, ) @host = host @options = end |
Instance Method Details
#http ⇒ Object
49 50 51 |
# File 'lib/es_client/client.rb', line 49 def http @http ||= Excon.new(@host, @options) end |
#log(message, level = :info) ⇒ Object
57 58 59 |
# File 'lib/es_client/client.rb', line 57 def log(, level=:info) EsClient.logger.try!(level, ) end |
#reconnect! ⇒ Object
53 54 55 |
# File 'lib/es_client/client.rb', line 53 def reconnect! @http = nil end |
#request(options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/es_client/client.rb', line 27 def request() retry_times = 0 begin raw_response = http.request() response = ::EsClient::Response.new(raw_response.body, raw_response.status, raw_response.headers) EsClient.logger.request(http, response, ) if EsClient.logger.try!(:debug?) response rescue Excon::Errors::SocketError => e if retry_times >= RETRY_TIMES EsClient.logger.exception(e, http, ) if EsClient.logger raise end retry_times += 1 EsClient.logger.info "[#{retry_times}] Try reconnect to #{@host}" reconnect! retry rescue Excon::Errors::BadRequest => e EsClient.logger.exception(e, http, .merge(response: e.response.body)) if EsClient.logger raise end end |