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

java: Add convenient construction of rawPayload messages #1544

Merged
merged 1 commit into from
Dec 4, 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
57 changes: 48 additions & 9 deletions java/lib/src/main/java/com/svix/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.svix;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.svix.exceptions.ApiException;
import com.svix.internal.api.MessageApi;
Expand All @@ -16,18 +19,54 @@ public final class Message {
api = new MessageApi();
}

private static MessageIn messageInEmptyPayload() {
return new MessageIn()
.payload(Collections.emptyMap());
}

/**
* 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). The default Content-Type of application/json
* is used. See the other overload if you want to send non-JSON payloads.
*
* @param payload Serialized message payload
*/
public static MessageIn messageInRaw(final String payload) {
return messageInEmptyPayload().transformationsParams(
Collections.singletonMap("rawPayload", payload)
);
}

/**
* Creates a MessageIn with the payload already being serialized.
*
* This overload is intended for non-JSON payloads.
*
* @param payload Serialized message payload
* @param contentType The value to use for the Content-Type header
*/
public static MessageIn messageInRaw(final String payload, final String contentType) {
HashMap<String, Object> trParam = new HashMap<>();
trParam.put("rawPayload", payload);
trParam.put("headers", Collections.singletonMap("content-type", contentType));
return messageInEmptyPayload().transformationsParams(trParam);
}

public ListResponseMessageOut list(final String appId, final MessageListOptions options) throws ApiException {
try {
return api.v1MessageList(
appId,
options.getLimit(),
options.getIterator(),
options.getChannel(),
options.getBefore(),
options.getAfter(),
options.getWithContent(),
options.getTag(),
new HashSet<>(options.getEventTypes())
appId,
options.getLimit(),
options.getIterator(),
options.getChannel(),
options.getBefore(),
options.getAfter(),
options.getWithContent(),
options.getTag(),
new HashSet<>(options.getEventTypes())
);
} catch (com.svix.internal.ApiException e) {
throw Utils.wrapInternalApiException(e);
Expand Down