com.pubnub.internal.endpoints.push.AddChannelsToPushEndpoint.kt Maven / Gradle / Ivy
package com.pubnub.internal.endpoints.push
import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
import com.pubnub.api.endpoints.push.AddChannelsToPush
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.enums.PNPushEnvironment
import com.pubnub.api.enums.PNPushType
import com.pubnub.api.models.consumer.push.PNPushAddChannelResult
import com.pubnub.api.retry.RetryableEndpointGroup
import com.pubnub.internal.EndpointCore
import com.pubnub.internal.PubNubImpl
import com.pubnub.internal.toCsv
import retrofit2.Call
import retrofit2.Response
import java.util.Locale
/**
* @see [PubNubImpl.addPushNotificationsOnChannels]
*/
class AddChannelsToPushEndpoint internal constructor(
pubnub: PubNubImpl,
override val pushType: PNPushType,
override val channels: List,
override val deviceId: String,
override val topic: String? = null,
override val environment: PNPushEnvironment = PNPushEnvironment.DEVELOPMENT,
) : EndpointCore(pubnub), AddChannelsToPush {
override fun getAffectedChannels() = channels
override fun validateParams() {
super.validateParams()
if (deviceId.isBlank()) {
throw PubNubException(PubNubError.DEVICE_ID_MISSING)
}
if (channels.isEmpty()) {
throw PubNubException(PubNubError.CHANNEL_MISSING)
}
if (pushType == PNPushType.APNS2 && topic.isNullOrBlank()) {
throw PubNubException(PubNubError.PUSH_TOPIC_MISSING)
}
}
override fun doWork(queryParams: HashMap): Call {
addQueryParams(queryParams)
return if (pushType != PNPushType.APNS2) {
retrofitManager.pushService
.modifyChannelsForDevice(
subKey = configuration.subscribeKey,
pushToken = deviceId,
options = queryParams,
)
} else {
retrofitManager.pushService
.modifyChannelsForDeviceApns2(
subKey = configuration.subscribeKey,
deviceApns2 = deviceId,
options = queryParams,
)
}
}
override fun createResponse(input: Response): PNPushAddChannelResult = PNPushAddChannelResult()
override fun operationType() = PNOperationType.PNAddPushNotificationsOnChannelsOperation
override fun getEndpointGroupName(): RetryableEndpointGroup = RetryableEndpointGroup.PUSH_NOTIFICATION
private fun addQueryParams(queryParams: MutableMap) {
queryParams["add"] = channels.toCsv()
if (pushType != PNPushType.APNS2) {
queryParams["type"] = pushType.toParamString()
return
}
queryParams["environment"] = environment.name.lowercase(Locale.getDefault())
topic?.run { queryParams["topic"] = this }
}
}