Skip to content

Commit 167b601

Browse files
committed
Move to case insensitive header extraction where possible
1 parent 20f93b1 commit 167b601

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

src/lib/bluelink-regions/base.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,17 @@ export class Bluelink {
408408
})
409409
}
410410

411+
// helper function to extract a parameter from header / cookie etc - as the Bluelink API changes case frequently
412+
protected caseInsensitiveParamExtraction(key: string, data: Record<string, any>): string | undefined {
413+
if (Object.hasOwn(data, key)) return data[key] // check for exact match first
414+
415+
const lowerKey = key.toLowerCase()
416+
for (const [k, v] of Object.entries(data)) {
417+
if (lowerKey === k.toLowerCase()) return v
418+
}
419+
return undefined
420+
}
421+
411422
protected async login(): Promise<BluelinkTokens | undefined> {
412423
// implemented in country specific sub-class
413424
throw Error('Not Implemented')

src/lib/bluelink-regions/canada.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class BluelinkCanada extends Bluelink {
9898
req.method = 'GET'
9999
await req.load()
100100
for (const cookie of req.response.cookies) {
101-
if (cookie.name === 'dtCookie') {
101+
if (cookie.name.toLowerCase() === 'dtcookie') {
102102
return `dtCookie=${cookie.value}`
103103
}
104104
}
@@ -298,15 +298,6 @@ export class BluelinkCanada extends Bluelink {
298298
throw Error(error)
299299
}
300300

301-
protected getTransactionId(headers: Record<string, any>): string {
302-
// response is inconsistant re case
303-
if (Object.hasOwn(headers, 'transactionId')) return headers.transactionId
304-
if (Object.hasOwn(headers, 'transactionid')) return headers.transactionid
305-
const error = `Failed to extract transaction id from: ${JSON.stringify(headers)}`
306-
if (this.config.debugLogging) this.logger.log(error)
307-
throw Error(error)
308-
}
309-
310301
protected async pollForCommandCompletion(
311302
id: string,
312303
authCode: string,
@@ -376,8 +367,8 @@ export class BluelinkCanada extends Bluelink {
376367
validResponseFunction: this.requestResponseValid,
377368
})
378369
if (this.requestResponseValid(resp.resp, resp.json).valid) {
379-
const transactionId = this.getTransactionId(resp.resp.headers)
380-
return await this.pollForCommandCompletion(id, authCode, transactionId)
370+
const transactionId = this.caseInsensitiveParamExtraction('transactionid', resp.resp.headers)
371+
if (transactionId) return await this.pollForCommandCompletion(id, authCode, transactionId)
381372
}
382373
const error = `Failed to send lockUnlock command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
383374
if (this.config.debugLogging) this.logger.log(error)
@@ -411,8 +402,8 @@ export class BluelinkCanada extends Bluelink {
411402
validResponseFunction: this.requestResponseValid,
412403
})
413404
if (this.requestResponseValid(resp.resp, resp.json).valid) {
414-
const transactionId = this.getTransactionId(resp.resp.headers)
415-
return await this.pollForCommandCompletion(id, authCode, transactionId)
405+
const transactionId = this.caseInsensitiveParamExtraction('transactionid', resp.resp.headers)
406+
if (transactionId) return await this.pollForCommandCompletion(id, authCode, transactionId)
416407
}
417408
const error = `Failed to send charge command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
418409
if (this.config.debugLogging) this.logger.log(error)
@@ -456,8 +447,8 @@ export class BluelinkCanada extends Bluelink {
456447
validResponseFunction: this.requestResponseValid,
457448
})
458449
if (this.requestResponseValid(resp.resp, resp.json).valid) {
459-
const transactionId = this.getTransactionId(resp.resp.headers)
460-
return await this.pollForCommandCompletion(id, authCode, transactionId)
450+
const transactionId = this.caseInsensitiveParamExtraction('transactionid', resp.resp.headers)
451+
if (transactionId) return await this.pollForCommandCompletion(id, authCode, transactionId)
461452
}
462453
const error = `Failed to send climateOff command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
463454
if (this.config.debugLogging) this.logger.log(error)
@@ -480,8 +471,8 @@ export class BluelinkCanada extends Bluelink {
480471
validResponseFunction: this.requestResponseValid,
481472
})
482473
if (this.requestResponseValid(resp.resp, resp.json).valid) {
483-
const transactionId = this.getTransactionId(resp.resp.headers)
484-
return await this.pollForCommandCompletion(id, authCode, transactionId)
474+
const transactionId = this.caseInsensitiveParamExtraction('transactionid', resp.resp.headers)
475+
if (transactionId) return await this.pollForCommandCompletion(id, authCode, transactionId)
485476
}
486477
const error = `Failed to send climateOff command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
487478
if (this.config.debugLogging) this.logger.log(error)

src/lib/bluelink-regions/usa.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ export class BluelinkUSA extends Bluelink {
287287
validResponseFunction: this.requestResponseValid,
288288
})
289289
if (this.requestResponseValid(resp.resp, resp.json).valid) {
290-
const transactionId = resp.resp.headers.tmsTid as string
291-
return await this.pollForCommandCompletion(resp, transactionId)
290+
const transactionId = this.caseInsensitiveParamExtraction('tmsTid', resp.resp.headers)
291+
if (transactionId) return await this.pollForCommandCompletion(resp, transactionId)
292292
}
293293
const error = `Failed to send lockUnlock command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
294294
if (this.config.debugLogging) this.logger.log(error)
@@ -322,8 +322,8 @@ export class BluelinkUSA extends Bluelink {
322322
validResponseFunction: this.requestResponseValid,
323323
})
324324
if (this.requestResponseValid(resp.resp, resp.json).valid) {
325-
const transactionId = resp.resp.headers.tmsTid as string
326-
return await this.pollForCommandCompletion(resp, transactionId)
325+
const transactionId = this.caseInsensitiveParamExtraction('tmsTid', resp.resp.headers)
326+
if (transactionId) return await this.pollForCommandCompletion(resp, transactionId)
327327
}
328328
const error = `Failed to send charge command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
329329
if (this.config.debugLogging) this.logger.log(error)
@@ -355,8 +355,8 @@ export class BluelinkUSA extends Bluelink {
355355
validResponseFunction: this.requestResponseValid,
356356
})
357357
if (this.requestResponseValid(resp.resp, resp.json).valid) {
358-
const transactionId = resp.resp.headers.tmsTid as string
359-
return await this.pollForCommandCompletion(resp, transactionId)
358+
const transactionId = this.caseInsensitiveParamExtraction('tmsTid', resp.resp.headers)
359+
if (transactionId) return await this.pollForCommandCompletion(resp, transactionId)
360360
}
361361
const error = `Failed to send climateOn command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
362362
if (this.config.debugLogging) this.logger.log(error)
@@ -375,8 +375,8 @@ export class BluelinkUSA extends Bluelink {
375375
validResponseFunction: this.requestResponseValid,
376376
})
377377
if (this.requestResponseValid(resp.resp, resp.json).valid) {
378-
const transactionId = resp.resp.headers.tmsTid as string
379-
return await this.pollForCommandCompletion(resp, transactionId)
378+
const transactionId = this.caseInsensitiveParamExtraction('tmsTid', resp.resp.headers)
379+
if (transactionId) return await this.pollForCommandCompletion(resp, transactionId)
380380
}
381381
const error = `Failed to send climateOff command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
382382
if (this.config.debugLogging) this.logger.log(error)

0 commit comments

Comments
 (0)