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

Extending HttpAmazonESConnector #24

Open
theverything opened this issue Mar 9, 2017 · 0 comments
Open

Extending HttpAmazonESConnector #24

theverything opened this issue Mar 9, 2017 · 0 comments

Comments

@theverything
Copy link

This isn't an issue it's more of a recommendation to anyone using this lib.

If you need any functionality in any of the pull requests you can easily just extend the HttpAmazonESConnector class to override the request method and add any functionality you need. This is what I use and it seems to be working for me.

// AsyncHttpAmazonESConnector.js

import HttpAmazonESConnector from 'http-aws-es';

function isPromise(obj) {
  return Boolean(obj)
  && (typeof obj === 'object' || typeof obj === 'function')
  && typeof obj.then === 'function';
}

class AsyncHttpAmazonESConnector extends HttpAmazonESConnector {
  request(params, cb) {
    const credentials = this.amazonES.credentials;

    if (credentials && isPromise(credentials)) {
      credentials
        .then((creds) => {
          if (creds.needsRefresh()) {
            return creds.refreshPromise();
          }
          return creds;
        })
        .then((creds) => {
          this.creds = creds;
          super.request(params, cb);
        })
        .catch((err) => {
          cb(err);
        });
    } else {
      super.request(params, cb);
    }
  }
}

export default AsyncHttpAmazonESConnector;
// esClient.js

import elasticsearch from 'elasticsearch';
import awsSDK from 'aws-sdk';
import AsyncHttpAmazonESConnector from './AsyncHttpAmazonESConnector';

const providerChain = new awsSDK.CredentialProviderChain([
  () => new awsSDK.EnvironmentCredentials('AWS'),
  () => new awsSDK.EnvironmentCredentials('AMAZON'),
  () => new awsSDK.SharedIniFileCredentials(),
  () => ((awsSDK.ECSCredentials.prototype.getECSRelativeUri() !== undefined)
    ? new awsSDK.ECSCredentials()
    : new awsSDK.EC2MetadataCredentials()),
]);

const esClient = new elasticsearch.Client({
  host: ES_HOST,
  connectionClass: AsyncHttpAmazonESConnector,
  amazonES: {
    region: AWS_REGION,
    credentials: providerChain.resolvePromise(),
  },
});

Hope this helps anyone who is stuck.

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

No branches or pull requests

1 participant