Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kotlin: Add convenient construction of rawPayload messages #1531

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
}