Skip to content

Commit

Permalink
java: Add convenient construction of rawPayload messages (#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-jplatte authored Dec 4, 2024
2 parents 1a5d746 + ceb9668 commit b2f0708
Showing 1 changed file with 48 additions and 9 deletions.
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

0 comments on commit b2f0708

Please sign in to comment.