Skip to content

Commit ad15cdd

Browse files
committed
Move empty API key handling to core SDK
1 parent 7f3e688 commit ad15cdd

11 files changed

Lines changed: 45 additions & 41 deletions

File tree

posthog-android/src/main/java/com/posthog/android/PostHogAndroid.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public class PostHogAndroid private constructor() {
4848
config: T,
4949
) {
5050
synchronized(lock) {
51-
if (config.apiKey.isEmpty()) {
52-
PostHog.setup(config)
53-
return
54-
}
55-
5651
setAndroidConfig(context.appContext(), config)
5752

5853
PostHog.setup(config)
@@ -75,10 +70,6 @@ public class PostHogAndroid private constructor() {
7570
context: Context,
7671
config: T,
7772
): PostHogInterface {
78-
if (config.apiKey.isEmpty()) {
79-
return PostHog.with(config)
80-
}
81-
8273
setAndroidConfig(context.appContext(), config)
8374
return PostHog.with(config)
8475
}

posthog-android/src/main/java/com/posthog/android/PostHogAndroidConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.posthog.internal.PostHogQueue
1616
public open class PostHogAndroidConfig
1717
@JvmOverloads
1818
constructor(
19-
apiKey: String?,
19+
apiKey: String,
2020
host: String = DEFAULT_HOST,
2121
public var captureApplicationLifecycleEvents: Boolean = true,
2222
public var captureDeepLinks: Boolean = true,

posthog-android/src/test/java/com/posthog/android/PostHogAndroidConfigJavaTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
public class PostHogAndroidConfigJavaTest {
88
@Test
9-
public void nullApiKeyFromJavaDefaultsToEmptyString() {
10-
PostHogAndroidConfig config = new PostHogAndroidConfig(null);
9+
public void apiKeyFromJavaIsTrimmed() {
10+
PostHogAndroidConfig config = new PostHogAndroidConfig(" \n" + UtilsKt.API_KEY + "\t ");
1111

12-
assertEquals("", config.getApiKey());
12+
assertEquals(UtilsKt.API_KEY, config.getApiKey());
1313
}
1414
}

posthog-android/src/test/java/com/posthog/android/PostHogAndroidConfigTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ internal class PostHogAndroidConfigTest {
1313
}
1414

1515
@Test
16-
fun `defaults null api key to empty string`() {
17-
val config = PostHogAndroidConfig(null)
16+
fun `trims whitespace-sensitive config values`() {
17+
val config = PostHogAndroidConfig(" \n$API_KEY\t ")
1818

19-
assertEquals("", config.apiKey)
19+
assertEquals(API_KEY, config.apiKey)
2020
}
2121

2222
@Test

posthog-android/src/test/java/com/posthog/android/PostHogAndroidTest.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.posthog.android.internal.PostHogAndroidNetworkStatus
1010
import com.posthog.android.internal.PostHogAppInstallIntegration
1111
import com.posthog.android.internal.PostHogLifecycleObserverIntegration
1212
import com.posthog.android.internal.PostHogSharedPreferences
13+
import com.posthog.internal.PostHogLogger
1314
import org.junit.Rule
1415
import org.junit.rules.TemporaryFolder
1516
import org.junit.runner.RunWith
@@ -40,23 +41,18 @@ internal class PostHogAndroidTest {
4041
}
4142

4243
@Test
43-
fun `setup no-ops for null api key`() {
44-
val config = PostHogAndroidConfig(null)
45-
46-
PostHogAndroid.setup(context, config)
47-
48-
assertNull(config.cachePreferences)
49-
assertTrue(config.integrations.isEmpty())
50-
}
51-
52-
@Test
53-
fun `setup no-ops for empty trimmed api key`() {
44+
fun `setup configures Android then core disables SDK for empty trimmed api key`() {
5445
val config = PostHogAndroidConfig(" \n\t ")
46+
val logger = TestLogger()
47+
config.logger = logger
48+
49+
mockContextAppStart(context, tmpDir)
5550

5651
PostHogAndroid.setup(context, config)
5752

58-
assertNull(config.cachePreferences)
59-
assertTrue(config.integrations.isEmpty())
53+
assertTrue(config.cachePreferences is PostHogSharedPreferences)
54+
assertTrue(config.integrations.isNotEmpty())
55+
assertTrue(logger.messages.any { it.contains("PostHog SDK is disabled because the API key is required") })
6056
}
6157

6258
@Test
@@ -237,4 +233,14 @@ internal class PostHogAndroidTest {
237233

238234
postHog.close()
239235
}
236+
237+
private class TestLogger : PostHogLogger {
238+
val messages = mutableListOf<String>()
239+
240+
override fun log(message: String) {
241+
messages.add(message)
242+
}
243+
244+
override fun isEnabled(): Boolean = true
245+
}
240246
}

posthog/src/main/java/com/posthog/PostHog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class PostHog private constructor(
100100
if (config.logger is PostHogNoOpLogger) PostHogPrintLogger(config) else config.logger
101101

102102
if (config.apiKey.isEmpty()) {
103-
config.logger.log("apiKey is empty after trimming whitespace; check your project API key")
103+
config.logger.log("PostHog SDK is disabled because the API key is required and was empty after trimming whitespace.")
104104
return
105105
}
106106

posthog/src/main/java/com/posthog/PostHogConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public open class PostHogConfig(
3131
/**
3232
* The PostHog API Key
3333
*/
34-
apiKey: String?,
34+
apiKey: String,
3535
/**
3636
* The PostHog Host
3737
* Defaults to https://us.i.posthog.com
@@ -328,7 +328,7 @@ public open class PostHogConfig(
328328
/**
329329
* The PostHog API Key
330330
*/
331-
public val apiKey: String = apiKey?.trim().orEmpty()
331+
public val apiKey: String = apiKey.trim()
332332

333333
/**
334334
* The PostHog Host

posthog/src/main/java/com/posthog/PostHogStateless.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public open class PostHogStateless protected constructor(
5454
if (config.logger is PostHogNoOpLogger) PostHogPrintLogger(config) else config.logger
5555

5656
if (config.apiKey.isEmpty()) {
57-
config.logger.log("apiKey is empty after trimming whitespace; check your project API key")
57+
config.logger.log("PostHog SDK is disabled because the API key is required and was empty after trimming whitespace.")
5858
return
5959
}
6060

posthog/src/test/java/com/posthog/PostHogConfigTest.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ internal class PostHogConfigTest {
2323
assertEquals("https://eu.i.posthog.com/", config.host)
2424
}
2525

26-
@Test
27-
fun `defaults null api key to empty string`() {
28-
val config = PostHogConfig(null, "https://api.posthog.com")
29-
30-
assertEquals("", config.apiKey)
31-
}
32-
3326
@Test
3427
fun `defaults a blank host after trimming whitespace`() {
3528
val config = PostHogConfig(API_KEY, " \n\t ")

posthog/src/test/java/com/posthog/PostHogStatelessTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ internal class PostHogStatelessTest {
245245
sut.setup(config)
246246

247247
assertFalse(sut.isEnabledPublic())
248-
assertTrue(mockLogger.messages.any { it.contains("apiKey is empty after trimming whitespace") })
248+
assertTrue(mockLogger.messages.any { it.contains("PostHog SDK is disabled because the API key is required") })
249249
}
250250

251251
@Test

0 commit comments

Comments
 (0)