Skip to content

Commit

Permalink
kotlin: Add convenient construction of rawPayload messages (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-jplatte authored Nov 26, 2024
2 parents 2cf9eb0 + eabe234 commit f3ff715
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions kotlin/lib/src/main/kotlin/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.svix.kotlin
import com.svix.kotlin.exceptions.ApiException
import com.svix.kotlin.internal.apis.MessageApi
import com.svix.kotlin.models.ListResponseMessageOut
import com.svix.kotlin.models.ApplicationIn
import com.svix.kotlin.models.MessageIn
import com.svix.kotlin.models.MessageOut

Expand Down Expand Up @@ -71,3 +72,43 @@ class Message internal constructor(token: String, options: SvixOptions) {
}
}
}

/**
* Creates a [MessageIn] with the payload already being serialized.
*
* @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,
payload: String,
contentType: String? = null,
application: ApplicationIn? = null,
channels: Set<String>? = null,
eventId: String? = null,
payloadRetentionHours: Long? = null,
payloadRetentionPeriod: Long? = 90L,
tags: Set<String>? = null,
transformationsParams: Map<String, Any> = mapOf(),
): MessageIn {
val transformationsParams = transformationsParams.toMutableMap()
transformationsParams.put("rawPayload", payload)
if (contentType != null) {
val headers = mapOf("content-type" to contentType)
transformationsParams.put("headers", headers)
}

return MessageIn(
eventType = eventType,
payload = mapOf<String, String>(),
application = application,
channels = channels,
eventId = eventId,
payloadRetentionHours = payloadRetentionHours,
payloadRetentionPeriod = payloadRetentionPeriod,
tags = tags,
transformationsParams = transformationsParams,
)
}

0 comments on commit f3ff715

Please sign in to comment.