Skip to content

Commit

Permalink
rust: Add MessageIn::{new_raw_payload, with_content_type} (#1533)
Browse files Browse the repository at this point in the history
… and add some extra detail to the docs of the kotlin equivalent.

Part of svix/monorepo-private#9470.
  • Loading branch information
svix-jplatte authored Nov 27, 2024
2 parents 34a910a + 92d1dcc commit 5bbb0fb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions kotlin/lib/src/main/kotlin/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern crate serde_derive;

pub mod api;
pub mod error;
mod model_ext;
mod request;
pub mod webhooks;

Expand Down
36 changes: 36 additions & 0 deletions rust/src/model_ext.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 5bbb0fb

Please sign in to comment.