From ed69a4d82173ec52ee0e386d3b0f3826b4a5d74e Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 27 Nov 2024 15:35:36 +0100 Subject: [PATCH 1/2] rust: Add MessageIn::{new_raw_payload, with_content_type} --- rust/src/lib.rs | 1 + rust/src/model_ext.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 rust/src/model_ext.rs diff --git a/rust/src/lib.rs b/rust/src/lib.rs index a094e3fb1..1e3ed1dc0 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -19,6 +19,7 @@ extern crate serde_derive; pub mod api; pub mod error; +mod model_ext; mod request; pub mod webhooks; diff --git a/rust/src/model_ext.rs b/rust/src/model_ext.rs new file mode 100644 index 000000000..4e6bf0dc9 --- /dev/null +++ b/rust/src/model_ext.rs @@ -0,0 +1,36 @@ +//! Extensions of the auto-generated "models" (schema structs). + +use serde_json::json; + +use crate::models::MessageIn; + +impl MessageIn { + /// Create a new message with a pre-serialized payload. + /// + /// The payload is not normalized on the server (usually whitespace outside + /// of string literals, unnecessarily escaped characters in string and such + /// are fixed up by the server), and is not even required to be JSON. + /// + /// The default `content-type` of `application/json` will still be used, + /// unless overwritten with [`with_content_type`][Self::with_content_type]. + pub fn new_raw_payload(event_type: String, payload: String) -> Self { + Self { + transformations_params: Some(json!({ "rawPayload": payload })), + ..Self::new(event_type, json!({})) + } + } + + /// Set the `content-type` header to use for the message. + pub fn with_content_type(mut self, content_type: String) -> Self { + let transformations_params = self.transformations_params.get_or_insert_with(|| json!({})); + + // This will panic if transformations_params, its headers field, or the + // headers' content-type field already exists as an array, bool, number + // or string. + // That would make the whole parameter struct invalid anyways though, + // and can hardly happen accidentally. + transformations_params["headers"]["content-type"] = content_type.into(); + + self + } +} From 92d1dcc4a4214e2ab9351b913e567c5f34d34169 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 27 Nov 2024 15:39:17 +0100 Subject: [PATCH 2/2] kotlin: Add more info to messageInRaw docs --- kotlin/lib/src/main/kotlin/Message.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kotlin/lib/src/main/kotlin/Message.kt b/kotlin/lib/src/main/kotlin/Message.kt index a3d3b6e39..f343b6a21 100644 --- a/kotlin/lib/src/main/kotlin/Message.kt +++ b/kotlin/lib/src/main/kotlin/Message.kt @@ -76,10 +76,15 @@ class Message internal constructor(token: String, options: SvixOptions) { /** * Creates a [MessageIn] with the payload already being serialized. * + * The payload is not normalized on the server (usually whitespace outside + * of string literals, unnecessarily escaped characters in string and such + * are fixed up by the server), and is not even required to be JSON. + * * @param payload Serialized message payload * @param contentType Content type of the payload to send as a header. Defaults to `application/json`. * * See the class documentation for details about the other parameters. + * */ fun messageInRaw( eventType: String,