Class: OAuth2ElasticsearchTransport

Inherits:
Elasticsearch::Transport::Transport::HTTP::Faraday
  • Object
show all
Defined in:
lib/oauth2-elasticsearch-transport.rb

Constant Summary collapse

TWO_MINUTES =
120

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ OAuth2ElasticsearchTransport

Returns a new instance of OAuth2ElasticsearchTransport.



7
8
9
10
# File 'lib/oauth2-elasticsearch-transport.rb', line 7

def initialize arguments={}
  super arguments
  @client = OAuth2::Client.new arguments[:options][:oauth2][:id], arguments[:options][:oauth2][:secret], arguments[:options][:oauth2][:options]
end

Instance Method Details

#perform_request(method, path, params, body) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/oauth2-elasticsearch-transport.rb', line 12

def perform_request method, path, params, body
  unless @token && Time.now.to_i < ( @token.expires_at - TWO_MINUTES )
    @token = @client.client_credentials.get_token
    @headers = {
      'Content-Type' => 'application/json',
      'Authorization' => @token.options[:header_format] % @token.token
    }
  end

  Elasticsearch::Transport::Transport::Base.instance_method(:perform_request).bind(self).call(method, path, params, body) do |connection, url|
    connection.connection.run_request(
      method.downcase.to_sym,
      url,
      ( body ? __convert_to_json(body) : nil ),
      @headers
    )
  end
end