Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add client_class option #301

Closed
wants to merge 1 commit into from

Conversation

feymartynov
Copy link
Contributor

A use case for it is setting up a connection to AWS Elasticsearch.

According to elasticsearch-transport's readme one should pass a block to ::ElasticsearchClient#initialize which adds necessary authorization headers to each request. However this can't be done with chewy.

A possible hack as it was discussed in the issue #296 is to assign Thread.current[:chewy_client] somewhere (say, in Rails initializer) with a custom instance. But this would affect only the current thread.

With :client_class option it becomes possible to perfom dependency injection of a custom client class which gets instantiated in every thread you call Chewy.client.

So an example initializer for AWS tuning looks like this:

require 'faraday_middleware/aws_signers_v4'

class AwsElasticsearchClient < ::Elasticsearch::Client
  def initialize(options)
    super(options) do |f|
      f.request :aws_signers_v4,
                service_name: 'es',
                region: 'us-east-1',
                credentials: Aws::Credentials.new(
                  ENV['AWS_ACCESS_KEY'],
                  ENV['AWS_SECRET_ACCESS_KEY'])
    end
  end
end

Chewy.configuration[:chewy_client] = AwsElasticsearchClient

@davekaro
Copy link
Contributor

Awesome great idea. This would work perfect for #296

@pyromaniac
Copy link
Contributor

Hm, I was mostly thinking about transport_config settings proc, which is passed on transport initializaton.

@feymartynov
Copy link
Contributor Author

Created a new pull request for this: #302.

@morgler
Copy link

morgler commented Aug 17, 2016

Thanks for the pull request! But I have trouble using it correctly :(. I do this in my chewy.rb initializer:

  Chewy.settings = {
    host: 'http://my-es-instance-on-aws.us-east-1.es.amazonaws.com'
  }
  Chewy.configuration[:transport_options] = {
    headers: { content_type: 'application/json' },
    proc: -> (f) do
        Rails.logger.debug ">>> Calling proc for chewy client"
        f.request :aws_signers_v4,
                  service_name: 'es',
                  region: 'us-east-1',
                  credentials: Aws::Credentials.new(
                    Rails.application.secrets.aws_access_key_id,
                    Rails.application.secrets.aws_secret_access_key)
    end
  }

If I try to use chewy afterwards, I always get a Elasticsearch::Transport::Transport::Errors::Forbidden error as if I would not have supplied the AWS credentials. I inserted a log line in the proc and it is never reached.

I also deleted chewy.yml to avoid collisions of it with the initializer.

What am I doing wrong? Maybe you could add an example to the readme of how to use this proc correctly.

@morgler
Copy link

morgler commented Aug 17, 2016

Okay, rubber ducking helped ;). It seems you can't mix the configuration methods. So I had to write it all in one block:

  Chewy.settings = {
    host: 'http://my-es-instance-on-aws.us-east-1.es.amazonaws.com',
    transport_options: {
      headers: { content_type: 'application/json' },
      proc: -> (f) do
          Rails.logger.info ">>> Calling proc for chewy client"
          f.request :aws_signers_v4,
                    service_name: 'es',
                    region: 'us-east-1',
                    credentials: Aws::Credentials.new(
                      Rails.application.secrets.aws_access_key_id,
                      Rails.application.secrets.aws_secret_access_key)
      end
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants