Skip to content

Commit

Permalink
Add updateBaseUrl docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed May 4, 2024
1 parent 207bd47 commit 66af0b3
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/kotlin/settings.gradle.kts
Expand Up @@ -9,7 +9,7 @@ pluginManagement {
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
version("realm", "1.14.0")
version("realm", "1.16.0")
version("kotlinx-coroutines", "1.7.0")
version("kotlinx-serialization", "1.5.0")
library("realm-plugin", "io.realm.kotlin", "gradle-plugin").versionRef("realm")
Expand Down
10 changes: 4 additions & 6 deletions examples/kotlin/shared/build.gradle.kts
Expand Up @@ -12,7 +12,7 @@ kotlin {
iosX64()
iosArm64()
jvm()

sourceSets {
val commonMain by getting {
dependencies {
Expand All @@ -24,33 +24,31 @@ kotlin {
api("co.touchlab:kermit:0.1.8")
}
}
sourceSets["commonMain"].kotlin.setSrcDirs(listOf("src/commonMain/kotlin"))
val commonTest by getting {
dependencies {
implementation(libs.kotlinx.coroutines.test) // required to use coroutines in test suite
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation(kotlin("test-junit"))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
implementation("com.google.android.gms:play-services-auth:20.7.0")
implementation("com.google.android.gms:play-services-base:18.2.0")
}
}
sourceSets["commonTest"].kotlin.setSrcDirs(listOf("src/commonTest/kotlin"))
val androidMain by getting {
dependencies {
implementation(libs.kotlinx.coroutines.android)
implementation("com.google.android.gms:play-services-auth:20.7.0")
implementation("com.google.android.gms:play-services-base:18.2.0")
}
}
sourceSets["androidMain"].kotlin.setSrcDirs(listOf("src/androidMain/kotlin"))
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
sourceSets["androidTest"].kotlin.setSrcDirs(listOf("src/androidTest/kotlin"))
val iosX64Main by getting
val iosArm64Main by getting
val iosMain by creating {
Expand All @@ -67,7 +65,7 @@ kotlin {
}
jvm().compilations["main"].defaultSourceSet {
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation(libs.kotlinx.coroutines)
implementation(libs.kotlinx.coroutines.test)
}
Expand Down
Expand Up @@ -7,6 +7,7 @@ import io.realm.kotlin.log.RealmLogger
import io.realm.kotlin.mongodb.App
import io.realm.kotlin.mongodb.AppConfiguration
import io.realm.kotlin.mongodb.Credentials
import io.realm.kotlin.mongodb.annotations.ExperimentalEdgeServerApi
import io.realm.kotlin.mongodb.exceptions.ConnectionException
import io.realm.kotlin.mongodb.exceptions.InvalidCredentialsException
import io.realm.kotlin.mongodb.exceptions.ServiceException
Expand All @@ -15,12 +16,12 @@ import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds


class AppClientTest: RealmTest() {
class AppClientTest : RealmTest() {
@Test
fun initializeAndCloseAppClientTest() {
// :snippet-start: initialize-app-client
Expand Down Expand Up @@ -51,15 +52,52 @@ class AppClientTest: RealmTest() {
}

@Test
fun changeBaseUrl() {
val defaultBaseUrl = "https://realm.mongodb.com"
val newBaseUrl = "https://example.org/"
val config = AppConfiguration.create(YOUR_APP_ID)
assertEquals(config.baseUrl, defaultBaseUrl)
val configWithNewBaseUrl = AppConfiguration.Builder(YOUR_APP_ID)
.baseUrl(newBaseUrl)
fun customBaseUrl() {
val defaultBaseUrl = "https://services.cloud.mongodb.com"
val newBaseUrl = "https://example.com"
val defaultConfig = AppConfiguration.create(YOUR_APP_ID)
assertEquals(defaultConfig.baseUrl, defaultBaseUrl)
// :snippet-start: custom-base-url
// Specify a baseUrl to connect to instead of the default
val config = AppConfiguration.Builder(YOUR_APP_ID)
.baseUrl("https://example.com")
.build()
assertEquals(configWithNewBaseUrl.baseUrl, newBaseUrl)
// :snippet-end:
assertEquals(config.baseUrl, newBaseUrl)
}

// :snippet-start: experimental-opt-in
// Opt in to the experimental Edge Server API
@OptIn(ExperimentalEdgeServerApi::class)
// :snippet-end:
@Ignore
// TODO: Update when we get Edge Server running in a CI and can write automated tests for full flow
// Ignored until we can test in CI (was tested locally and succeeded)
fun changeBaseUrl() {
runBlocking {
val defaultBaseUrl = "https://services.cloud.mongodb.com"
val customBaseUrl = "http://localhost:80"
// :snippet-start: change-base-url
// Specify a custom baseUrl to connect to.
// In this case, an Edge Server instance running on the device.
val config = AppConfiguration.Builder(EDGE_SERVER_APP_ID)
.baseUrl("http://localhost:80")
.build()
val app = App.create(config)

// ... log in a user and use the app ...
// :remove-start:
assertEquals(app.baseUrl, customBaseUrl)
app.login(Credentials.anonymous())
assertNotNull(app.currentUser)
// :remove-end:

// Later, change the baseUrl to the default:
// https://services.cloud.mongodb.com
app.updateBaseUrl(null)
// :snippet-end:
assertEquals(app.baseUrl, defaultBaseUrl)
}
}

@Test
Expand Down Expand Up @@ -131,8 +169,8 @@ class AppClientTest: RealmTest() {
@Test
fun setCustomHttpHeadersTest() {
val config1 = AppConfiguration.Builder(YOUR_APP_ID)
.appName("my-app-name")
.build()
.appName("my-app-name")
.build()
val config2 =
// :snippet-start: set-custom-http-headers
AppConfiguration.Builder(YOUR_APP_ID)
Expand Down Expand Up @@ -160,8 +198,7 @@ class AppClientTest: RealmTest() {
message: String?,
vararg args: Any?,
) {
if (level == LogLevel.DEBUG && message!!.contains("-> X-MyApp-Version: 1.0.0") && message.contains("MyApp-Authorization"))
{
if (level == LogLevel.DEBUG && message!!.contains("-> X-MyApp-Version: 1.0.0") && message.contains("MyApp-Authorization")) {
channel.trySend(true)
}
}
Expand Down Expand Up @@ -204,13 +241,15 @@ class AppClientTest: RealmTest() {
// "Invalid username or password. Please try again.", Toast.LENGTH_LONG).show()
// :uncomment-end:
}

is ConnectionException -> {
Log.e("Failed to login due to a connection error: ${ex.message}")
// :uncomment-start:
// Toast.makeText(baseContext,
// "Login failed due to a connection error. Check your network connection and try again.", Toast.LENGTH_LONG).show()
// :uncomment-end:
}

else -> {
Log.e("Failed to login: ${ex.message}")
// generic error message for niche and unknown fail cases
Expand Down
Expand Up @@ -43,6 +43,7 @@ open class RealmTest {

val SYNCED_REALM_SCHEMA = setOf(Frog::class, Sample::class)
val YOUR_APP_ID: String = "kmm-example-testers-viybt"
val EDGE_SERVER_APP_ID = "sync-edge-server-cskhoow"
val yourAppId = AppConfiguration.Builder(YOUR_APP_ID).syncRootDirectory("tmp/sync/".plus(getRandom())).build()

val TESTER_APP_ID: String = "example-testers-kvjdy"
Expand Down Expand Up @@ -71,4 +72,4 @@ open class RealmTest {
//Napier.base(DebugAntilog()) // initialize napier
// log is getting really spammy -- probably because napier is initialized multiple times. Testing commenting this out.
}
}
}
@@ -0,0 +1,12 @@
// Specify a custom baseUrl to connect to.
// In this case, an Edge Server instance running on the device.
val config = AppConfiguration.Builder(EDGE_SERVER_APP_ID)
.baseUrl("http://localhost:80")
.build()
val app = App.create(config)

// ... log in a user and use the app ...

// Later, change the baseUrl to the default:
// https://services.cloud.mongodb.com
app.updateBaseUrl(null)
@@ -0,0 +1,4 @@
// Specify a baseUrl to connect to instead of the default
val config = AppConfiguration.Builder(YOUR_APP_ID)
.baseUrl("https://example.com")
.build()
@@ -0,0 +1,2 @@
// Opt in to the experimental Edge Server API
@OptIn(ExperimentalEdgeServerApi::class)

0 comments on commit 66af0b3

Please sign in to comment.