Skip to content

Commit 733a669

Browse files
committed
refactor: update webhook interface
1 parent 5d62552 commit 733a669

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

raven/omni_channel_chat/api/webhooks.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from werkzeug.wrappers import Response
33

44
from raven.omni_channel_chat.doctype.omni_channel_chat_provider.omni_channel_chat_provider import (
5-
get_omni_channel_chat,
5+
get_omni_channel_chat_provider,
66
)
77

88

@@ -26,7 +26,10 @@ def extract_provider_slug() -> str:
2626

2727
@frappe.whitelist(allow_guest=True, methods=["POST", "GET"])
2828
def handle() -> Response:
29+
"""
30+
Endpoint:
31+
/api/method/raven.omni_channel_chat.api.webhooks.handle
32+
"""
2933
slug = extract_provider_slug()
30-
omni_channel_chat = get_omni_channel_chat(slug=slug)
31-
provider = omni_channel_chat.get_provider()
34+
provider = get_omni_channel_chat_provider(slug=slug)
3235
return provider.handle_frappe_api()

raven/omni_channel_chat/doctype/omni_channel_chat_provider/omni_channel_chat_provider.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@ def get_provider(self) -> Provider:
5353
frappe.throw(_("Provider not implemented."))
5454

5555

56-
def get_omni_channel_chat(slug: str) -> OmniChannelChatProvider:
57-
return frappe.get_doc("Omni Channel Chat Provider", slug)
56+
def get_omni_channel_chat_provider(slug: str) -> Provider:
57+
doc = frappe.get_doc("Omni Channel Chat Provider", slug)
58+
59+
if not doc:
60+
frappe.throw(_("Omni Channel Chat Provider not found."))
61+
62+
return doc.get_provider()

raven/omni_channel_chat/doctype/omni_channel_chat_provider/provider/base_provider.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ def push_message_to_raven(self, messages: list[dict]) -> None:
2323
for message in messages:
2424
handler.receive_from_provider(message)
2525

26-
def handle_webhook(self, body: bytes, headers: dict) -> None:
26+
def handle_webhook(self, body: bytes, headers: dict) -> Response:
2727
messages = self.extract_messages(body=body, headers=headers)
2828
self.push_message_to_raven(messages=messages)
29+
return Response("ok", status=200, content_type="text/plain")
2930

3031
@abstractmethod
3132
def handle_frappe_api(self) -> Response:
32-
"""Extract data from frappe request and pass to handle webhook."""
33+
"""Extract data from frappe request and pass to `handle_webhook`."""
3334

3435
@abstractmethod
3536
def get_user_info(self, user_id: str) -> dict:

0 commit comments

Comments
 (0)