
com.pubnub.api.endpoints.presence.Heartbeat Maven / Gradle / Ivy
package com.pubnub.api.endpoints.presence;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.PubNubUtil;
import com.pubnub.api.builder.PubNubErrorBuilder;
import com.pubnub.api.endpoints.Endpoint;
import com.pubnub.api.enums.PNOperationType;
import com.pubnub.api.models.server.Envelope;
import lombok.Setter;
import lombok.experimental.Accessors;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Accessors(chain = true, fluent = true)
public class Heartbeat extends Endpoint {
@Setter
private Object state;
@Setter
private List channels;
@Setter
private List channelGroups;
public Heartbeat(PubNub pubnub, Retrofit retrofit) {
super(pubnub, retrofit);
channels = new ArrayList<>();
channelGroups = new ArrayList<>();
}
@Override
protected void validateParams() throws PubNubException {
if (this.getPubnub().getConfiguration().getSubscribeKey() == null || this.getPubnub().getConfiguration().getSubscribeKey().isEmpty()) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_SUBSCRIBE_KEY_MISSING).build();
}
if (channels.size() == 0 && channelGroups.size() == 0) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_CHANNEL_AND_GROUP_MISSING).build();
}
}
@Override
protected Call doWork(Map params) throws PubNubException {
ObjectWriter ow = new ObjectMapper().writer();
params.put("heartbeat", String.valueOf(this.getPubnub().getConfiguration().getPresenceTimeout()));
if (channelGroups.size() > 0) {
params.put("channel-group", PubNubUtil.joinString(channelGroups, ","));
}
String channelsCSV;
if (channels.size() > 0) {
channelsCSV = PubNubUtil.joinString(channels, ",");
} else {
channelsCSV = ",";
}
if (state != null) {
String stringifiedState;
try {
stringifiedState = ow.writeValueAsString(state);
} catch (JsonProcessingException e) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS).errormsg(e.getMessage()).build();
}
stringifiedState = PubNubUtil.urlEncode(stringifiedState);
params.put("state", stringifiedState);
}
PresenceService service = this.getRetrofit().create(PresenceService.class);
return service.heartbeat(this.getPubnub().getConfiguration().getSubscribeKey(), channelsCSV, params);
}
@Override
protected Boolean createResponse(Response input) throws PubNubException {
return true;
}
@Override
protected PNOperationType getOperationType() {
return PNOperationType.PNHeartbeatOperation;
}
@Override
protected boolean isAuthRequired() {
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy