Skip to content

Commit 29a3bd8

Browse files
committed
Added webhook management functions
1 parent c7237ce commit 29a3bd8

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

src/unstract/llmwhisperer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "2.2.1"
1+
__version__ = "2.3.0"
22

33
from .client import LLMWhispererClient # noqa: F401
44
from .client_v2 import LLMWhispererClientV2 # noqa: F401

src/unstract/llmwhisperer/client_v2.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,45 @@ def register_webhook(self, url: str, auth_token: str, webhook_name: str) -> dict
510510
prepared = req.prepare()
511511
s = requests.Session()
512512
response = s.send(prepared, timeout=self.api_timeout)
513+
if response.status_code != 201:
514+
err = json.loads(response.text)
515+
err["status_code"] = response.status_code
516+
raise LLMWhispererClientException(err)
517+
return json.loads(response.text)
518+
519+
def update_webhook_details(self, webhook_name: str, url: str, auth_token: str) -> dict:
520+
"""Updates the details of a webhook from the LLMWhisperer API.
521+
522+
This method sends a PUT request to the '/whisper-manage-callback' endpoint of the LLMWhisperer API.
523+
The response is a JSON object containing the status of the webhook update.
524+
525+
Refer to https://docs.unstract.com/llm_whisperer/apis/
526+
527+
Args:
528+
webhook_name (str): The name of the webhook.
529+
url (str): The URL of the webhook.
530+
auth_token (str): The authentication token for the webhook.
531+
532+
Returns:
533+
dict: A dictionary containing the status code and the response from the API.
534+
535+
Raises:
536+
LLMWhispererClientException: If the API request fails, it raises an exception with
537+
the error message and status code returned by the API.
538+
"""
539+
540+
data = {
541+
"url": url,
542+
"auth_token": auth_token,
543+
"webhook_name": webhook_name,
544+
}
545+
url = f"{self.base_url}/whisper-manage-callback"
546+
headersx = copy.deepcopy(self.headers)
547+
headersx["Content-Type"] = "application/json"
548+
req = requests.Request("PUT", url, headers=headersx, json=data)
549+
prepared = req.prepare()
550+
s = requests.Session()
551+
response = s.send(prepared, timeout=self.api_timeout)
513552
if response.status_code != 200:
514553
err = json.loads(response.text)
515554
err["status_code"] = response.status_code
@@ -547,6 +586,37 @@ def get_webhook_details(self, webhook_name: str) -> dict:
547586
raise LLMWhispererClientException(err)
548587
return json.loads(response.text)
549588

589+
def delete_webhook(self, webhook_name: str) -> dict:
590+
"""Deletes a webhook from the LLMWhisperer API.
591+
592+
This method sends a DELETE request to the '/whisper-manage-callback' endpoint of the LLMWhisperer API.
593+
The response is a JSON object containing the status of the webhook deletion.
594+
595+
Refer to https://docs.unstract.com/llm_whisperer/apis/
596+
597+
Args:
598+
webhook_name (str): The name of the webhook.
599+
600+
Returns:
601+
dict: A dictionary containing the status code and the response from the API.
602+
603+
Raises:
604+
LLMWhispererClientException: If the API request fails, it raises an exception with
605+
the error message and status code returned by the API.
606+
"""
607+
608+
url = f"{self.base_url}/whisper-manage-callback"
609+
params = {"webhook_name": webhook_name}
610+
req = requests.Request("DELETE", url, headers=self.headers, params=params)
611+
prepared = req.prepare()
612+
s = requests.Session()
613+
response = s.send(prepared, timeout=self.api_timeout)
614+
if response.status_code != 200:
615+
err = json.loads(response.text)
616+
err["status_code"] = response.status_code
617+
raise LLMWhispererClientException(err)
618+
return json.loads(response.text)
619+
550620
def get_highlight_rect(
551621
self,
552622
line_metadata: list[int],

tests/integration/client_v2_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import pytest
77

8+
from unstract.llmwhisperer.client_v2 import LLMWhispererClientException
9+
810
logger = logging.getLogger(__name__)
911

1012

@@ -168,6 +170,63 @@ def test_whisper_v2_url_in_post(client_v2, data_dir, output_mode, mode, url, inp
168170
verify_usage(usage_before, usage_after, page_count, mode)
169171

170172

173+
@pytest.mark.parametrize(
174+
"url,token,webhook_name",
175+
[
176+
(
177+
"https://webhook.site/b76ecc5f-8320-4410-b24f-66525d2c92cb",
178+
"",
179+
"client_v2_test",
180+
),
181+
],
182+
)
183+
def test_webhook(client_v2, url, token, webhook_name):
184+
"""Tests the registration, retrieval, update, and deletion of a webhook.
185+
186+
This test method performs the following steps:
187+
1. Registers a new webhook with the provided URL, token, and webhook name.
188+
2. Retrieves the details of the registered webhook and verifies the URL, token, and webhook name.
189+
3. Updates the webhook details with a new token.
190+
4. Deletes the webhook and verifies the deletion.
191+
192+
Args:
193+
client_v2 (LLMWhispererClientV2): The client instance for making API requests.
194+
url (str): The URL of the webhook.
195+
token (str): The authentication token for the webhook.
196+
webhook_name (str): The name of the webhook.
197+
198+
Returns:
199+
None
200+
"""
201+
result = client_v2.register_webhook(url, token, webhook_name)
202+
assert isinstance(result, dict)
203+
assert result["message"] == "Webhook created successfully"
204+
205+
result = client_v2.get_webhook_details(webhook_name)
206+
assert isinstance(result, dict)
207+
assert result["url"] == url
208+
assert result["auth_token"] == token
209+
assert result["webhook_name"] == webhook_name
210+
211+
result = client_v2.update_webhook_details(webhook_name, url, "new_token")
212+
assert isinstance(result, dict)
213+
assert result["message"] == "Webhook updated successfully"
214+
215+
result = client_v2.get_webhook_details(webhook_name)
216+
assert isinstance(result, dict)
217+
assert result["auth_token"] == "new_token"
218+
219+
result = client_v2.delete_webhook(webhook_name)
220+
assert isinstance(result, dict)
221+
assert result["message"] == "Webhook deleted successfully"
222+
223+
try:
224+
client_v2.get_webhook_details(webhook_name)
225+
except LLMWhispererClientException as e:
226+
assert e.error_message()["message"] == "Webhook details not found"
227+
assert e.error_message()["status_code"] == 404
228+
229+
171230
def assert_error_message(whisper_result):
172231
assert isinstance(whisper_result, dict)
173232
assert whisper_result["status"] == "error"

0 commit comments

Comments
 (0)