Skip to content

Commit dfe3b6c

Browse files
authored
Merge pull request #11 from Zipstack/add-webhook-managment-fns
Added webhook management functions
2 parents 92a5b94 + 1aa16a9 commit dfe3b6c

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,48 @@ 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+
* @function
718+
* @name updateWebhookDetails
719+
* @description This function updates the details of a webhook.
720+
* @async
721+
* @param {string} webhookName - The name of the webhook.
722+
* @param {string} webhookUrl - The URL of the webhook.
723+
* @param {string} authToken - The authentication token for the webhook.
724+
* @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.
725+
* @throws {LLMWhispererClientException} Throws an LLMWhispererClientException if an error occurs during the operation.
726+
*
727+
*/
728+
async updateWebhookDetails(webhookName, webhookUrl, authToken) {
729+
const apiUrl = `${this.baseUrl}/whisper-manage-callback`;
730+
const data = {
731+
webhook_name: webhookName,
732+
url: webhookUrl,
733+
auth_token: authToken,
734+
};
735+
const myHeaders = { ...this.headers, "Content-Type": "application/json" };
736+
const options = {
737+
method: "put",
738+
url: apiUrl,
739+
headers: myHeaders,
740+
timeout: this.apiTimeout * 1000,
741+
data: data,
742+
};
743+
744+
const response = await axios(options);
745+
704746
if (response.status !== 200) {
705747
const message = response.data;
706748
message.statusCode = response.status;
@@ -748,6 +790,41 @@ class LLMWhispererClientV2 {
748790
}
749791
}
750792

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

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "llmwhisperer-client",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "LLMWhisper JS Client",
55
"main": "index.js",
66
"scripts": {
@@ -14,7 +14,6 @@
1414
"license": "MIT",
1515
"dependencies": {
1616
"axios": "~1.7.2",
17-
"llmwhisperer-client": "^2.0.1",
1817
"string-similarity": "^4.0.4",
1918
"winston": "~3.13.0"
2019
},

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)