Skip to content

Commit 3616398

Browse files
committed
[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
1 parent e8fe70b commit 3616398

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.ably.lib.chat;
2+
3+
import com.google.gson.JsonElement;
4+
import io.ably.lib.http.HttpCore;
5+
import io.ably.lib.http.HttpUtils;
6+
import io.ably.lib.rest.AblyRest;
7+
import io.ably.lib.types.AblyException;
8+
import io.ably.lib.types.HttpPaginatedResponse;
9+
import io.ably.lib.types.Param;
10+
import java.util.Arrays;
11+
import java.util.Optional;
12+
13+
public class ChatAPIClient {
14+
15+
private final int API_PROTOCOL_VERSION = 3;
16+
private final String PROTOCOL_VERSION_PARAM_NAME = "v";
17+
private final Param apiProtocolParam = new Param(PROTOCOL_VERSION_PARAM_NAME, API_PROTOCOL_VERSION);
18+
19+
AblyRest ablyRest;
20+
21+
ChatAPIClient(AblyRest ablyRest) {
22+
this.ablyRest = ablyRest;
23+
}
24+
25+
protected Optional<JsonElement> makeAuthorizedRequest(String url, String method, JsonElement body) throws AblyException {
26+
HttpCore.RequestBody httpRequestBody = HttpUtils.requestBodyFromGson(body, ablyRest.options.useBinaryProtocol);
27+
HttpPaginatedResponse response = ablyRest.request(method, url, new Param[]{apiProtocolParam}, httpRequestBody, null);
28+
return Arrays.stream(response.items()).findFirst();
29+
}
30+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package io.ably.lib.chat;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import io.ably.lib.rest.AblyRest;
6+
7+
import java.util.Map;
8+
9+
public class ChatRoom extends ChatAPIClient{
10+
private final String roomId;
11+
12+
private ChatRoom(AblyRest ablyRest, String roomId) {
13+
super(ablyRest);
14+
this.roomId = roomId;
15+
}
16+
17+
public JsonElement sendMessage(SendMessageParams params) throws Exception {
18+
return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages", "POST", params.toJsonObject())
19+
.orElseThrow(() -> new Exception("Failed to send message"));
20+
}
21+
22+
public JsonElement updateMessage(String serial, UpdateMessageParams params) throws Exception {
23+
return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial, "PUT", params.toJsonObject())
24+
.orElseThrow(() -> new Exception("Failed to update message"));
25+
}
26+
27+
public JsonElement deleteMessage(String serial, DeleteMessageParams params) throws Exception {
28+
return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial + "/delete", "POST", params.toJsonObject())
29+
.orElseThrow(() -> new Exception("Failed to delete message"));
30+
}
31+
32+
public static class SendMessageParams {
33+
private final String text;
34+
private final JsonElement metadata;
35+
private final Map<String, String> headers;
36+
37+
public SendMessageParams(String text, JsonElement metadata, Map<String, String> headers) {
38+
this.text = text;
39+
this.metadata = metadata;
40+
this.headers = headers;
41+
}
42+
43+
private JsonObject toJsonObject() {
44+
JsonObject jsonObject = new JsonObject();
45+
jsonObject.addProperty("text", text);
46+
// (CHA-M3b)
47+
if (headers != null) {
48+
JsonObject headersJson = new JsonObject();
49+
headers.forEach(headersJson::addProperty);
50+
jsonObject.add("headers", headersJson);
51+
}
52+
// (CHA-M3b)
53+
if (metadata != null) {
54+
jsonObject.add("metadata", metadata);
55+
}
56+
return jsonObject;
57+
}
58+
}
59+
60+
public static class UpdateMessageParams {
61+
private final SendMessageParams message;
62+
private final String description;
63+
private final JsonElement metadata;
64+
65+
public UpdateMessageParams(SendMessageParams message, String description, JsonElement metadata) {
66+
this.message = message;
67+
this.description = description;
68+
this.metadata = metadata;
69+
}
70+
71+
private JsonObject toJsonObject() {
72+
JsonObject jsonObject = new JsonObject();
73+
jsonObject.add("message", message.toJsonObject());
74+
if (description != null) {
75+
jsonObject.addProperty("description", description);
76+
}
77+
if (metadata != null) {
78+
jsonObject.add("metadata", metadata);
79+
}
80+
return jsonObject;
81+
}
82+
}
83+
84+
public static class DeleteMessageParams {
85+
private final String description;
86+
private final JsonElement metadata;
87+
88+
public DeleteMessageParams(String description, JsonElement metadata) {
89+
this.description = description;
90+
this.metadata = metadata;
91+
}
92+
93+
private JsonObject toJsonObject() {
94+
JsonObject jsonObject = new JsonObject();
95+
if (description != null) {
96+
jsonObject.addProperty("description", description);
97+
}
98+
if (metadata != null) {
99+
jsonObject.add("metadata", metadata);
100+
}
101+
return jsonObject;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)