Skip to content

Instantly share code, notes, and snippets.

@goofballLogic
Created February 15, 2017 14:00
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 goofballLogic/e1636829015a297bb329c1876b46b6e2 to your computer and use it in GitHub Desktop.
Save goofballLogic/e1636829015a297bb329c1876b46b6e2 to your computer and use it in GitHub Desktop.
/*eslint-env node*/
/*
This class is for:
1. Creating Authentication HTTP headers
2. Creating Accept-Language headers
3. Sending a HTTP request
*/
const LDServiceClient = require( "./LDServiceClient" );
function getHeaders( options ) {
const { localisation, defaultHeaders, identifier } = options;
const headers = {};
// do we have an authorization header to pass through?
const authorizationHeader = defaultHeaders.authorization || defaultHeaders.Authorization;
if ( authorizationHeader ) { headers.authorization = authorizationHeader; }
// do we have an accept language header to pass through as preferred??
const acceptLanguage = "Accept-Language";
const preferredLanguages = [ localisation.language, "en;q=0.5", "*;q=0.1" ];
headers[ acceptLanguage ] = preferredLanguages.join( ", " );
// headers for sprint sleuth traceability
// stupid Spring Could Sleuth requires (hex) numbers
const spanId = identifier.slice( identifier.indexOf( "-" ) + 1 );
const traceId = process.hrtime()[ 1 ];
Object.assign( headers, {
"X-B3-TraceId": traceId,
"X-B3-SpanId": spanId,
"X-B3-ParentSpanId": 0
} );
return headers;
}
class KMServiceClient extends LDServiceClient {
request( uri, method, options ) {
options.headers = getHeaders( options );
options.traceId = options.headers[ "X-B3-TraceId" ];
return super.request( uri, method, options )
.then( response => response.body )
.catch( e => {
const err = Error( `Error calling ${uri}: ${e.stack}` );
err.body = e.body;
throw err;
} );
}
}
module.exports = KMServiceClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment