Skip to content

Instantly share code, notes, and snippets.

@reconbot
Last active December 19, 2016 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reconbot/75e52b1304b215feaf39bfa6f37f463c to your computer and use it in GitHub Desktop.
Save reconbot/75e52b1304b215feaf39bfa6f37f463c to your computer and use it in GitHub Desktop.
Trying to make aws4 work with got and elastic search
import AWS from 'aws-sdk'
import aws4 from 'aws4'
import bole from 'bole'
import got from 'got'
import Promise from 'bluebird'
import config from './config'
const logger = bole('es-client')
const { region, host } = config
const awsConfig = new AWS.Config({ region })
function request (options) {
const opts = Object.assign({
host,
region: awsConfig.region,
protocol: 'https:',
headers: {
'Content-Type': 'application/json'
},
json: true
}, options)
aws4.sign(opts, awsConfig.credentials)
const { method, path } = opts
logger.debug({ method, path, host }, 'Performing request')
return Promise.resolve(got(opts)).then(resp => resp.body)
}
const es = {}
const METHODS = [
'get',
'post',
'put',
'delete'
]
METHODS.forEach(method => {
es[method] = (path, body) => request({
path,
method: method.toUpperCase(),
body: JSON.stringify(body)
})
})
es.bulk = ops => request({
path: '/_bulk',
method: 'POST',
body: ops.map(op => `${JSON.stringify(op)}\n`).join('')
})
es.getRecord = ({index, type, id}) => es.get(`${index}/${type}/${id}`)
es.indexRecord = ({index, type, id, body}) => es.put(`/${index}/${type}/${id}`, body)
es.deleteRecord = ({index, type, id}) => es.delete(`/${index}/${type}/${id}`)
export default es
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment