Skip to content

Commit

Permalink
Update for renew token.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanganderson committed Aug 20, 2024
1 parent a5be54b commit 53ba1be
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 74 deletions.
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.2",
"version": "3.0.3",
"license": "Apache-2.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"ajv": "^6.12.6",
"axios": "^1.3.6",
"moment": "^2.30.1",
"uuid": "^9.0.0"
}
}
14 changes: 2 additions & 12 deletions src/messaging/classes/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { URLSearchParams } from 'url';
import { RequestError, AuthError } from './Errors';
import {
getAuthToken,
setAuthToken,
getAuthTokenRetryCount,
setAuthTokenRetryCount,
setAuthToken
} from '../utils';

declare module 'axios' {
Expand All @@ -23,7 +21,6 @@ declare module 'axios' {
export abstract class HttpClient {
protected readonly instance: AxiosInstance;
private auth: Auth;
private authRetryCount: number = 0;

public constructor(public authConfig?: AuthConfigProps) {
this.instance = axios.create({
Expand Down Expand Up @@ -123,18 +120,11 @@ export abstract class HttpClient {
);
}

this.authRetryCount = await getAuthTokenRetryCount();

// attempt to refresh an auth token
if (
error.response?.status === 401 &&
error.response?.config.url !== '/v2/oauth/token' &&
this.authRetryCount < 3
error.response?.config.url !== '/v2/oauth/token'
) {
// increment auth token retry count
this.authRetryCount++;
await setAuthTokenRetryCount(this.authRetryCount);

// retrieve auth credentials
const authCredentials = await this.auth.getCredentials();

Expand Down
1 change: 0 additions & 1 deletion src/messaging/constants/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export abstract class Constants {
static readonly SHARED_CREDENTIALS: string = `${os.homedir()}/.telstra/credentials`;
static readonly API_URL: string = 'https://products.api.telstra.com/';
static readonly BUCKET_AUTH_STORE: string = 'authStore';
static readonly BUCKET_KEY_AUTH_RETRY_COUNT: string = 'tokenRetryAttempt';
static readonly BUCKET_KEY_ACCESS_TOKEN: string = 'accessToken';
static readonly BUCKET_KEY_CLIENT_CREDENTIALS: string = 'clientCredentials';
static readonly USER_AGENT: string = 'Telstra Messaging SDK/0.3.18';
Expand Down
28 changes: 0 additions & 28 deletions src/messaging/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,3 @@ export const getAuthToken = async (): Promise<string | boolean> => {
}
};

export const setAuthTokenRetryCount = async (
retryCount: number
): Promise<boolean> => {
try {
if (!retryCount)
throw new StorageError(Constants.ERRORS.STORAGE_ERROR_SET);
await storage().set({
bucket: Constants.BUCKET_AUTH_STORE,
key: Constants.BUCKET_KEY_AUTH_RETRY_COUNT,
data: JSON.stringify(retryCount),
});
return true;
} catch (error) {
return false;
}
};

export const getAuthTokenRetryCount = async (): Promise<number> => {
try {
const data = await storage().get({
bucket: Constants.BUCKET_AUTH_STORE,
key: Constants.BUCKET_KEY_AUTH_RETRY_COUNT,
});
return parseInt(JSON.parse(data));
} catch (error) {
return 0;
}
};
5 changes: 4 additions & 1 deletion test/src/messaging/classes/Reports.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { server, rest } = require('../testServer');
const { Reports, AssertionError } = require('../../../../src/messaging/classes');
const AUTH_CONFIG = require('../credentials.json');
const { Constants } = require('../Constants');
const moment = require('moment');

const reports = new Reports(AUTH_CONFIG);

Expand All @@ -11,7 +12,9 @@ describe("Reports", () => {
describe("request a messages report", () => {
describe('when the client sends a valid request', () => {
it("should pass", async () => {
const data = { startDate: "2024-05-01", endDate: "2024-05-10", reportCallbackUrl: "https://www.example.com", filter:"test"}
const startDate = moment().subtract(3, 'days').format('YYYY-MM-DD');
const endDate = moment().format('YYYY-MM-DD');
const data = { startDate, endDate, reportCallbackUrl: "https://www.example.com", filter: "test" };
await expect(reports.create(data)).resolves.toEqual(Constants.CREATE_MESSAGES_REPORT_RESPONSE);
});
});
Expand Down
30 changes: 1 addition & 29 deletions test/src/messaging/utils/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ const {
setAuthConfig,
getAuthConfig,
setAuthToken,
getAuthToken,
setAuthTokenRetryCount,
getAuthTokenRetryCount,
getAuthToken
} = require('../../../../src/messaging/utils');

const AUTH_CONFIG = require('../credentials.json');
Expand Down Expand Up @@ -46,31 +44,5 @@ describe('config', () => {
});
});

describe('when getAuthTokenRetryCount is called', () => {
it('should return 0 as a number', async () => {
expect(await getAuthTokenRetryCount()).toEqual(0);
});
});

describe('when setAuthTokenRetryCount is called', () => {
it('should get auth token retry count of 0 when storage not yet initialised', async () => {
expect(await getAuthTokenRetryCount()).toEqual(0);
});
});

describe('when setAuthTokenRetryCount is called', () => {
it('should return true given a number', async () => {
expect(await setAuthTokenRetryCount(1)).toBeTruthy();
});
it('should return false given no payload', async () => {
expect(await setAuthTokenRetryCount()).toBeFalsy();
});
});

describe('when getAuthTokenRetryCount is called', () => {
it('should return 1 as a number', async () => {
expect(await getAuthTokenRetryCount()).toEqual(1);
});
});

});

0 comments on commit 53ba1be

Please sign in to comment.