Files
zulip/zerver/webhooks/json/view.py
Zixuan James Li 318a9316a7 webhooks: Migrate webhooks with special payload types to use @typed_endpoint.
Instead of a WildValue, the JSON/Sentry webhook expect the request body to be a
dict.

For the JSON webhook, json.dumps accepts other types of input as well and the
constraint is not necessary, but this serve as a good example of an alternative
use of WebhookPayload to describe a payload that is intended to be parsed from
the entire request body from JSON, into a type other than WildValue.
2023-09-08 08:20:17 -07:00

41 lines
1.1 KiB
Python

import json
from typing import Any, Dict
from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import WebhookPayload, typed_endpoint
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")
@typed_endpoint
def api_json_webhook(
request: HttpRequest,
user_profile: UserProfile,
*,
payload: WebhookPayload[Dict[str, Any]],
) -> HttpResponse:
body = get_body_for_http_request(payload)
topic = get_topic_for_http_request(payload)
check_send_webhook_message(request, user_profile, topic, body)
return json_success(request)
def get_topic_for_http_request(payload: Dict[str, Any]) -> str:
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)