Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DIA-4611 integrate custom consent call into Android SDK #836

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmplibrary/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ android {
}

dependencies {
implementation("com.sourcepoint:core:0.0.5")
implementation("com.sourcepoint:core:0.0.6")

// kotlin
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,19 @@ internal class SpConsentLibImpl(
legIntCategories: List<String>,
success: (SPConsents?) -> Unit,
) {
val customConsentReq = CustomConsentReq(
consentUUID = campaignManager.gdprUuid ?: "",
propertyId = campaignManager.spConfig.propertyId,
categories = categories,
legIntCategories = legIntCategories,
vendors = vendors
)
executor.run {
executeOnWorkerThread {
val ccResp = service.sendCustomConsentServ(customConsentReq, env)
if (campaignManager.gdprUuid.isNullOrEmpty()) {
spClient.onError(IllegalStateException("gdprUuid cannot be nil or empty!"))
return@executeOnWorkerThread
}
val ccResp = service.sendCustomConsentServ(
consentUUID = campaignManager.gdprUuid!!,
propertyId = campaignManager.spConfig.propertyId,
vendors = vendors,
categories = categories,
legIntCategories = legIntCategories
)
executeOnMain {
when (ccResp) {
is Either.Right -> success(userConsents(context))
Expand All @@ -267,16 +270,18 @@ internal class SpConsentLibImpl(
legIntCategories: List<String>,
success: (SPConsents?) -> Unit
) {
val customConsentReq = CustomConsentReq(
consentUUID = campaignManager.gdprUuid ?: "",
propertyId = campaignManager.spConfig.propertyId,
categories = categories,
legIntCategories = legIntCategories,
vendors = vendors
)
executor.run {
executeOnWorkerThread {
val ccResp = service.deleteCustomConsentToServ(customConsentReq, env)
if (campaignManager.gdprUuid.isNullOrEmpty()) {
spClient.onError(IllegalStateException("gdprUuid cannot be nil or empty!"))
}
val ccResp = service.deleteCustomConsentToServ(
consentUUID = campaignManager.gdprUuid!!,
propertyId = campaignManager.spConfig.propertyId,
vendors = vendors,
categories = categories,
legIntCategories = legIntCategories
)
executeOnMain {
when (ccResp) {
is Either.Right -> success(userConsents(context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.sourcepoint.cmplibrary.data.network.model.optimized.MessagesResp
import com.sourcepoint.cmplibrary.data.network.model.optimized.choice.ChoiceResp
import com.sourcepoint.cmplibrary.data.network.util.Env
import com.sourcepoint.cmplibrary.model.ConsentActionImpl
import com.sourcepoint.cmplibrary.model.CustomConsentReq
import com.sourcepoint.cmplibrary.model.exposed.SPConsents

/**
Expand All @@ -25,13 +24,19 @@ internal interface Service : NetworkClient, CampaignManager {
): Either<ChoiceResp>

fun sendCustomConsentServ(
customConsentReq: CustomConsentReq,
env: Env
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): Either<GdprCS>

fun deleteCustomConsentToServ(
customConsentReq: CustomConsentReq,
env: Env
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): Either<GdprCS>

fun getMessages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import com.sourcepoint.cmplibrary.core.* //ktlint-disable
import com.sourcepoint.cmplibrary.data.local.DataStorage
import com.sourcepoint.cmplibrary.data.network.NetworkClient
import com.sourcepoint.cmplibrary.data.network.connection.ConnectionManager
import com.sourcepoint.cmplibrary.data.network.converter.JsonConverter
import com.sourcepoint.cmplibrary.data.network.converter.converter
import com.sourcepoint.cmplibrary.data.network.converter.genericFail
import com.sourcepoint.cmplibrary.data.network.model.optimized.* //ktlint-disable
import com.sourcepoint.cmplibrary.data.network.model.optimized.choice.ChoiceResp
Expand All @@ -35,8 +33,6 @@ import com.sourcepoint.cmplibrary.util.extensions.toMapOfAny
import com.sourcepoint.mobile_core.network.requests.MetaDataRequest
import com.sourcepoint.mobile_core.network.responses.MetaDataResponse
import kotlinx.serialization.json.jsonObject
import org.json.JSONArray
import org.json.JSONObject

/**
* Factory method to create an instance of a [Service] using its implementation
Expand Down Expand Up @@ -69,67 +65,76 @@ internal class ServiceImpl(
private val connectionManager: ConnectionManager,
) : Service, NetworkClient by networkClient, CampaignManager by campaignManager {

private fun JSONArray.toArrayList(): ArrayList<String> {
val list = arrayListOf<String>()
for (i in 0 until this.length()) {
list.add(this.getString(i))
}
return list
}

override fun sendCustomConsentServ(customConsentReq: CustomConsentReq, env: Env): Either<GdprCS> = check {
override fun sendCustomConsentServ(
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): Either<GdprCS> = check {
if (connectionManager.isConnected.not()) throw NoInternetConnectionException()

networkClient.sendCustomConsent(customConsentReq, env)
.map {
if (campaignManager.gdprConsentStatus == null) {
genericFail("CustomConsent cannot be executed. Consent is missing!!!")
}

val categories: List<String> = (it.content.get("categories") as JSONArray).toArrayList()
val vendors: List<String> = (it.content.get("vendors") as JSONArray).toArrayList()
val legIntCategories: List<String> = (it.content.get("legIntCategories") as JSONArray).toArrayList()
val specialFeatures: List<String> = (it.content.get("specialFeatures") as JSONArray).toArrayList()

val grantsString: String = (it.content.get("grants") as JSONObject).toString()
val grants = JsonConverter.converter.decodeFromString<Map<String, GDPRPurposeGrants>>(grantsString)
val updatedGrants = campaignManager.gdprConsentStatus?.copy(
grants = grants,
categories = categories,
vendors = vendors,
legIntCategories = legIntCategories,
specialFeatures = specialFeatures
)
campaignManager.gdprConsentStatus = updatedGrants
try {
val response = networkClient.sendCustomConsent(
consentUUID = consentUUID,
propertyId = propertyId,
vendors = vendors,
categories = categories,
legIntCategories = legIntCategories
)
if (campaignManager.gdprConsentStatus == null) {
throw IllegalStateException("CustomConsent cannot be executed. Consent is missing!!!")
}
campaignManager.gdprConsentStatus!!
val grants = response.grants.map {
e -> e.key to GDPRPurposeGrants(e.value.vendorGrant,e.value.purposeGrants)
}.toMap()
campaignManager.gdprConsentStatus = campaignManager.gdprConsentStatus?.copy(
grants = grants,
categories = response.categories,
vendors = response.vendors,
legIntCategories = response.legIntCategories,
specialFeatures = response.specialFeatures
)
return@check campaignManager.gdprConsentStatus!!
} catch (error: Throwable) {
throw error
}
}

override fun deleteCustomConsentToServ(customConsentReq: CustomConsentReq, env: Env): Either<GdprCS> = check {
override fun deleteCustomConsentToServ(
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): Either<GdprCS> = check {
if (connectionManager.isConnected.not()) throw NoInternetConnectionException()

networkClient.deleteCustomConsentTo(customConsentReq, env)
.map {
if (campaignManager.gdprConsentStatus == null) {
genericFail("CustomConsent cannot be executed. Consent is missing!!!")
}

val categories: List<String> = (it.content.get("categories") as JSONArray).toArrayList()
val vendors: List<String> = (it.content.get("vendors") as JSONArray).toArrayList()
val legIntCategories: List<String> = (it.content.get("legIntCategories") as JSONArray).toArrayList()
val specialFeatures: List<String> = (it.content.get("specialFeatures") as JSONArray).toArrayList()

val grantsString: String = (it.content.get("grants") as JSONObject).toString()
val grants = JsonConverter.converter.decodeFromString<Map<String, GDPRPurposeGrants>>(grantsString)
campaignManager.gdprConsentStatus = campaignManager.gdprConsentStatus?.copy(
grants = grants,
categories = categories,
vendors = vendors,
legIntCategories = legIntCategories,
specialFeatures = specialFeatures
)
try {
val response = networkClient.deleteCustomConsentTo(
consentUUID = consentUUID,
propertyId = propertyId,
vendors = vendors,
categories = categories,
legIntCategories = legIntCategories
)
if (campaignManager.gdprConsentStatus == null) {
throw IllegalStateException("CustomConsent cannot be executed. Consent is missing!!!")
}
campaignManager.gdprConsentStatus!!
val grants = response.grants.map {
e -> e.key to GDPRPurposeGrants(e.value.vendorGrant,e.value.purposeGrants)
}.toMap()
campaignManager.gdprConsentStatus = campaignManager.gdprConsentStatus?.copy(
grants = grants,
categories = response.categories,
vendors = response.vendors,
legIntCategories = response.legIntCategories,
specialFeatures = response.specialFeatures
)
return@check campaignManager.gdprConsentStatus!!
} catch (error: Throwable) {
throw error
}
}

override fun getMessages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.sourcepoint.cmplibrary.data.network.model.optimized.ConsentStatusPara
import com.sourcepoint.cmplibrary.data.network.model.optimized.MessagesParamReq
import com.sourcepoint.cmplibrary.data.network.model.optimized.choice.ChoiceResp
import com.sourcepoint.cmplibrary.data.network.model.optimized.choice.GetChoiceParamReq
import com.sourcepoint.cmplibrary.data.network.util.Env
import com.sourcepoint.cmplibrary.model.* // ktlint-disable
import com.sourcepoint.mobile_core.models.consents.GDPRConsent
import com.sourcepoint.mobile_core.network.requests.MetaDataRequest
import com.sourcepoint.mobile_core.network.responses.MetaDataResponse

Expand All @@ -19,14 +19,20 @@ const val DEFAULT_TIMEOUT = 10000L
internal interface NetworkClient {

fun sendCustomConsent(
customConsentReq: CustomConsentReq,
env: Env
): Either<CustomConsentResp>
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): GDPRConsent

fun deleteCustomConsentTo(
customConsentReq: CustomConsentReq,
env: Env
): Either<CustomConsentResp>
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): GDPRConsent

fun getMetaData(campaigns: MetaDataRequest.Campaigns): MetaDataResponse

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ import com.sourcepoint.cmplibrary.data.network.util.ResponseManager
import com.sourcepoint.cmplibrary.data.network.util.create
import com.sourcepoint.cmplibrary.exception.ApiRequestPostfix
import com.sourcepoint.cmplibrary.exception.Logger
import com.sourcepoint.cmplibrary.model.CustomConsentReq
import com.sourcepoint.cmplibrary.model.CustomConsentResp
import com.sourcepoint.cmplibrary.model.toBodyRequest
import com.sourcepoint.cmplibrary.model.toBodyRequestDeleteCustomConsentTo
import com.sourcepoint.cmplibrary.util.check
import com.sourcepoint.mobile_core.models.consents.GDPRConsent
import com.sourcepoint.mobile_core.network.SourcepointClient
import com.sourcepoint.mobile_core.network.requests.MetaDataRequest
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -60,55 +57,35 @@ private class NetworkClientImpl(
) : NetworkClient {

override fun sendCustomConsent(
customConsentReq: CustomConsentReq,
env: Env
): Either<CustomConsentResp> = check {
val mediaType = "application/json".toMediaType()
val jsonBody = customConsentReq.toBodyRequest()
val body: RequestBody = RequestBody.create(mediaType, jsonBody)
val url = urlManager.sendCustomConsentUrl(env)

logger.req(
tag = "CustomConsentReq",
url = url.toString(),
body = jsonBody,
type = "POST"
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): GDPRConsent = runBlocking {
coreClient.customConsentGDPR(
consentUUID = consentUUID,
propertyId = propertyId,
vendors = vendors,
categories = categories,
legIntCategories = legIntCategories
)

val request: Request = Request.Builder()
.url(url)
.post(body)
.build()

val response = httpClient.newCall(request).execute()

responseManager.parseCustomConsentRes(response)
}

override fun deleteCustomConsentTo(
customConsentReq: CustomConsentReq,
env: Env
): Either<CustomConsentResp> = check {
val mediaType = "application/json".toMediaType()
val jsonBody = customConsentReq.toBodyRequestDeleteCustomConsentTo()
val body: RequestBody = RequestBody.create(mediaType, jsonBody)
val url = urlManager.deleteCustomConsentToUrl(env.host, customConsentReq)

logger.req(
tag = "DeleteCustomConsentReq",
url = url.toString(),
body = jsonBody,
type = "DELETE"
consentUUID: String,
propertyId: Int,
vendors: List<String>,
categories: List<String>,
legIntCategories: List<String>
): GDPRConsent = runBlocking {
coreClient.deleteCustomConsentGDPR(
consentUUID = consentUUID,
propertyId = propertyId,
vendors = vendors,
categories = categories,
legIntCategories = legIntCategories
)

val request: Request = Request.Builder()
.url(url)
.delete(body)
.build()

val response = httpClient.newCall(request).execute()

responseManager.parseCustomConsentRes(response)
}

override fun getMetaData(campaigns: MetaDataRequest.Campaigns) = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import com.sourcepoint.cmplibrary.core.Either
import com.sourcepoint.cmplibrary.core.layout.model.NativeMessageDto
import com.sourcepoint.cmplibrary.data.network.model.optimized.* // ktlint-disable
import com.sourcepoint.cmplibrary.data.network.model.optimized.choice.ChoiceResp
import com.sourcepoint.cmplibrary.exception.CampaignType
import com.sourcepoint.cmplibrary.model.* // ktlint-disable
import com.sourcepoint.cmplibrary.model.ConsentResp

/**
* Component used to convert the response body of the message call to its DTO
Expand All @@ -17,12 +15,8 @@ internal interface JsonConverter {
* @return [Either] object contain either a DTO or an [Throwable]
*/

fun toConsentResp(body: String, campaignType: CampaignType): Either<ConsentResp>

fun toConsentAction(body: String): Either<ConsentActionImpl>

fun toCustomConsentResp(body: String): Either<CustomConsentResp>

fun toNativeMessageDto(body: String): Either<NativeMessageDto>

fun toNativeMessageRespK(body: String): Either<NativeMessageRespK>
Expand Down
Loading