Skip to content

Commit a5a5d85

Browse files
authored
DOCSP-39166: Kotlin: Change baseURL during runtime (#3245)
## Pull Request Info Jira ticket: https://jira.mongodb.org/browse/DOCSP-39166 - [Connect to Atlas App Services - Kotlin SDK](https://preview-mongodbcbullinger.gatsbyjs.io/realm/docsp-39166-kotlin-baseurl/sdk/kotlin/app-services/connect-to-app-services-backend/#connect-to-a-specific-server): New "Connect to a Specific Server" section with baseURL info. New "Connect to a Different Server During Runtime" subsection with info about the experimental API ### Reminder Checklist Before merging your PR, make sure to check a few things. - [x] Did you tag pages appropriately? - genre - meta.keywords - meta.description - [x] Describe your PR's changes in the Release Notes section - [x] Create a Jira ticket for related docs-app-services work, if any ### Release Notes - **Kotlin** SDK - Connect to Atlas/Connect to App Services: New "Connect to a Specific Server" section with baseUrl info. New "Connect to a Different Server During Runtime" subsection with info about the experimental API. ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
1 parent ee4b327 commit a5a5d85

File tree

9 files changed

+165
-50
lines changed

9 files changed

+165
-50
lines changed

examples/kotlin/settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pluginManagement {
99
dependencyResolutionManagement {
1010
versionCatalogs {
1111
create("libs") {
12-
version("realm", "1.14.0")
12+
version("realm", "1.16.0")
1313
version("kotlinx-coroutines", "1.7.0")
1414
version("kotlinx-serialization", "1.5.0")
1515
library("realm-plugin", "io.realm.kotlin", "gradle-plugin").versionRef("realm")

examples/kotlin/shared/build.gradle.kts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ kotlin {
1212
iosX64()
1313
iosArm64()
1414
jvm()
15-
15+
1616
sourceSets {
1717
val commonMain by getting {
1818
dependencies {
@@ -24,33 +24,31 @@ kotlin {
2424
api("co.touchlab:kermit:0.1.8")
2525
}
2626
}
27-
sourceSets["commonMain"].kotlin.setSrcDirs(listOf("src/commonMain/kotlin"))
2827
val commonTest by getting {
2928
dependencies {
3029
implementation(libs.kotlinx.coroutines.test) // required to use coroutines in test suite
3130
implementation(kotlin("test-common"))
3231
implementation(kotlin("test-annotations-common"))
3332
implementation(kotlin("test-junit"))
33+
implementation("org.jetbrains.kotlin:kotlin-stdlib")
34+
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
3435
implementation("com.google.android.gms:play-services-auth:20.7.0")
3536
implementation("com.google.android.gms:play-services-base:18.2.0")
3637
}
3738
}
38-
sourceSets["commonTest"].kotlin.setSrcDirs(listOf("src/commonTest/kotlin"))
3939
val androidMain by getting {
4040
dependencies {
4141
implementation(libs.kotlinx.coroutines.android)
4242
implementation("com.google.android.gms:play-services-auth:20.7.0")
4343
implementation("com.google.android.gms:play-services-base:18.2.0")
4444
}
4545
}
46-
sourceSets["androidMain"].kotlin.setSrcDirs(listOf("src/androidMain/kotlin"))
4746
val androidTest by getting {
4847
dependencies {
4948
implementation(kotlin("test-junit"))
5049
implementation("junit:junit:4.13.2")
5150
}
5251
}
53-
sourceSets["androidTest"].kotlin.setSrcDirs(listOf("src/androidTest/kotlin"))
5452
val iosX64Main by getting
5553
val iosArm64Main by getting
5654
val iosMain by creating {
@@ -67,7 +65,7 @@ kotlin {
6765
}
6866
jvm().compilations["main"].defaultSourceSet {
6967
dependencies {
70-
implementation(kotlin("stdlib-jdk8"))
68+
implementation("org.jetbrains.kotlin:kotlin-stdlib")
7169
implementation(libs.kotlinx.coroutines)
7270
implementation(libs.kotlinx.coroutines.test)
7371
}

examples/kotlin/shared/src/commonTest/kotlin/com/mongodb/realm/realmkmmapp/AppClientTest.kt

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.realm.kotlin.log.RealmLogger
77
import io.realm.kotlin.mongodb.App
88
import io.realm.kotlin.mongodb.AppConfiguration
99
import io.realm.kotlin.mongodb.Credentials
10+
import io.realm.kotlin.mongodb.annotations.ExperimentalEdgeServerApi
1011
import io.realm.kotlin.mongodb.exceptions.ConnectionException
1112
import io.realm.kotlin.mongodb.exceptions.InvalidCredentialsException
1213
import io.realm.kotlin.mongodb.exceptions.ServiceException
@@ -15,12 +16,12 @@ import kotlin.test.Ignore
1516
import kotlin.test.Test
1617
import kotlin.test.assertEquals
1718
import kotlin.test.assertFailsWith
19+
import kotlin.test.assertNotNull
1820
import kotlin.test.assertTrue
1921
import kotlin.time.Duration.Companion.minutes
2022
import kotlin.time.Duration.Companion.seconds
2123

22-
23-
class AppClientTest: RealmTest() {
24+
class AppClientTest : RealmTest() {
2425
@Test
2526
fun initializeAndCloseAppClientTest() {
2627
// :snippet-start: initialize-app-client
@@ -51,15 +52,53 @@ class AppClientTest: RealmTest() {
5152
}
5253

5354
@Test
54-
fun changeBaseUrl() {
55-
val defaultBaseUrl = "https://realm.mongodb.com"
56-
val newBaseUrl = "https://example.org/"
57-
val config = AppConfiguration.create(YOUR_APP_ID)
58-
assertEquals(config.baseUrl, defaultBaseUrl)
59-
val configWithNewBaseUrl = AppConfiguration.Builder(YOUR_APP_ID)
60-
.baseUrl(newBaseUrl)
55+
fun customBaseUrl() {
56+
val defaultBaseUrl = "https://services.cloud.mongodb.com"
57+
val newBaseUrl = "https://example.com"
58+
val defaultConfig = AppConfiguration.create(YOUR_APP_ID)
59+
assertEquals(defaultConfig.baseUrl, defaultBaseUrl)
60+
// :snippet-start: custom-base-url
61+
// Specify a baseUrl to connect to instead of the default
62+
val config = AppConfiguration.Builder(YOUR_APP_ID)
63+
.baseUrl("https://example.com")
6164
.build()
62-
assertEquals(configWithNewBaseUrl.baseUrl, newBaseUrl)
65+
// :snippet-end:
66+
assertEquals(config.baseUrl, newBaseUrl)
67+
}
68+
69+
// :snippet-start: experimental-opt-in
70+
// Opt in to the experimental Edge Server API
71+
@OptIn(ExperimentalEdgeServerApi::class)
72+
// :snippet-end:
73+
@Ignore
74+
// TODO: Update when we get Edge Server running in a CI and can write automated tests for full flow
75+
// Ignored until we can test in CI (was tested locally and succeeded)
76+
fun changeBaseUrl() {
77+
runBlocking {
78+
val defaultBaseUrl = "https://services.cloud.mongodb.com"
79+
val customBaseUrl = "http://localhost:80"
80+
// :snippet-start: change-base-url
81+
// Specify a custom baseUrl to connect to.
82+
// In this case, an Edge Server instance running on the device:
83+
val config = AppConfiguration.Builder(EDGE_SERVER_APP_ID)
84+
.baseUrl("http://localhost:80")
85+
.build()
86+
val app = App.create(config)
87+
88+
// ... log in a user and use the app ...
89+
// :remove-start:
90+
assertEquals(app.baseUrl, customBaseUrl)
91+
app.login(Credentials.anonymous())
92+
assertNotNull(app.currentUser)
93+
// :remove-end:
94+
95+
// Later, change the baseUrl.
96+
// In this case, pass `null` to reset to default:
97+
// https://services.cloud.mongodb.com
98+
app.updateBaseUrl(null)
99+
// :snippet-end:
100+
assertEquals(app.baseUrl, defaultBaseUrl)
101+
}
63102
}
64103

65104
@Test
@@ -131,8 +170,8 @@ class AppClientTest: RealmTest() {
131170
@Test
132171
fun setCustomHttpHeadersTest() {
133172
val config1 = AppConfiguration.Builder(YOUR_APP_ID)
134-
.appName("my-app-name")
135-
.build()
173+
.appName("my-app-name")
174+
.build()
136175
val config2 =
137176
// :snippet-start: set-custom-http-headers
138177
AppConfiguration.Builder(YOUR_APP_ID)
@@ -160,8 +199,7 @@ class AppClientTest: RealmTest() {
160199
message: String?,
161200
vararg args: Any?,
162201
) {
163-
if (level == LogLevel.DEBUG && message!!.contains("-> X-MyApp-Version: 1.0.0") && message.contains("MyApp-Authorization"))
164-
{
202+
if (level == LogLevel.DEBUG && message!!.contains("-> X-MyApp-Version: 1.0.0") && message.contains("MyApp-Authorization")) {
165203
channel.trySend(true)
166204
}
167205
}
@@ -204,13 +242,15 @@ class AppClientTest: RealmTest() {
204242
// "Invalid username or password. Please try again.", Toast.LENGTH_LONG).show()
205243
// :uncomment-end:
206244
}
245+
207246
is ConnectionException -> {
208247
Log.e("Failed to login due to a connection error: ${ex.message}")
209248
// :uncomment-start:
210249
// Toast.makeText(baseContext,
211250
// "Login failed due to a connection error. Check your network connection and try again.", Toast.LENGTH_LONG).show()
212251
// :uncomment-end:
213252
}
253+
214254
else -> {
215255
Log.e("Failed to login: ${ex.message}")
216256
// generic error message for niche and unknown fail cases

examples/kotlin/shared/src/commonTest/kotlin/com/mongodb/realm/realmkmmapp/AuthenticationTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,9 @@ class AuthenticationTest: RealmTest() {
471471
app.close()
472472
}
473473

474-
/*
475-
** Tests for Multi-User Applications page **
476-
*/
474+
/*
475+
** Tests for Multi-User Applications page **
476+
*/
477477

478478
private val app = App.create(YOUR_APP_ID)
479479
val joeEmail = getRandomEmail()

examples/kotlin/shared/src/commonTest/kotlin/com/mongodb/realm/realmkmmapp/RealmTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ open class RealmTest {
4343

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

4849
val TESTER_APP_ID: String = "example-testers-kvjdy"
@@ -71,4 +72,4 @@ open class RealmTest {
7172
//Napier.base(DebugAntilog()) // initialize napier
7273
// log is getting really spammy -- probably because napier is initialized multiple times. Testing commenting this out.
7374
}
74-
}
75+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Specify a custom baseUrl to connect to.
2+
// In this case, an Edge Server instance running on the device:
3+
val config = AppConfiguration.Builder(EDGE_SERVER_APP_ID)
4+
.baseUrl("http://localhost:80")
5+
.build()
6+
val app = App.create(config)
7+
8+
// ... log in a user and use the app ...
9+
10+
// Later, change the baseUrl.
11+
// In this case, pass `null` to reset to default:
12+
// https://services.cloud.mongodb.com
13+
app.updateBaseUrl(null)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Specify a baseUrl to connect to instead of the default
2+
val config = AppConfiguration.Builder(YOUR_APP_ID)
3+
.baseUrl("https://example.com")
4+
.build()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Opt in to the experimental Edge Server API
2+
@OptIn(ExperimentalEdgeServerApi::class)

0 commit comments

Comments
 (0)