2021-04-09 03:50:04 +02:00
|
|
|
import json
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2021-04-09 03:50:04 +02:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2021-04-09 03:50:04 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
JSON_MESSAGE_TEMPLATE = """
|
|
|
|
```json
|
|
|
|
{webhook_payload}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("JSON")
|
2023-08-14 23:40:43 +02:00
|
|
|
@typed_endpoint
|
2021-04-09 03:50:04 +02:00
|
|
|
def api_json_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-14 23:40:43 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[Dict[str, Any]],
|
2021-04-09 03:50:04 +02:00
|
|
|
) -> HttpResponse:
|
|
|
|
body = get_body_for_http_request(payload)
|
2023-07-12 13:37:08 +02:00
|
|
|
topic = get_topic_for_http_request(payload)
|
2021-04-09 03:50:04 +02:00
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2021-04-09 03:50:04 +02:00
|
|
|
|
|
|
|
|
2023-07-12 13:37:08 +02:00
|
|
|
def get_topic_for_http_request(payload: Dict[str, Any]) -> str:
|
2021-04-09 03:50:04 +02:00
|
|
|
return "JSON"
|
|
|
|
|
|
|
|
|
|
|
|
def get_body_for_http_request(payload: Dict[str, Any]) -> str:
|
|
|
|
prettypayload = json.dumps(payload, indent=2)
|
|
|
|
return JSON_MESSAGE_TEMPLATE.format(webhook_payload=prettypayload, sort_keys=True)
|