Skip to content

Commit

Permalink
feat: add the ability to configure custom request polling timeout and…
Browse files Browse the repository at this point in the history
… interval values (#82)

* feat: add the ability to configure custom request polling timeout and interval values

* docs: fix typo
  • Loading branch information
samrum committed May 28, 2021
1 parent e1cd5bd commit 0d7286c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Use the Get Account Vehicles request to see which requests your vehicle supports

## Sample

Use a random version 4 uuid (there are online generators) as a deviceId.
Use a random version 4 uuid as a deviceId. Generator available [here](https://www.uuidgenerator.net/version4).

import OnStar from "onstarjs";

Expand All @@ -24,7 +24,11 @@ Use a random version 4 uuid (there are online generators) as a deviceId.
username: "foo@bar.com",
password: "p@ssw0rd",
onStarPin: "1234",

// Optional
checkRequestStatus: true, // When false, requests are complete when 'In Progress' (Much faster).
requestPollingIntervalSeconds: 6, // When checkRequestStatus is true, this is how often status check requests will be made
requestPollingTimeoutSeconds: 60, // When checkRequestStatus is true, this is when requests while polling are considered timed out
};

const onStar = OnStar.create(config);
Expand Down
26 changes: 19 additions & 7 deletions src/RequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ enum OnStarApiCommand {
class RequestService {
private config: OnStarConfig;
private authToken?: OAuthToken;
private checkRequestTimeout = 6000;
private checkRequestStatus: boolean;
private requestPollingTimeoutSeconds: number;
private requestPollingIntervalSeconds: number;
private tokenRefreshPromise?: Promise<OAuthToken>;
private tokenUpgradePromise?: Promise<void>;

Expand All @@ -56,6 +57,10 @@ class RequestService {
};

this.checkRequestStatus = this.config.checkRequestStatus ?? true;
this.requestPollingTimeoutSeconds =
config.requestPollingTimeoutSeconds ?? 60;
this.requestPollingIntervalSeconds =
config.requestPollingIntervalSeconds ?? 6;
}

setClient(client: HttpClient) {
Expand All @@ -70,8 +75,14 @@ class RequestService {
return this;
}

setCheckRequestTimeout(timeoutMs: number) {
this.checkRequestTimeout = timeoutMs;
setRequestPollingTimeoutSeconds(seconds: number) {
this.requestPollingTimeoutSeconds = seconds;

return this;
}

setRequestPollingIntervalSeconds(seconds: number) {
this.requestPollingIntervalSeconds = seconds;

return this;
}
Expand Down Expand Up @@ -354,9 +365,10 @@ class RequestService {

const requestTimestamp = new Date(requestTime).getTime();

const requestGiveup = this.checkRequestTimeout * 10;

if (Date.now() >= requestTimestamp + requestGiveup) {
if (
Date.now() >=
requestTimestamp + this.requestPollingTimeoutSeconds * 1000
) {
throw new RequestError("Command Timeout")
.setResponse(response)
.setRequest(request);
Expand Down Expand Up @@ -435,7 +447,7 @@ class RequestService {

private checkRequestPause() {
return new Promise((resolve) =>
setTimeout(resolve, this.checkRequestTimeout),
setTimeout(resolve, this.requestPollingIntervalSeconds * 1000),
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface OnStarConfig {
password: string;
onStarPin: string;
checkRequestStatus?: boolean;
requestPollingIntervalSeconds?: number;
requestPollingTimeoutSeconds?: number;
}

export interface OAuthToken {
Expand Down
18 changes: 18 additions & 0 deletions test/functional/OnStarJs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@ describe("OnStarJs", () => {
expect(result.status).toEqual("success");
expect(result.response?.data).toHaveProperty("commandResponse");
});

test.skip("Diagnostics Request Successful", async () => {
jest.setTimeout(60000);

onStar.setCheckRequestStatus(true);

const result = await onStar.diagnostics();

if (!result.response?.data || typeof result.response?.data === "string") {
throw new Error("Invalid response returned");
}

expect(result.status).toEqual("success");
expect(result.response?.data.commandResponse?.status).toEqual("success");
expect(result.response?.data.commandResponse?.body).toHaveProperty(
"diagnosticResponse",
);
});
});
8 changes: 4 additions & 4 deletions test/unit/RequestService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ describe("RequestService", () => {
testConfig,
instance(tokenHandler),
httpClient,
);

requestService.setAuthToken(authToken);
requestService.setCheckRequestTimeout(1);
)
.setAuthToken(authToken)
.setRequestPollingIntervalSeconds(0)
.setRequestPollingTimeoutSeconds(0);
});

test("start", async () => {
Expand Down

0 comments on commit 0d7286c

Please sign in to comment.