make it possible to change notification data field key name

This commit is contained in:
arabcoders 2025-04-09 17:35:11 +03:00
parent 817e7d2e37
commit 88163cad42
3 changed files with 44 additions and 7 deletions

View file

@ -46,12 +46,14 @@ class TargetRequest:
method: str
url: str
headers: list[TargetRequestHeader] = field(default_factory=list)
data_key: str = "data"
def serialize(self) -> dict:
return {
"type": self.type,
"method": self.method,
"url": self.url,
"data_key": self.data_key,
"headers": [h.serialize() for h in self.headers],
}
@ -247,6 +249,7 @@ class Notification(metaclass=Singleton):
type=target.get("request", {}).get("type", "json"),
method=target.get("request", {}).get("method", "POST"),
url=target.get("request", {}).get("url"),
data_key=target.get("request", {}).get("data_key", "data"),
headers=[
TargetRequestHeader(
key=str(h.get("key", "")).strip(),
@ -288,6 +291,10 @@ class Notification(metaclass=Singleton):
msg = "Invalid notification target. No URL found."
raise ValueError(msg)
if "data_key" not in target["request"]:
msg = "Invalid notification target. No data_key found."
raise ValueError(msg)
if "method" in target["request"] and target["request"]["method"].upper() not in ["POST", "PUT"]:
msg = "Invalid notification target. Invalid method found."
raise ValueError(msg)
@ -363,10 +370,17 @@ class Notification(metaclass=Singleton):
for h in target.request.headers:
reqBody["headers"][h.key] = h.value
reqBody["json" if "json" == target.request.type.lower() else "data"] = self._deep_unpack(ev.serialize())
body_key = "json" if "json" == target.request.type.lower() else "data"
reqBody[body_key] = self._deep_unpack(ev.serialize())
if "data" != target.request.data_key:
reqBody[body_key][target.request.data_key] = reqBody[body_key]["data"]
reqBody[body_key].pop("data", None)
if "form" == target.request.type.lower():
reqBody["data"]["data"] = self._encoder.encode(reqBody["data"]["data"])
reqBody[body_key][target.request.data_key] = self._encoder.encode(
reqBody[body_key][target.request.data_key]
)
response = await self._client.request(**reqBody)

View file

@ -138,7 +138,7 @@
</div>
</div>
<div class="column is-12 is-clearfix">
<div class="column is-6-tablet is-12-mobile">
<div class="field">
<label class="label is-inline" for="on">
Select Events
@ -167,6 +167,26 @@
</div>
</div>
<div class="column is-6-tablet is-12-mobile">
<div class="field">
<label class="label is-inline" for="data_key">
Data field
</label>
<div class="control has-icons-left">
<input type="text" class="input" id="data_key" v-model="form.request.data_key"
:disabled="addInProgress" required>
<span class="icon is-small is-left"><i class="fa-solid fa-key" /></span>
</div>
<span class="help">
<span class="icon"><i class="fa-solid fa-info" /></span>
<span>
The field name to use when sending the notification. This is used to identify the data in the
request. The default is <code>data</code>.
</span>
</span>
</div>
</div>
<div class="column is-12">
<div class="field">
<label class="label is-inline is-unselectable">
@ -277,7 +297,7 @@ const showImport = useStorage('showImport', false);
const import_string = ref('');
const checkInfo = async () => {
const required = ['name', 'request.url', 'request.method', 'request.type'];
const required = ['name', 'request.url', 'request.method', 'request.type', 'request.data_key'];
for (const key of required) {
if (key.includes('.')) {
const [parent, child] = key.split('.');
@ -356,6 +376,10 @@ const importItem = async () => {
form.request = item.request
}
if (item.data_key) {
form.data_key = item.data_key
}
if (item.on) {
form.on = item.on
}
@ -366,5 +390,4 @@ const importItem = async () => {
toast.error(`Failed to import task. ${e.message}`)
}
}
</script>

View file

@ -126,8 +126,8 @@ div.is-centered {
</li>
<li>
When you set the request type as <code>Form</code>, the event data will be JSON encoded and sent as the
and sent as <code>...&data=json_string</code>, only the <code>data</code> field will be JSON encoded. The
other keys <code>id</code>, <code>event</code> and <code>created_at</code> will be sent as they are.
and sent as <code>...&data_key=json_string</code>, only the <code>data</code> field will be JSON encoded.
The other keys <code>id</code>, <code>event</code> and <code>created_at</code> will be sent as they are.
</li>
<li>We also send two special headers <code>X-Event-ID</code> and <code>X-Event</code> with the request.</li>
</ul>