Skip to content

Commit

Permalink
Merge pull request #31 from FeernandoOFF/url-building
Browse files Browse the repository at this point in the history
URL Build Updates
  • Loading branch information
jasonconnery authored Nov 20, 2024
2 parents c44ae35 + 9445965 commit 3429db0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
13 changes: 9 additions & 4 deletions hubspot/src/main/java/com/hubspot/mobilesdk/HubspotManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,7 +86,9 @@ class HubspotManager private constructor(private val context: Context) {
.bufferedReader()
.use { it.readText() }
val json = Json.decodeFromString<HubspotConfig>(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)
}

/**
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

/**
Expand All @@ -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"
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
}
Expand Down

0 comments on commit 3429db0

Please sign in to comment.