|
| 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 | +} |
0 commit comments