Skip to content

Instantly share code, notes, and snippets.

@rjriel
Last active October 27, 2016 15:07
Show Gist options
  • Save rjriel/b4dea7e6a229137feba02a9f557563c8 to your computer and use it in GitHub Desktop.
Save rjriel/b4dea7e6a229137feba02a9f557563c8 to your computer and use it in GitHub Desktop.
CREATE OPTIONS FUNCTION
// This is the api key passed to the Qritter Wars REST API in the Authorization header
// for authentication
// format: base64 encoded value of <apiId>:<apiSecret>
const apiKey = new Buffer(`${config.apiId}:${config.apiSecret}`).toString('base64')
let createOptions = (endpoint, method, body) => {
// we need to return all options that the request module expects
// for an http request. 'uri' is the location of the request, 'method'
// is what http method we want to use (most likely GET or POST). headers
// are the http headers we want attached to our request
let options = {
uri: `http://${config.host}:${config.apiPort}/${endpoint}`,
method: method.toUpperCase(),
headers: {
"Authorization": `Basic ${apiKey}`,
"Content-Type": "application/json"
}
}
if (body != null) {
// if a body has been specified we want to add it to the http request
options.body = JSON.stringify(body)
}
return options
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment