mirror of https://github.com/zulip/zulip.git
webhooks: Extract the '__' method of mapping to headers.
In the GitHub integration we established that for many integrations, we can directly map the fixture filename to the set of required headers and by following a simple naming convention we can greatly ease the logic involved in fixture_to_headers method required . So to prevent the need for duplicating the logic used by the GitHub integration, we created a method called `get_http_headers_from_filename` which will take the name of the HTTP header (key) and then return a corresponding method (in a decorator-like fashion) which could then be equated to fixture_to_headers in headers.py. The GitHub integration was modified to use this method and the docs were updated to suggest using this when possible.
This commit is contained in:
parent
271319fb13
commit
4691028097
|
@ -57,9 +57,11 @@ this, which is probably best explained by looking at the example for
|
|||
GitHub: `zerver/webhooks/github/headers.py`; basically, as part of
|
||||
writing your integration, you'll write a special function at that path
|
||||
that maps the filename of the fixture to the set of HTTP headers to
|
||||
use. Most such functions will use the same strategy as the GitHub
|
||||
use. Most such functions will use the same strategy as the GitHub
|
||||
integration: encoding the third party variable header data (usually
|
||||
just an event type) in the fixture filename.
|
||||
just an event type) in the fixture filename, in such a case, you
|
||||
won't need to explicitly write such a special function and can instead
|
||||
just use the same helper method that the GitHub integration uses.
|
||||
|
||||
## Step 1: Initialize your webhook python package
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ from urllib.parse import unquote
|
|||
|
||||
from django.http import HttpRequest
|
||||
from django.utils.translation import ugettext as _
|
||||
from typing import Optional, Dict, Union, Any
|
||||
from typing import Optional, Dict, Union, Any, Callable
|
||||
|
||||
from zerver.lib.actions import check_send_stream_message, \
|
||||
check_send_private_message, send_rate_limited_pm_notification_to_bot_owner
|
||||
|
@ -167,3 +167,17 @@ def get_fixture_http_headers(integration_name: str,
|
|||
)
|
||||
raise AttributeError(str(e) + msg)
|
||||
return fixture_to_headers(fixture_name)
|
||||
|
||||
|
||||
def get_http_headers_from_filename(http_header_key: str) -> Callable[[str], Dict[str, str]]:
|
||||
"""If an integration requires an event type kind of HTTP header which can
|
||||
be easily (statically) determined, then name the fixtures in the format
|
||||
of "header_value__other_details" or even "header_value" and the use this
|
||||
method in the headers.py file for the integration."""
|
||||
def fixture_to_headers(filename: str) -> Dict[str, str]:
|
||||
if '__' in filename:
|
||||
event_type = filename.split("__")[0]
|
||||
else:
|
||||
event_type = filename
|
||||
return {http_header_key: event_type}
|
||||
return fixture_to_headers
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
from typing import Dict
|
||||
from zerver.lib.webhooks.common import get_http_headers_from_filename
|
||||
|
||||
def fixture_to_headers(filename: str) -> Dict[str, str]:
|
||||
if '__' in filename:
|
||||
event_type = filename.split("__")[0]
|
||||
else:
|
||||
event_type = filename
|
||||
return {"HTTP_X_GITHUB_EVENT": event_type}
|
||||
key = "HTTP_X_GITHUB_EVENT"
|
||||
fixture_to_headers = get_http_headers_from_filename(key)
|
||||
|
|
Loading…
Reference in New Issue