Skip to content

Commit

Permalink
feat(accessories): request logging
Browse files Browse the repository at this point in the history
- axios request logging in debug mode
- implements `axios-debug` with some changes
- supports log output & format of homebridge
  • Loading branch information
johannrichard committed Nov 28, 2020
1 parent 6ee2c66 commit d4be51d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/lib/axiosDebugHelper.ts
@@ -0,0 +1,87 @@
import {
AxiosError,
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
} from 'axios';
import { Logger } from 'homebridge';
import { DingzLogger } from './dingzLogHelper';

/**
* Implements AxiosDebug for classes
*/
export class AxiosDebugHelper {
private readonly isAbsoluteURL = require('axios/lib/helpers/isAbsoluteURL');
private readonly buildURL = require('axios/lib/helpers/buildURL');
private readonly combineURLs = require('axios/lib/helpers/combineURLs');
private requestUrl = '';

/**
*
* @param instance AxiosInstance
* @param logger Logger
*/
constructor(
readonly instance: AxiosInstance,
readonly logger: Logger | DingzLogger,
) {
this.addLogger(instance);
}

private getURL(config: AxiosRequestConfig) {
let url = config.url;
if (config.baseURL && !this.isAbsoluteURL(url)) {
url = this.combineURLs(config.baseURL, url);
}
return this.buildURL(url, config.params, config.paramsSerializer);
}

public addLogger(
instance: AxiosInstance,
): AxiosRequestConfig | AxiosResponse | void {
instance.interceptors.request.use((config: AxiosRequestConfig) => {
this.request(config);
return config;
});
instance.interceptors.response.use(
(response: AxiosResponse) => {
this.response(response);
return response;
},
(error) => {
this.error(error);
throw error;
},
);
}

private request(config: AxiosRequestConfig) {
this.requestUrl = this.getURL(config);
Object.defineProperty(config, this.requestUrl, { value: this.requestUrl });
this.logger.debug(config.method?.toUpperCase() + ' ' + this.requestUrl);
}

private response(response: AxiosResponse) {
this.logger.debug(
response.status +
' ' +
response.statusText +
' (' +
response.config?.method?.toUpperCase() +
' ' +
this.requestUrl +
')',
);
}

private error(error: AxiosError) {
if (error.config) {
this.logger.error(
error.name + ': ' + error.message,
'(' + error.config?.method?.toUpperCase() + ' ' + this.requestUrl + ')',
);
} else {
this.logger.error(error.name + ': ' + error.message);
}
}
}
3 changes: 3 additions & 0 deletions src/lib/dingzDaBaseAccessory.ts
Expand Up @@ -9,6 +9,7 @@ import axiosRetry from 'axios-retry';

import { REQUEST_RETRIES, RETRY_TIMEOUT } from '../settings';
import { DeviceNotReachableError } from './errors';
import { AxiosDebugHelper } from './axiosDebugHelper';
export class DingzDaBaseAccessory {
protected static axiosRetryConfig = {
retries: REQUEST_RETRIES,
Expand All @@ -20,6 +21,7 @@ export class DingzDaBaseAccessory {

protected readonly log: DingzLogger;
protected readonly request: AxiosInstance;
protected readonly debugHelper: AxiosDebugHelper;

protected device: DeviceInfo;
protected baseUrl: string;
Expand Down Expand Up @@ -49,6 +51,7 @@ export class DingzDaBaseAccessory {
*/
axiosRetry(this.request, DingzDaBaseAccessory.axiosRetryConfig);
axiosRetry(axios, DingzDaBaseAccessory.axiosRetryConfig);
this.debugHelper = new AxiosDebugHelper(this.request, this.log);

// Register listener for updated device info (e.g. on restore with new IP)
this.platform.eb.on(
Expand Down

0 comments on commit d4be51d

Please sign in to comment.