com.pubnub.api.endpoints.access.Grant.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.google.gson.JsonElement
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.PNAccessManagerGrantResult
import com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData
import com.pubnub.api.models.server.Envelope
import com.pubnub.api.models.server.access_manager.AccessManagerGrantPayload
import com.pubnub.api.toCsv
import retrofit2.Call
import retrofit2.Response
import java.util.HashMap
/**
* @see [PubNub.grant]
*/
class Grant internal constructor(
pubnub: PubNub,
val read: Boolean = false,
val write: Boolean = false,
val manage: Boolean = false,
val delete: Boolean = false,
val ttl: Int = -1,
val authKeys: List = emptyList(),
val channels: List = emptyList(),
val channelGroups: List = emptyList()
) : Endpoint, PNAccessManagerGrantResult>(pubnub) {
override fun validateParams() {
super.validateParams()
if (!pubnub.configuration.secretKey.isValid()) throw PubNubException(PubNubError.SECRET_KEY_MISSING)
}
override fun getAffectedChannels() = channels
override fun getAffectedChannelGroups() = channelGroups
override fun doWork(queryParams: HashMap): Call> {
addQueryParams(queryParams)
return pubnub.retrofitManager.accessManagerService
.grant(
subKey = pubnub.configuration.subscribeKey,
options = queryParams
)
}
override fun createResponse(input: Response>): PNAccessManagerGrantResult? {
val data = input.body()!!.payload!!
val constructedChannels = mutableMapOf?>()
val constructedGroups = mutableMapOf?>()
// we have a case of a singular channel.
data.channel?.let {
constructedChannels[it] = data.authKeys!!
}
if (channelGroups.size == 1) {
constructedGroups[pubnub.mapper.elementToString(data.channelGroups)!!] = data.authKeys!!
} else if (channelGroups.size > 1) {
val it = pubnub.mapper.getObjectIterator(data.channelGroups!!)
while (it.hasNext()) {
val (k, v) = it.next()
constructedGroups[k] = createKeyMap(v)
}
}
data.channels?.forEach {
constructedChannels[it.key] = data.channels[it.key]!!.authKeys
}
return PNAccessManagerGrantResult(
level = data.level!!,
ttl = data.ttl,
subscribeKey = data.subscribeKey!!,
channels = constructedChannels,
channelGroups = constructedGroups
)
}
override fun operationType() = PNOperationType.PNAccessManagerGrant
override fun isAuthRequired() = false
private fun createKeyMap(input: JsonElement): Map {
val result: MutableMap =
HashMap()
val it: Iterator> =
pubnub.mapper.getObjectIterator(input, "auths")
while (it.hasNext()) {
val keyMap = it.next()
val pnAccessManagerKeyData = PNAccessManagerKeyData()
pnAccessManagerKeyData.manageEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "m"))
pnAccessManagerKeyData.writeEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "w"))
pnAccessManagerKeyData.readEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "r"))
pnAccessManagerKeyData.deleteEnabled = (pubnub.mapper.getAsBoolean(keyMap.value, "d"))
result[keyMap.key] = pnAccessManagerKeyData
}
return result
}
private fun addQueryParams(queryParams: MutableMap) {
channels.run { if (isNotEmpty()) queryParams["channel"] = toCsv() }
channelGroups.run { if (isNotEmpty()) queryParams["channel-group"] = toCsv() }
authKeys.run { if (isNotEmpty()) queryParams["auth"] = toCsv() }
if (ttl >= -1) {
queryParams["ttl"] = ttl.toString()
}
queryParams["r"] = if (read) "1" else "0"
queryParams["w"] = if (write) "1" else "0"
queryParams["m"] = if (manage) "1" else "0"
queryParams["d"] = if (delete) "1" else "0"
}
}