Skip to content

Commit 95d214b

Browse files
committed
Added webhook management functions
1 parent 92a5b94 commit 95d214b

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,49 @@ class LLMWhispererClientV2 {
701701

702702
const response = await axios(options);
703703

704+
if (response.status !== 201) {
705+
const message = response.data;
706+
message.statusCode = response.status;
707+
throw new LLMWhispererClientException(message.message, response.status);
708+
} else {
709+
return {
710+
status_code: response.status,
711+
message: response.data,
712+
};
713+
}
714+
}
715+
716+
717+
/**
718+
* @function
719+
* @name updateWebhookDetails
720+
* @description This function updates the details of a webhook.
721+
* @async
722+
* @param {string} webhookName - The name of the webhook.
723+
* @param {string} webhookUrl - The URL of the webhook.
724+
* @param {string} authToken - The authentication token for the webhook.
725+
* @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.
726+
* @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation.
727+
*
728+
*/
729+
async updateWebhookDetails(webhookName, webhookUrl, authToken) {
730+
const apiUrl = `${this.baseUrl}/whisper-manage-callback`;
731+
const data = {
732+
webhook_name: webhookName,
733+
url: webhookUrl,
734+
auth_token: authToken,
735+
};
736+
const myHeaders = { ...this.headers, "Content-Type": "application/json" };
737+
const options = {
738+
method: "put",
739+
url: apiUrl,
740+
headers: myHeaders,
741+
timeout: this.apiTimeout * 1000,
742+
data: data,
743+
};
744+
745+
const response = await axios(options);
746+
704747
if (response.status !== 200) {
705748
const message = response.data;
706749
message.statusCode = response.status;
@@ -713,6 +756,7 @@ class LLMWhispererClientV2 {
713756
}
714757
}
715758

759+
716760
/**
717761
* @function
718762
* @name getWebhookDetails
@@ -748,6 +792,42 @@ class LLMWhispererClientV2 {
748792
}
749793
}
750794

795+
/**
796+
* @function
797+
* @name deleteWebhookDetails
798+
* @description This function deletes the details of a webhook.
799+
* @async
800+
* @param {string} webhookName - The name of the webhook.
801+
* @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.
802+
* @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation.
803+
*
804+
*/
805+
async deleteWebhookDetails(webhookName) {
806+
const apiUrl = `${this.baseUrl}/whisper-manage-callback`;
807+
const params = { webhook_name: webhookName };
808+
const options = {
809+
method: "delete",
810+
url: apiUrl,
811+
headers: this.headers,
812+
params: params,
813+
timeout: 200 * 1000,
814+
};
815+
816+
const response = await axios(options);
817+
818+
if (response.status !== 200) {
819+
const message = response.data;
820+
message.statusCode = response.status;
821+
throw new LLMWhispererClientException(message.message, response.status);
822+
} else {
823+
return {
824+
status_code: response.status,
825+
message: response.data,
826+
};
827+
}
828+
}
829+
830+
751831
/**
752832
* Retrieves the highlight information of the LLMWhisperer API.
753833
*

test/test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,46 @@ describe("LLMWhispererClientV2", () => {
106106
expect(line2.page).toBe(0);
107107
expect(line2.page_height).toBe(3168);
108108
}, 20000); // 20-second timeout
109+
110+
111+
test("webhook", async () => {
112+
const url = "https://webhook.site/b76ecc5f-8320-4410-b24f-66525d2c92cb";
113+
const token = "";
114+
const webhookName = "llmwhisperer-js-client-test";
115+
const response = await client.registerWebhook(url, token, webhookName);
116+
117+
expect(response).toEqual({ status_code: 201, message: { message: 'Webhook created successfully' } });
118+
119+
const getResponse = await client.getWebhookDetails(webhookName);
120+
121+
expect(getResponse).toEqual({
122+
status_code: 200, message: {
123+
auth_token: token, url: url, webhook_name: webhookName
124+
}
125+
});
126+
127+
const updateResponse = await client.updateWebhookDetails(webhookName, url, "new_token");
128+
expect(updateResponse).toEqual({ status_code: 200, message: { message: 'Webhook updated successfully' } });
129+
130+
const getUpdatedResponse = await client.getWebhookDetails(webhookName);
131+
expect(getUpdatedResponse).toEqual({
132+
status_code: 200, message: {
133+
auth_token: "new_token", url: url, webhook_name: webhookName
134+
}
135+
});
136+
137+
const deleteResponse = await client.deleteWebhookDetails(webhookName);
138+
expect(deleteResponse).toEqual({ status_code: 200, message: { message: 'Webhook deleted successfully' } });
139+
140+
try {
141+
await client.getWebhookDetails(webhookName);
142+
143+
} catch (e) {
144+
expect(e.response.status).toBe(404);
145+
expect(e.response.data.message).toBe('Webhook details not found');
146+
}
147+
148+
}, 15000); // 15-second timeout
149+
109150
});
110151

0 commit comments

Comments
 (0)