Skip to content

Commit

Permalink
fix(api): fix issue with status from api not updating on its own
Browse files Browse the repository at this point in the history
This adds the new 'activation' api call to tell the api to fetch new data.

fix #397
  • Loading branch information
grivkees committed Jan 14, 2023
1 parent 229871c commit 2df066d
Showing 1 changed file with 29 additions and 1 deletion.
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

0 comments on commit 2df066d

Please sign in to comment.