Skip to content

Commit cdd563b

Browse files
marceloinacioMax Presman
authored andcommitted
change result to PNHistoryForChannelsResult
1 parent e0a47f2 commit cdd563b

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

src/main/java/com/pubnub/api/endpoints/FetchMessages.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import com.pubnub.api.builder.PubNubErrorBuilder;
99
import com.pubnub.api.enums.PNOperationType;
1010
import com.pubnub.api.managers.MapperManager;
11-
import com.pubnub.api.models.server.HistoryForChannelsEnvelope;
11+
import com.pubnub.api.models.consumer.history.PNFetchMessagesResult;
12+
import com.pubnub.api.models.consumer.pubsub.PNMessageResult;
13+
import com.pubnub.api.models.server.FetchMessagesEnvelope;
1214
import com.pubnub.api.models.server.HistoryForChannelsItem;
1315
import com.pubnub.api.vendor.Crypto;
1416
import lombok.Setter;
@@ -21,11 +23,12 @@
2123
import retrofit2.http.QueryMap;
2224

2325
import java.util.ArrayList;
26+
import java.util.HashMap;
2427
import java.util.List;
2528
import java.util.Map;
2629

2730
@Accessors(chain = true, fluent = true)
28-
public class FetchMessages extends Endpoint<HistoryForChannelsEnvelope, HistoryForChannelsEnvelope> {
31+
public class FetchMessages extends Endpoint<FetchMessagesEnvelope, PNFetchMessagesResult> {
2932
private static final int MAX_MESSAGES = 25;
3033
@Setter
3134
private List<String> channels;
@@ -44,9 +47,9 @@ public FetchMessages(PubNub pubnub, Retrofit retrofit) {
4447

4548
private interface HistoryForChannelsService {
4649
@GET("v3/history/sub-key/{subKey}/channel/{channels}")
47-
Call<HistoryForChannelsEnvelope> fetchHistoryForChannels(@Path("subKey") String subKey,
48-
@Path("channels") String channels,
49-
@QueryMap Map<String, String> options);
50+
Call<FetchMessagesEnvelope> fetchMessages(@Path("subKey") String subKey,
51+
@Path("channels") String channels,
52+
@QueryMap Map<String, String> options);
5053
}
5154

5255
@Override
@@ -62,7 +65,7 @@ protected void validateParams() throws PubNubException {
6265
}
6366

6467
@Override
65-
protected Call<HistoryForChannelsEnvelope> doWork(Map<String, String> params) {
68+
protected Call<FetchMessagesEnvelope> doWork(Map<String, String> params) {
6669

6770
HistoryForChannelsService service = this.getRetrofit().create(HistoryForChannelsService.class);
6871

@@ -75,27 +78,39 @@ protected Call<HistoryForChannelsEnvelope> doWork(Map<String, String> params) {
7578
params.put("end", Long.toString(end).toLowerCase());
7679
}
7780

78-
return service.fetchHistoryForChannels(this.getPubnub().getConfiguration().getSubscribeKey(), PubNubUtil.joinString(channels, ","), params);
81+
return service.fetchMessages(this.getPubnub().getConfiguration().getSubscribeKey(), PubNubUtil.joinString(channels, ","), params);
7982
}
8083

8184
@Override
82-
protected HistoryForChannelsEnvelope createResponse(Response<HistoryForChannelsEnvelope> input) throws PubNubException {
85+
protected PNFetchMessagesResult createResponse(Response<FetchMessagesEnvelope> input) throws PubNubException {
8386
if (input.body() == null) {
8487
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build();
8588
}
8689

87-
HistoryForChannelsEnvelope envelope = input.body();
88-
Map<String, List<HistoryForChannelsItem>> channelsList = envelope.getChannels();
90+
PNFetchMessagesResult.PNFetchMessagesResultBuilder result = PNFetchMessagesResult.builder();
91+
Map<String, List<PNMessageResult>> listMap = new HashMap<>();
92+
93+
FetchMessagesEnvelope envelope = input.body();
94+
95+
for (Map.Entry<String, List<HistoryForChannelsItem>> entry : envelope.getChannels().entrySet()) {
96+
97+
List<PNMessageResult> messages = new ArrayList<>();
8998

90-
for (Map.Entry<String, List<HistoryForChannelsItem>> entry : channelsList.entrySet()) {
9199
for (HistoryForChannelsItem item: entry.getValue()) {
100+
PNMessageResult.PNMessageResultBuilder pnMessageResultBuilder = PNMessageResult.builder();
101+
pnMessageResultBuilder.channel(entry.getKey());
92102
JsonNode message = processMessage(item.getMessage());
93-
item.setMessage(message);
103+
pnMessageResultBuilder.message(message);
104+
pnMessageResultBuilder.timetoken(item.getTimeToken());
105+
messages.add(pnMessageResultBuilder.build());
94106
}
95107

108+
listMap.put(entry.getKey(), messages);
96109
}
97110

98-
return envelope;
111+
result.channels(listMap);
112+
113+
return result.build();
99114
}
100115

101116
@Override
@@ -120,19 +135,19 @@ private JsonNode processMessage(JsonNode message) throws PubNubException {
120135
String outputText;
121136
JsonNode outputObject;
122137

123-
if (message.isObject() && message.has("message")) {
124-
inputText = message.get("message").asText();
138+
if (message.isObject() && message.has("pn_other")) {
139+
inputText = message.get("pn_other").asText();
125140
} else {
126141
inputText = message.asText();
127142
}
128143

129144
outputText = crypto.decrypt(inputText);
130145
outputObject = mapper.fromJson(outputText, JsonNode.class);
131146

132-
// inject the decoded response into the payload
133-
if (message.isObject() && message.has("message")) {
147+
// inject the decoded resposne into the payload
148+
if (message.isObject() && message.has("pn_other")) {
134149
ObjectNode objectNode = (ObjectNode) message;
135-
objectNode.set("message", outputObject);
150+
objectNode.set("pn_other", outputObject);
136151
outputObject = objectNode;
137152
}
138153

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.pubnub.api.models.consumer.history;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.pubnub.api.models.consumer.pubsub.PNMessageResult;
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@Getter
12+
@Builder
13+
public class PNFetchMessagesResult {
14+
@JsonProperty("channels")
15+
private Map<String, List<PNMessageResult>> channels;
16+
17+
18+
}

src/main/java/com/pubnub/api/models/server/HistoryForChannelsEnvelope.java renamed to src/main/java/com/pubnub/api/models/server/FetchMessagesEnvelope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@Getter
1111
@Setter
12-
public class HistoryForChannelsEnvelope {
12+
public class FetchMessagesEnvelope {
1313

1414
@JsonProperty("channels")
1515
private Map<String, List<HistoryForChannelsItem>> channels;

src/test/java/com/pubnub/api/endpoints/FetchMessagesEndpointTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
55
import com.pubnub.api.PubNub;
66
import com.pubnub.api.PubNubException;
7-
import com.pubnub.api.models.server.HistoryForChannelsEnvelope;
7+
import com.pubnub.api.models.consumer.history.PNFetchMessagesResult;
88
import org.junit.Assert;
99
import org.junit.Before;
1010
import org.junit.Rule;
@@ -43,9 +43,9 @@ public void beforeEach() throws IOException {
4343
@Test
4444
public void testSyncSuccess() throws PubNubException {
4545
stubFor(get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
46-
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"hihi\",\"timetoken\":\"14698320467224036\"},{\"message\":\"hihi\",\"timetoken\":\"14698320468265639\"}],\"mychannel\":[{\"message\":\"sample message\",\"timetoken\":\"14369823849575729\"}]}}")));
46+
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"hihi\",\"timetoken\":\"14698320467224036\"},{\"message\":\"Hey\",\"timetoken\":\"14698320468265639\"}],\"mychannel\":[{\"message\":\"sample message\",\"timetoken\":\"14369823849575729\"}]}}")));
4747

48-
HistoryForChannelsEnvelope response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
48+
PNFetchMessagesResult response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
4949

5050
Assert.assertEquals(response.getChannels().size(), 2);
5151
Assert.assertEquals(response.getChannels().containsKey("mychannel"), true);
@@ -57,11 +57,11 @@ public void testSyncSuccess() throws PubNubException {
5757
@Test
5858
public void testSyncAuthSuccess() throws PubNubException {
5959
stubFor(get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
60-
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"hihi\",\"timetoken\":\"14698320467224036\"},{\"message\":\"hihi\",\"timetoken\":\"14698320468265639\"}],\"mychannel\":[{\"message\":\"sample message\",\"timetoken\":\"14369823849575729\"}]}}")));
60+
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"hihi\",\"timetoken\":\"14698320467224036\"},{\"message\":\"Hey\",\"timetoken\":\"14698320468265639\"}],\"mychannel\":[{\"message\":\"sample message\",\"timetoken\":\"14369823849575729\"}]}}")));
6161

6262
pubnub.getConfiguration().setAuthKey("authKey");
6363

64-
HistoryForChannelsEnvelope response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
64+
PNFetchMessagesResult response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
6565

6666
Assert.assertEquals(response.getChannels().size(), 2);
6767
Assert.assertEquals(response.getChannels().containsKey("mychannel"), true);
@@ -81,7 +81,7 @@ public void testSyncEncryptedSuccess() throws PubNubException {
8181
stubFor(get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
8282
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"jC/yJ2y99BeYFYMQ7c53pg==\",\"timetoken\":\"14797423056306675\"}],\"mychannel\":[{\"message\":\"jC/yJ2y99BeYFYMQ7c53pg==\",\"timetoken\":\"14797423056306675\"}]}}")));
8383

84-
HistoryForChannelsEnvelope response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
84+
PNFetchMessagesResult response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
8585

8686
Assert.assertEquals(response.getChannels().size(), 2);
8787
Assert.assertEquals(response.getChannels().containsKey("mychannel"), true);

0 commit comments

Comments
 (0)