-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add RoomServiceFetcher to join different regions
- Loading branch information
Showing
5 changed files
with
158 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
app/src/main/java/io/agora/flat/data/RoomServiceFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.agora.flat.data | ||
|
||
import io.agora.flat.di.NetworkModule | ||
import io.agora.flat.http.api.RoomService | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
/** | ||
* Fetch room service by uuid | ||
* | ||
* For joining rooms between different regions | ||
*/ | ||
@Singleton | ||
class RoomServiceFetcher @Inject constructor( | ||
@NetworkModule.NormalOkHttpClient private val client: OkHttpClient, | ||
private val appEnv: AppEnv | ||
) { | ||
companion object { | ||
val regions = listOf( | ||
"CN", | ||
"SG", | ||
) | ||
|
||
val codeMap = mapOf( | ||
"1" to "CN", | ||
"2" to "SG", | ||
) | ||
|
||
fun fetchEnv(uuid: String, currentEnv: String): String { | ||
var (region, envType) = currentEnv.split("_") | ||
|
||
// short invite code | ||
if (uuid.length == 11) { | ||
val code = uuid[0] + "" | ||
codeMap[code]?.let { region = it } | ||
} | ||
|
||
// long uuid | ||
if (uuid.length > 15) { | ||
val firstTwo = uuid.substring(0, 2) | ||
regions.find { it == firstTwo.uppercase() }?.let { region = it } | ||
} | ||
|
||
return "${region}_$envType".lowercase() | ||
} | ||
} | ||
|
||
private val cache = mutableMapOf<String, RoomService>() | ||
|
||
fun fetch(uuid: String): RoomService { | ||
val env = fetchEnv(uuid, appEnv.getEnv()) | ||
val envServiceUrl = appEnv.getEnvServiceUrl(env) | ||
|
||
return cache[env] ?: createRoomService(envServiceUrl).also { | ||
cache[env] = it | ||
} | ||
} | ||
|
||
private fun createRoomService(serviceUrl: String): RoomService { | ||
return Retrofit.Builder() | ||
.baseUrl(serviceUrl) | ||
.client(client) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
.create(RoomService::class.java) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
app/src/test/java/io/agora/flat/data/RoomServiceFetcherTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.agora.flat.data | ||
|
||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class RoomServiceFetcherTest { | ||
|
||
@Before | ||
fun setUp() { | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
} | ||
|
||
data class FetchEnvCase( | ||
val uuid: String, | ||
val env: String, | ||
val expected: String, | ||
) | ||
|
||
@Test | ||
fun test_fetch_env() { | ||
val cases = listOf( | ||
// sg invite code | ||
FetchEnvCase("22884465997", "cn_dev", "sg_dev"), | ||
|
||
// cn invite code | ||
FetchEnvCase("12122312333", "sg_prod", "cn_prod"), | ||
|
||
// old invite code | ||
FetchEnvCase("2231213331", "cn_prod", "cn_prod"), | ||
|
||
// sg uuid | ||
FetchEnvCase("SG-73e84969-1850-44db-ae01-7e46192cb01c", "cn_prod", "sg_prod"), | ||
|
||
// cn uuid | ||
FetchEnvCase("CN-73e84969-1850-44db-ae01-7e46192cb01c", "sg_dev", "cn_dev") | ||
) | ||
|
||
cases.forEach { it -> | ||
assertEquals(it.expected, RoomServiceFetcher.fetchEnv(it.uuid, it.env)) | ||
} | ||
} | ||
} |