Add automatic client token renewal

This commit is contained in:
Christopher Cookman 2025-01-26 11:13:44 -07:00
parent 1ca300771f
commit b7559411c6

View file

@ -11,12 +11,31 @@ class FreepbxManager {
* @param {Object} config.dbPool - The connection pool for managing database connections. * @param {Object} config.dbPool - The connection pool for managing database connections.
*/ */
constructor(config) { constructor(config) {
this.client = new FreepbxGqlClient(config.url, { this.client = null;
client: { this.renewClient = async () => {
id: config.clientId, this.client = new FreepbxGqlClient(config.url, {
secret: config.clientSecret, client: {
}, id: config.clientId,
}); secret: config.clientSecret,
},
});
}
this.renewClient();
this.pbxCall = async (query, variables) => {
try {
return await this.client.request(query, variables);
} catch (err) {
if (err.response && err.response.error && err.response.error.message === "The resource owner or authorization server denied the request.") {
await this.renewClient();
console.log("Client renewed");
return await this.pbxCall(query, variables);
} else {
throw err;
}
}
}
this.pool = config.dbPool; this.pool = config.dbPool;
if (!this.pool) { if (!this.pool) {
@ -26,6 +45,8 @@ class FreepbxManager {
} }
async getExtension(ext) { async getExtension(ext) {
ext = String(ext); ext = String(ext);
@ -46,7 +67,7 @@ class FreepbxManager {
extensionId: ext.match(/\d+/)[0], extensionId: ext.match(/\d+/)[0],
}; };
return await this.client.request(query, variables); return await this.pbxCall(query, variables);
} }
async listExtensions() { async listExtensions() {
@ -63,7 +84,7 @@ class FreepbxManager {
} }
`; `;
return await this.client.request(query); return await this.pbxCall(query);
} }
async addExtension(ext, name) { async addExtension(ext, name) {
@ -92,7 +113,7 @@ class FreepbxManager {
vmPassword: ext, vmPassword: ext,
}; };
return await this.client.request(query, variables); return await this.pbxCall(query, variables);
} }
async deleteExtension(ext) { async deleteExtension(ext) {
@ -109,7 +130,7 @@ class FreepbxManager {
ext, ext,
}; };
const fpbxQuery = this.client.request(query, variables); const fpbxQuery = this.pbxCall(query, variables);
const dbQuery = this.pool.query('DELETE FROM paging_groups WHERE ext = ?', [ext]); const dbQuery = this.pool.query('DELETE FROM paging_groups WHERE ext = ?', [ext]);
return await Promise.all([fpbxQuery, dbQuery]); return await Promise.all([fpbxQuery, dbQuery]);
} }
@ -123,7 +144,7 @@ class FreepbxManager {
} }
`; `;
return await this.client.request(query); return await this.pbxCall(query);
} }
// async updateName(ext, name) { // async updateName(ext, name) {
@ -140,7 +161,7 @@ class FreepbxManager {
// name, // name,
// }; // };
// return await this.client.request(query, variables); // return await this.pbxCall(query, variables);
// } // }
// TODO: Implement updateName method, Current implementation resets extension for some reason // TODO: Implement updateName method, Current implementation resets extension for some reason