Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): fix issue with status from api not updating on its own #405

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/infinityApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ export class InfinityEvolutionApiConnection {
});
}

// Api seems to expect this every min or so. more frequent doesn't seem to
// make any difference.
@MemoizeExpiring(1 * 60 * 1000)
async activate(): Promise<void> {
try {
await this.forceRefreshToken();
} catch (error) {
this.log.error(
'[API] Failure sending activation signal: ',
Axios.isAxiosError(error) ? error.message : error,
);
}
}

async forceActivate(): Promise<void> {
await this.axios.post(
`/users/${this.username}/activateSystems`,
{
headers: {
Accept: 'application/json',
},
},
);
}

@MemoizeExpiring(24 * 60 * 60 * 1000) // every 24 hrs
async refreshToken(): Promise<void> {
try {
Expand Down Expand Up @@ -257,6 +282,7 @@ abstract class BaseInfinityEvolutionApiModel {

protected async forceFetch(): Promise<void> {
await this.api_connection.refreshToken();
await this.api_connection.activate();
const response = await this.api_connection.axios.get(this.getPath());
if (response.data) {
this.data_object = await xml2js.parseStringPromise(response.data);
Expand Down Expand Up @@ -301,7 +327,9 @@ abstract class BaseInfinityEvolutionSystemApiModel extends BaseInfinityEvolution
protected async forceFetch(): Promise<void> {
await super.forceFetch();
const top_level_key = Object.keys(this.data_object)[0];
this.last_updated = Date.parse(this.data_object[top_level_key].timestamp[0]);
const ts = this.data_object[top_level_key].timestamp[0];
this.last_updated = Date.parse(ts);
this.log.debug(`TIMESTAMP ${this.getPath()} reports ${ts} (${this.last_updated})`);
}
}

Expand Down