com.pubnub.api.endpoints.access.GrantToken.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.access
import com.pubnub.api.Endpoint
import com.pubnub.api.PNConfiguration.Companion.isValid
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.models.consumer.access_manager.v3.ChannelGrant
import com.pubnub.api.models.consumer.access_manager.v3.ChannelGroupGrant
import com.pubnub.api.models.consumer.access_manager.v3.PNGrantTokenResult
import com.pubnub.api.models.consumer.access_manager.v3.UUIDGrant
import com.pubnub.api.models.server.access_manager.v3.GrantTokenRequestBody
import com.pubnub.api.models.server.access_manager.v3.GrantTokenResponse
import retrofit2.Call
import retrofit2.Response
class GrantToken(
pubnub: PubNub,
val ttl: Int,
private val meta: Any?,
private val authorizedUUID: String?,
private val channels: List,
private val channelGroups: List,
private val uuids: List
) : Endpoint(pubnub) {
override fun getAffectedChannels(): List = channels.map { it.id }
override fun getAffectedChannelGroups(): List = channelGroups.map { it.id }
override fun validateParams() {
if (!pubnub.configuration.secretKey.isValid()) {
throw PubNubException(PubNubError.SECRET_KEY_MISSING)
}
if (!pubnub.configuration.subscribeKey.isValid()) {
throw PubNubException(PubNubError.SUBSCRIBE_KEY_MISSING)
}
if ((channels + channelGroups + uuids).isEmpty()) {
throw PubNubException(pubnubError = PubNubError.RESOURCES_MISSING, errorMessage = "At least one grant required")
}
}
override fun doWork(queryParams: HashMap): Call {
val requestBody: GrantTokenRequestBody = GrantTokenRequestBody.of(
ttl = ttl,
channels = channels,
groups = channelGroups,
uuids = uuids,
meta = meta,
uuid = authorizedUUID
)
return pubnub.retrofitManager
.accessManagerService
.grantToken(pubnub.configuration.subscribeKey, requestBody, queryParams)
}
override fun createResponse(input: Response): PNGrantTokenResult? {
return input.body()?.data?.token?.let { PNGrantTokenResult(it) }
}
override fun operationType(): PNOperationType = PNOperationType.PNAccessManagerGrantToken
override fun isAuthRequired(): Boolean = false
}