From ece6b98699854dcfede8d5d968490351c8019ea1 Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Mon, 14 Aug 2023 17:37:15 -0400 Subject: [PATCH] webhooks: Migrate helloworld to use WildValue to use @typed_endpoint. We owe more documentation on the use of WildValue. A follow-up on updating it with examples of WildValue and endpoint will be desirable. --- zerver/webhooks/helloworld/view.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/zerver/webhooks/helloworld/view.py b/zerver/webhooks/helloworld/view.py index 0bda45ce0e..c862cb1b26 100644 --- a/zerver/webhooks/helloworld/view.py +++ b/zerver/webhooks/helloworld/view.py @@ -1,21 +1,22 @@ # Webhooks for external integrations. -from typing import Any, Dict, Sequence from django.http import HttpRequest, HttpResponse from zerver.decorator import webhook_view -from zerver.lib.request import REQ, has_request_variables from zerver.lib.response import json_success +from zerver.lib.typed_endpoint import WebhookPayload, typed_endpoint +from zerver.lib.validator import WildValue, check_string from zerver.lib.webhooks.common import check_send_webhook_message from zerver.models import UserProfile @webhook_view("HelloWorld") -@has_request_variables +@typed_endpoint def api_helloworld_webhook( request: HttpRequest, user_profile: UserProfile, - payload: Dict[str, Sequence[Dict[str, Any]]] = REQ(argument_type="body"), + *, + payload: WebhookPayload[WildValue], ) -> HttpResponse: # construct the body of the message body = "Hello! I am happy to be here! :smile:" @@ -24,7 +25,10 @@ def api_helloworld_webhook( body_template = ( "\nThe Wikipedia featured article for today is **[{featured_title}]({featured_url})**" ) - body += body_template.format(**payload) + body += body_template.format( + featured_title=payload["featured_title"].tame(check_string), + featured_url=payload["featured_url"].tame(check_string), + ) topic = "Hello World"