-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECO-5193][TM*] Added test helpers for Chat message edit, update and …
…delete 1. Added ChatAPIClient class that sends authorized request for given url 2. Added ChatRoom class that extends ChatAPIClients, provides methods to send, update and delete the given messsage
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.ably.lib.chat; | ||
|
||
import com.google.gson.JsonElement; | ||
import io.ably.lib.http.HttpCore; | ||
import io.ably.lib.http.HttpUtils; | ||
import io.ably.lib.rest.AblyRest; | ||
import io.ably.lib.types.AblyException; | ||
import io.ably.lib.types.HttpPaginatedResponse; | ||
import io.ably.lib.types.Param; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
public class ChatAPIClient { | ||
|
||
private final int API_PROTOCOL_VERSION = 3; | ||
private final String PROTOCOL_VERSION_PARAM_NAME = "v"; | ||
private final Param apiProtocolParam = new Param(PROTOCOL_VERSION_PARAM_NAME, API_PROTOCOL_VERSION); | ||
|
||
AblyRest ablyRest; | ||
|
||
ChatAPIClient(AblyRest ablyRest) { | ||
this.ablyRest = ablyRest; | ||
} | ||
|
||
protected Optional<JsonElement> makeAuthorizedRequest(String url, String method, JsonElement body) throws AblyException { | ||
HttpCore.RequestBody httpRequestBody = HttpUtils.requestBodyFromGson(body, ablyRest.options.useBinaryProtocol); | ||
HttpPaginatedResponse response = ablyRest.request(method, url, new Param[]{apiProtocolParam}, httpRequestBody, null); | ||
return Arrays.stream(response.items()).findFirst(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.ably.lib.chat; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import io.ably.lib.rest.AblyRest; | ||
|
||
import java.util.Map; | ||
|
||
public class ChatRoom extends ChatAPIClient{ | ||
private final String roomId; | ||
|
||
private ChatRoom(AblyRest ablyRest, String roomId) { | ||
super(ablyRest); | ||
this.roomId = roomId; | ||
} | ||
|
||
public JsonElement sendMessage(SendMessageParams params) throws Exception { | ||
return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages", "POST", params.toJsonObject()) | ||
.orElseThrow(() -> new Exception("Failed to send message")); | ||
} | ||
|
||
public JsonElement updateMessage(String serial, UpdateMessageParams params) throws Exception { | ||
return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial, "PUT", params.toJsonObject()) | ||
.orElseThrow(() -> new Exception("Failed to update message")); | ||
} | ||
|
||
public JsonElement deleteMessage(String serial, DeleteMessageParams params) throws Exception { | ||
return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial + "/delete", "POST", params.toJsonObject()) | ||
.orElseThrow(() -> new Exception("Failed to delete message")); | ||
} | ||
|
||
public static class SendMessageParams { | ||
private final String text; | ||
private final JsonElement metadata; | ||
private final Map<String, String> headers; | ||
|
||
public SendMessageParams(String text, JsonElement metadata, Map<String, String> headers) { | ||
this.text = text; | ||
this.metadata = metadata; | ||
this.headers = headers; | ||
} | ||
|
||
private JsonObject toJsonObject() { | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("text", text); | ||
// (CHA-M3b) | ||
if (headers != null) { | ||
JsonObject headersJson = new JsonObject(); | ||
headers.forEach(headersJson::addProperty); | ||
jsonObject.add("headers", headersJson); | ||
} | ||
// (CHA-M3b) | ||
if (metadata != null) { | ||
jsonObject.add("metadata", metadata); | ||
} | ||
return jsonObject; | ||
} | ||
} | ||
|
||
public static class UpdateMessageParams { | ||
private final SendMessageParams message; | ||
private final String description; | ||
private final JsonElement metadata; | ||
|
||
public UpdateMessageParams(SendMessageParams message, String description, JsonElement metadata) { | ||
this.message = message; | ||
this.description = description; | ||
this.metadata = metadata; | ||
} | ||
|
||
private JsonObject toJsonObject() { | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.add("message", message.toJsonObject()); | ||
if (description != null) { | ||
jsonObject.addProperty("description", description); | ||
} | ||
if (metadata != null) { | ||
jsonObject.add("metadata", metadata); | ||
} | ||
return jsonObject; | ||
} | ||
} | ||
|
||
public static class DeleteMessageParams { | ||
private final String description; | ||
private final JsonElement metadata; | ||
|
||
public DeleteMessageParams(String description, JsonElement metadata) { | ||
this.description = description; | ||
this.metadata = metadata; | ||
} | ||
|
||
private JsonObject toJsonObject() { | ||
JsonObject jsonObject = new JsonObject(); | ||
if (description != null) { | ||
jsonObject.addProperty("description", description); | ||
} | ||
if (metadata != null) { | ||
jsonObject.add("metadata", metadata); | ||
} | ||
return jsonObject; | ||
} | ||
} | ||
} |