Skip to content

Commit

Permalink
Add request id and user agent to client headers
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <[email protected]>
  • Loading branch information
jsetton committed Nov 21, 2024
1 parent 41b05aa commit a6c80c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 1 addition & 2 deletions lambda/alexa/smarthome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

import { AxiosError } from 'axios';
import config from '#root/config.js';
import Debug from '#root/debug.js';
import log from '#root/log.js';
import OpenHAB from '#openhab/index.js';
Expand All @@ -32,7 +31,7 @@ export const handleRequest = async (request, context) => {
// Initialize directive object
const directive = new AlexaDirective(request.directive);
// Initialize openhab object
const openhab = new OpenHAB(config.openhab, debug, directive.auth.token, AlexaResponse.TIMEOUT);
const openhab = new OpenHAB(debug, context.awsRequestId, directive.auth.token, AlexaResponse.TIMEOUT);

let response;

Expand Down
15 changes: 10 additions & 5 deletions lambda/openhab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import fs from 'node:fs';
import axios from 'axios';
import { HttpsAgent } from 'agentkeepalive';
import { validate as uuidValidate } from 'uuid';
import config from '#root/config.js';
import pkg from '#root/package.json' with { type: 'json' };
import { ItemType, ItemValue, UnitSymbol } from './constants.js';

/**
Expand All @@ -30,13 +32,13 @@ export default class OpenHAB {

/**
* Constructor
* @param {Object} config
* @param {Object} debug
* @param {String} requestId
* @param {String} token
* @param {Number} timeout
*/
constructor(config, debug, token, timeout) {
this._client = OpenHAB.createClient(config, debug, token, timeout);
constructor(debug, requestId, token, timeout) {
this._client = OpenHAB.createClient(config.openhab, debug, requestId, token, timeout);
}

/**
Expand Down Expand Up @@ -207,16 +209,19 @@ export default class OpenHAB {
* Returns request client
* @param {Object} config
* @param {Object} debug
* @param {String} requestId
* @param {String} token
* @param {Number} timeout
* @return {Object}
*/
static createClient(config, debug, token, timeout) {
static createClient(config, debug, requestId, token, timeout) {
const client = axios.create({
baseURL: config.baseURL,
headers: {
common: {
'Cache-Control': 'no-cache'
'Cache-Control': 'no-cache',
'User-Agent': `${pkg.name}/${pkg.version}`,
'X-Amzn-RequestId': requestId
}
},
httpsAgent: new HttpsAgent({
Expand Down

0 comments on commit a6c80c2

Please sign in to comment.