Skip to content

Added webhook management functions #11

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

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
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
77 changes: 77 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,48 @@ class LLMWhispererClientV2 {

const response = await axios(options);

if (response.status !== 201) {
const message = response.data;
message.statusCode = response.status;
throw new LLMWhispererClientException(message.message, response.status);
} else {
return {
status_code: response.status,
message: response.data,
};
}
}

/**
* @function
* @name updateWebhookDetails
* @description This function updates the details of a webhook.
* @async
* @param {string} webhookName - The name of the webhook.
* @param {string} webhookUrl - The URL of the webhook.
* @param {string} authToken - The authentication token for the webhook.
* @returns {Promise<Object>} Returns a promise that resolves with an object containing the response from the webhook details update. The object includes the status code and the response data.
* @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation.
*
*/
async updateWebhookDetails(webhookName, webhookUrl, authToken) {
const apiUrl = `${this.baseUrl}/whisper-manage-callback`;
const data = {
webhook_name: webhookName,
url: webhookUrl,
auth_token: authToken,
};
const myHeaders = { ...this.headers, "Content-Type": "application/json" };
const options = {
method: "put",
url: apiUrl,
headers: myHeaders,
timeout: this.apiTimeout * 1000,
data: data,
};

const response = await axios(options);

if (response.status !== 200) {
const message = response.data;
message.statusCode = response.status;
Expand Down Expand Up @@ -748,6 +790,41 @@ class LLMWhispererClientV2 {
}
}

/**
* @function
* @name deleteWebhookDetails
* @description This function deletes the details of a webhook.
* @async
* @param {string} webhookName - The name of the webhook.
* @returns {Promise<Object>} Returns a promise that resolves with an object containing the response from the delete operation. The object includes the status code and the response data.
* @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation.
*
*/
async deleteWebhookDetails(webhookName) {
const apiUrl = `${this.baseUrl}/whisper-manage-callback`;
const params = { webhook_name: webhookName };
const options = {
method: "delete",
url: apiUrl,
headers: this.headers,
params: params,
timeout: 200 * 1000,
};

const response = await axios(options);

if (response.status !== 200) {
const message = response.data;
message.statusCode = response.status;
throw new LLMWhispererClientException(message.message, response.status);
} else {
return {
status_code: response.status,
message: response.data,
};
}
}

/**
* Retrieves the highlight information of the LLMWhisperer API.
*
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "llmwhisperer-client",
"version": "2.2.0",
"version": "2.3.0",
"description": "LLMWhisper JS Client",
"main": "index.js",
"scripts": {
Expand All @@ -14,7 +14,6 @@
"license": "MIT",
"dependencies": {
"axios": "~1.7.2",
"llmwhisperer-client": "^2.0.1",
"string-similarity": "^4.0.4",
"winston": "~3.13.0"
},
Expand Down
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,46 @@ describe("LLMWhispererClientV2", () => {
expect(line2.page).toBe(0);
expect(line2.page_height).toBe(3168);
}, 20000); // 20-second timeout


test("webhook", async () => {
const url = "https://webhook.site/b76ecc5f-8320-4410-b24f-66525d2c92cb";
const token = "";
const webhookName = "llmwhisperer-js-client-test";
const response = await client.registerWebhook(url, token, webhookName);

expect(response).toEqual({ status_code: 201, message: { message: 'Webhook created successfully' } });

const getResponse = await client.getWebhookDetails(webhookName);

expect(getResponse).toEqual({
status_code: 200, message: {
auth_token: token, url: url, webhook_name: webhookName
}
});

const updateResponse = await client.updateWebhookDetails(webhookName, url, "new_token");
expect(updateResponse).toEqual({ status_code: 200, message: { message: 'Webhook updated successfully' } });

const getUpdatedResponse = await client.getWebhookDetails(webhookName);
expect(getUpdatedResponse).toEqual({
status_code: 200, message: {
auth_token: "new_token", url: url, webhook_name: webhookName
}
});

const deleteResponse = await client.deleteWebhookDetails(webhookName);
expect(deleteResponse).toEqual({ status_code: 200, message: { message: 'Webhook deleted successfully' } });

try {
await client.getWebhookDetails(webhookName);

} catch (e) {
expect(e.response.status).toBe(404);
expect(e.response.data.message).toBe('Webhook details not found');
}

}, 15000); // 15-second timeout

});