Skip to content

Commit

Permalink
SHOTS-4317: handle auth token expiration better (#20)
Browse files Browse the repository at this point in the history
* SHOTS-4317: handle auth token expiration better
  • Loading branch information
kkanwar authored Dec 1, 2023
1 parent b443249 commit b2c42c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spresso-sdk/price_optimization",
"version": "2.2.0",
"version": "2.2.1",
"description": "Spresso Price Optimization SDK for Node",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
67 changes: 39 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const DEFAULT_CONNECTION_TIMEOUT_MS = 1000;
const DEFAULT_KEEPALIVE_TIMEOUT_MS = 30000;
const DEFAULT_SOCKET_COUNT = 128;

// 30 minute expiration padding for auth tokens
const AUTH_EXPIRATION_PAD_MS = 30 * 60 * 1000;

export interface ILogger {
error(message?: any, ...optionalParams: any[]): void;
info?(message?: any, ...optionalParams: any[]): void;
Expand All @@ -15,15 +18,15 @@ export type PricingRequest = {
defaultPrice?: number;
deviceId: string;
overrideToDefaultPrice?: boolean;
sku: string;
itemId: string;
userId?: string;
};

export type PricingResponse = {
deviceId: string;
isPriceOptimized: boolean;
price: number | null;
sku: string;
itemId: string;
userId: string | null;
};

Expand Down Expand Up @@ -90,44 +93,52 @@ class SpressoSDK {
}

async getPrice(request: PricingRequest, userAgent: string | undefined): Promise<PricingResponse> {
const response = await this.makeRequest(
'get',
request,
undefined,
userAgent
).catch((err) => {
this.handleAxiosError(err);
return this.emptyResponse(request);
});
try {
const response = await this.makeRequest(
'get',
request,
undefined,
userAgent
).catch((err) => {
this.handleAxiosError(err);
return this.emptyResponse(request);
});

if (response == null) {
if (response == null) {
return this.emptyResponse(request);
}

return response as PricingResponse;
} catch (err) {
return this.emptyResponse(request);
}

return response as PricingResponse;
}

async getPrices(requests: PricingRequest[], userAgent: string | undefined): Promise<PricingResponse[]> {
const response = await this.makeRequest(
'post',
undefined,
{ requests },
userAgent
).catch((err) => {
this.handleAxiosError(err);
return this.emptyResponses(requests);
});
try {
const response = await this.makeRequest(
'post',
undefined,
{ requests },
userAgent
).catch((err) => {
this.handleAxiosError(err);
return this.emptyResponses(requests);
});

if (response == null) {
if (response == null) {
return this.emptyResponses(requests);
}

return response as PricingResponse[];
} catch (err) {
return this.emptyResponses(requests);
}

return response as PricingResponse[];
}

private async authenticate(): Promise<void> {
const now = new Date().getTime();
if (this.authToken != null && this.tokenExpiration != null && now < this.tokenExpiration) {
if (this.authToken != null && this.tokenExpiration != null && now < (this.tokenExpiration - AUTH_EXPIRATION_PAD_MS)) {
return Promise.resolve(); // Current token is valid and non-expired, no need to refetch
}

Expand Down Expand Up @@ -216,7 +227,7 @@ class SpressoSDK {
deviceId: request.deviceId,
isPriceOptimized: false,
price: request.defaultPrice ?? null,
sku: request.sku,
itemId: request.itemId,
userId: request.userId ?? null,
};
}
Expand Down

0 comments on commit b2c42c6

Please sign in to comment.