From 9445965f39926ffcb377a30d396ffe97fdc6e470 Mon Sep 17 00:00:00 2001 From: Fernando Obregon Date: Mon, 18 Nov 2024 11:27:24 +0000 Subject: [PATCH] URL Build Updates --- .../com/hubspot/mobilesdk/HubspotManager.kt | 13 ++++--- .../mobilesdk/config/HubspotConfiguration.kt | 35 +++++++++++++++++-- .../mobilesdk/network/NetworkDependencies.kt | 17 +++++++-- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/hubspot/src/main/java/com/hubspot/mobilesdk/HubspotManager.kt b/hubspot/src/main/java/com/hubspot/mobilesdk/HubspotManager.kt index 0ea3c89..b8d5ca2 100644 --- a/hubspot/src/main/java/com/hubspot/mobilesdk/HubspotManager.kt +++ b/hubspot/src/main/java/com/hubspot/mobilesdk/HubspotManager.kt @@ -9,14 +9,17 @@ package com.hubspot.mobilesdk import android.content.Context import android.net.Uri import com.hubspot.mobilesdk.HubspotWebActivity.Companion.CHAT_FLOW_KEY +import com.hubspot.mobilesdk.config.Environment import com.hubspot.mobilesdk.config.Hublet import com.hubspot.mobilesdk.config.HubspotConfig import com.hubspot.mobilesdk.config.HubspotConfig.Companion.defaultConfigFileName import com.hubspot.mobilesdk.config.HubspotConfigError +import com.hubspot.mobilesdk.config.HubspotEnvironment import com.hubspot.mobilesdk.util.PreferenceHelper import com.hubspot.mobilesdk.errorhandling.NetworkError import com.hubspot.mobilesdk.firebase.PushNotificationChatData import com.hubspot.mobilesdk.model.DeviceTokenParams +import com.hubspot.mobilesdk.network.NetworkDependencies import com.hubspot.mobilesdk.usecases.AddNewDeviceTokenUseCase import com.hubspot.mobilesdk.usecases.DeleteDeviceTokenUseCase import kotlinx.coroutines.Dispatchers @@ -83,7 +86,9 @@ class HubspotManager private constructor(private val context: Context) { .bufferedReader() .use { it.readText() } val json = Json.decodeFromString(jsonString) - hubspotConfig = HubspotConfig(json.environment, json.hublet, json.portalId, json.defaultChatFlow) + val config = HubspotConfig(json.environment, json.hublet, json.portalId, json.defaultChatFlow) + hubspotConfig = config + NetworkDependencies.configure(config) } /** @@ -108,16 +113,16 @@ class HubspotManager private constructor(private val context: Context) { fun chatURL(chatFlow: String? = null, pushData: PushNotificationChatData? = null): String { val hublet = hubspotConfig?.hublet?.let { Hublet(it) } ?: throw HubspotConfigError.MissingHubletID val portalId = hubspotConfig?.portalId?.let { it } ?: throw HubspotConfigError.MissingPortalID - val environment = hubspotConfig?.environment?.let { it } ?: throw HubspotConfigError.MissingEnvironment + val environment = hubspotConfig?.environment?.let { Environment(it) } ?: throw HubspotConfigError.MissingEnvironment val defaultChatFlow = hubspotConfig?.defaultChatFlow val components = Uri.Builder() .scheme("https") - .authority("${hublet.appsSubDomain}.hubspot.com") + .authority("${hublet.appsSubDomain}.hubspot${environment.chatURLSuffix}.com") .path("/conversations-visitor-embed") .appendQueryParameter("portalId", pushData?.portalId ?: portalId) .appendQueryParameter("hublet", hublet.id) - .appendQueryParameter("env", environment) + .appendQueryParameter("env", environment.environment.value) .appendQueryParameter("email", hubspotPref.email) .appendQueryParameter("identificationToken", hubspotPref.token) .build() diff --git a/hubspot/src/main/java/com/hubspot/mobilesdk/config/HubspotConfiguration.kt b/hubspot/src/main/java/com/hubspot/mobilesdk/config/HubspotConfiguration.kt index 6fd0d0f..85e788b 100644 --- a/hubspot/src/main/java/com/hubspot/mobilesdk/config/HubspotConfiguration.kt +++ b/hubspot/src/main/java/com/hubspot/mobilesdk/config/HubspotConfiguration.kt @@ -14,10 +14,30 @@ import java.util.Locale /** * HubspotEnvironment class has either QA or PRODUCTION variables */ -sealed class HubspotEnvironment(open var env: String) { - object QA : HubspotEnvironment("qa") +internal enum class HubspotEnvironment(val value: String) { + QA("qa"), + PRODUCTION("prod") +} + +internal data class Environment(private val env: String) { + val environment: HubspotEnvironment + get() { + return if (this.env == HubspotEnvironment.QA.value) { + HubspotEnvironment.QA + } else { + HubspotEnvironment.PRODUCTION + } + } + + val chatURLSuffix: String + get() { + return if (this.environment == HubspotEnvironment.QA) { + HubspotEnvironment.QA.value + } else { + "" + } - object PRODUCTION : HubspotEnvironment("prod") + } } /** @@ -33,6 +53,15 @@ internal data class Hublet(val id: String) { "app-$id" } } + + val apiSubDomain: String + get() { + return if (id.lowercase() == defaultUS) { + "api" + } else { + "api-$id" + } + } } /** diff --git a/hubspot/src/main/java/com/hubspot/mobilesdk/network/NetworkDependencies.kt b/hubspot/src/main/java/com/hubspot/mobilesdk/network/NetworkDependencies.kt index 0191b6f..eb244fe 100644 --- a/hubspot/src/main/java/com/hubspot/mobilesdk/network/NetworkDependencies.kt +++ b/hubspot/src/main/java/com/hubspot/mobilesdk/network/NetworkDependencies.kt @@ -6,7 +6,12 @@ ************************************************/ package com.hubspot.mobilesdk.network +import android.net.Uri import com.hubspot.mobilesdk.BuildConfig +import com.hubspot.mobilesdk.config.Environment +import com.hubspot.mobilesdk.config.Hublet +import com.hubspot.mobilesdk.config.HubspotConfig +import com.hubspot.mobilesdk.config.HubspotConfigError import com.hubspot.mobilesdk.metadata.HubspotApi import com.squareup.moshi.Moshi import okhttp3.OkHttpClient @@ -19,14 +24,14 @@ import retrofit2.converter.moshi.MoshiConverterFactory */ internal object NetworkDependencies { - private const val BASE_URL = "https://api.hubapi.com/livechat-public/v1/mobile-sdk/" + private var baseUrl = "https://api.hubapi.com/livechat-public/v1/mobile-sdk/" private var hubspotApi: HubspotApi? = null fun getHubspotApi(): HubspotApi { if (hubspotApi == null) { synchronized(this) { hubspotApi = Retrofit.Builder() - .baseUrl(BASE_URL) + .baseUrl(baseUrl) .client(createOkHttpClient()) .addConverterFactory(createMoshiConverterFactory(createMoshi())) .build() @@ -36,6 +41,14 @@ internal object NetworkDependencies { return hubspotApi!! } + fun configure(config: HubspotConfig) { + val hublet = Hublet(config.hublet) + val environment = Environment(config.environment) + val configuredUrl = "https://${hublet.apiSubDomain}.hubapi${environment.chatURLSuffix}.com/livechat-public/v1/mobile-sdk/" + + baseUrl = configuredUrl + } + private fun createMoshiConverterFactory(moshi: Moshi): MoshiConverterFactory { return MoshiConverterFactory.create(moshi) }