com.pubnub.api.endpoints.push.ListPushProvisions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-kotlin Show documentation
Show all versions of pubnub-kotlin Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of
broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter
second!
package com.pubnub.api.endpoints.push
import com.pubnub.api.Endpoint
import com.pubnub.api.PubNub
import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
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.PNPushListProvisionsResult
import retrofit2.Call
import retrofit2.Response
import java.util.HashMap
import java.util.Locale
/**
* @see [PubNub.auditPushChannelProvisions]
*/
class ListPushProvisions internal constructor(
pubnub: PubNub,
val pushType: PNPushType,
val deviceId: String,
val topic: String? = null,
val environment: PNPushEnvironment = PNPushEnvironment.DEVELOPMENT
) : Endpoint, PNPushListProvisionsResult>(pubnub) {
override fun validateParams() {
super.validateParams()
if (deviceId.isBlank()) throw PubNubException(PubNubError.DEVICE_ID_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)
pubnub.retrofitManager.pushService
.listChannelsForDevice(
subKey = pubnub.configuration.subscribeKey,
pushToken = deviceId,
options = queryParams
)
else
pubnub.retrofitManager.pushService
.listChannelsForDeviceApns2(
subKey = pubnub.configuration.subscribeKey,
deviceApns2 = deviceId,
options = queryParams
)
}
override fun createResponse(input: Response>): PNPushListProvisionsResult =
PNPushListProvisionsResult(input.body()!!)
override fun operationType() = PNOperationType.PNPushNotificationEnabledChannelsOperation
private fun addQueryParams(queryParams: MutableMap) {
if (pushType != PNPushType.APNS2) {
queryParams["type"] = pushType.toParamString()
return
}
queryParams["environment"] = environment.name.lowercase(Locale.getDefault())
topic?.run { queryParams["topic"] = this }
}
}