Skip to content

Commit 0943416

Browse files
author
Max Presman
committed
delete messages endpoint
1 parent 54de81e commit 0943416

6 files changed

Lines changed: 211 additions & 0 deletions

File tree

‎src/main/java/com/pubnub/api/PubNub.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.pubnub.api.builder.SubscribeBuilder;
55
import com.pubnub.api.builder.UnsubscribeBuilder;
66
import com.pubnub.api.callbacks.SubscribeCallback;
7+
import com.pubnub.api.endpoints.DeleteMessages;
78
import com.pubnub.api.endpoints.FetchMessages;
89
import com.pubnub.api.endpoints.History;
910
import com.pubnub.api.endpoints.Time;
@@ -131,6 +132,10 @@ public FetchMessages fetchMessages() {
131132
return new FetchMessages(this, this.retrofitManager.getTransactionInstance());
132133
}
133134

135+
public DeleteMessages deleteMessages() {
136+
return new DeleteMessages(this, this.retrofitManager.getTransactionInstance());
137+
}
138+
134139
public Audit audit() {
135140
return new Audit(this, this.retrofitManager.getTransactionInstance());
136141
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.pubnub.api.endpoints;
2+
3+
import com.pubnub.api.PubNub;
4+
import com.pubnub.api.PubNubException;
5+
import com.pubnub.api.PubNubUtil;
6+
import com.pubnub.api.builder.PubNubErrorBuilder;
7+
import com.pubnub.api.enums.PNOperationType;
8+
import com.pubnub.api.models.consumer.history.PNDeleteMessagesResult;
9+
import com.pubnub.api.models.server.DeleteMessagesEnvelope;
10+
import lombok.Setter;
11+
import lombok.experimental.Accessors;
12+
import retrofit2.Call;
13+
import retrofit2.Response;
14+
import retrofit2.Retrofit;
15+
import retrofit2.http.DELETE;
16+
import retrofit2.http.Path;
17+
import retrofit2.http.QueryMap;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
@Accessors(chain = true, fluent = true)
24+
25+
public class DeleteMessages extends Endpoint<DeleteMessagesEnvelope, PNDeleteMessagesResult> {
26+
27+
@Setter
28+
private List<String> channels;
29+
@Setter
30+
private Long start;
31+
@Setter
32+
private Long end;
33+
34+
private interface DeleteHistoryService {
35+
@DELETE("v3/history/sub-key/{subKey}/channel/{channels}")
36+
Call<DeleteMessagesEnvelope> deleteMessages(@Path("subKey") String subKey,
37+
@Path("channels") String channels,
38+
@QueryMap Map<String, String> options);
39+
}
40+
41+
public DeleteMessages(PubNub pubnubInstance, Retrofit retrofitInstance) {
42+
super(pubnubInstance, retrofitInstance);
43+
channels = new ArrayList<>();
44+
}
45+
46+
@Override
47+
protected List<String> getAffectedChannels() {
48+
return channels;
49+
}
50+
51+
@Override
52+
protected List<String> getAffectedChannelGroups() {
53+
return null;
54+
}
55+
56+
@Override
57+
protected void validateParams() throws PubNubException {
58+
if (channels == null || channels.size() == 0) {
59+
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_CHANNEL_MISSING).build();
60+
}
61+
}
62+
63+
@Override
64+
protected Call<DeleteMessagesEnvelope> doWork(Map<String, String> params) throws PubNubException {
65+
DeleteHistoryService service = this.getRetrofit().create(DeleteHistoryService.class);
66+
67+
if (start != null) {
68+
params.put("start", Long.toString(start).toLowerCase());
69+
}
70+
if (end != null) {
71+
params.put("end", Long.toString(end).toLowerCase());
72+
}
73+
74+
return service.deleteMessages(this.getPubnub().getConfiguration().getSubscribeKey(), PubNubUtil.joinString(channels, ","), params);
75+
}
76+
77+
@Override
78+
protected PNDeleteMessagesResult createResponse(Response<DeleteMessagesEnvelope> input) throws PubNubException {
79+
if (input.body() == null || input.body().getStatus() == null || input.body().getStatus() != 200) {
80+
String errorMsg = null;
81+
82+
if (input.body() != null && input.body().getErrorMessage() != null){
83+
errorMsg = input.body().getErrorMessage();
84+
} else {
85+
errorMsg = "n/a";
86+
}
87+
88+
throw PubNubException.builder()
89+
.pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR)
90+
.errormsg(errorMsg)
91+
.build();
92+
}
93+
94+
return PNDeleteMessagesResult.builder().build();
95+
}
96+
97+
@Override
98+
protected PNOperationType getOperationType() {
99+
return PNOperationType.PNDeleteMessagesOperation;
100+
}
101+
102+
@Override
103+
protected boolean isAuthRequired() {
104+
return true;
105+
}
106+
}

‎src/main/java/com/pubnub/api/enums/PNOperationType.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public enum PNOperationType {
1111

1212
PNHistoryOperation,
1313
PNFetchMessagesOperation,
14+
PNDeleteMessagesOperation,
1415

1516
PNWhereNowOperation,
1617

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.pubnub.api.models.consumer.history;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.ToString;
6+
7+
@Getter
8+
@Builder
9+
@ToString
10+
public class PNDeleteMessagesResult {
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.pubnub.api.models.server;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
@Setter
9+
public class DeleteMessagesEnvelope {
10+
11+
private Integer status;
12+
private boolean error;
13+
@SerializedName("error_message")
14+
private String errorMessage;
15+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.pubnub.api.endpoints;
2+
3+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
4+
import com.pubnub.api.PubNub;
5+
import com.pubnub.api.PubNubException;
6+
import com.pubnub.api.models.consumer.history.PNDeleteMessagesResult;
7+
import org.junit.Before;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
import java.util.Arrays;
13+
14+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
15+
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
16+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
17+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
18+
import static org.junit.Assert.assertNotNull;
19+
20+
21+
public class DeleteMessagesEndpointTest extends TestHarness {
22+
23+
private DeleteMessages partialHistory;
24+
private PubNub pubnub;
25+
26+
@Rule
27+
public WireMockRule wireMockRule = new WireMockRule();
28+
29+
@Before
30+
public void beforeEach() throws IOException {
31+
pubnub = this.createPubNubInstance(8080);
32+
partialHistory = pubnub.deleteMessages();
33+
wireMockRule.start();
34+
}
35+
36+
37+
@Test
38+
public void testSyncSuccess() throws PubNubException {
39+
stubFor(delete(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
40+
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": False, \"error_message\": \"\"}")));
41+
42+
PNDeleteMessagesResult response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).sync();
43+
44+
assertNotNull(response);
45+
}
46+
47+
@Test
48+
public void testSyncAuthSuccess() throws PubNubException {
49+
stubFor(delete(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
50+
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": False, \"error_message\": \"\"}")));
51+
52+
pubnub.getConfiguration().setAuthKey("authKey");
53+
54+
PNDeleteMessagesResult response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).sync();
55+
56+
assertNotNull(response);
57+
}
58+
59+
@Test
60+
public void testFailure() throws PubNubException {
61+
stubFor(delete(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
62+
.willReturn(aResponse().withBody("{\"status\": 403, \"error\": False, \"error_message\": \"wut\"}")));
63+
64+
pubnub.getConfiguration().setAuthKey("authKey");
65+
66+
try {
67+
partialHistory.channels(Arrays.asList("mychannel,my_channel")).sync();
68+
} catch (PubNubException ex) {
69+
assert(ex.getErrormsg().equals("wut"));
70+
}
71+
}
72+
73+
}

0 commit comments

Comments
 (0)