Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhooks: Strengthen types using WildValue in JSON Integration. #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions zerver/webhooks/json/view.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import json
from typing import Any, Dict

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.validator import (
WildValue,
check_anything,
to_wild_value,
)
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile

Expand All @@ -21,7 +25,7 @@
def api_json_webhook(
request: HttpRequest,
user_profile: UserProfile,
payload: Dict[str, Any] = REQ(argument_type="body"),
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
) -> HttpResponse:
body = get_body_for_http_request(payload)
subject = get_subject_for_http_request(payload)
Expand All @@ -30,10 +34,10 @@ def api_json_webhook(
return json_success(request)


def get_subject_for_http_request(payload: Dict[str, Any]) -> str:
def get_subject_for_http_request(payload: WildValue) -> str:
return "JSON"


def get_body_for_http_request(payload: Dict[str, Any]) -> str:
prettypayload = json.dumps(payload, indent=2)
def get_body_for_http_request(payload: WildValue) -> str:
prettypayload = json.dumps(payload.tame(check_anything), indent=2)
return JSON_MESSAGE_TEMPLATE.format(webhook_payload=prettypayload, sort_keys=True)