/*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