Skip to content

Commit

Permalink
add load api_key logic (#208)
Browse files Browse the repository at this point in the history
* add load api_key logic

* add test
update changelog

* Update CHANGELOG.md
  • Loading branch information
wasnot authored Jan 23, 2023
1 parent 1079f77 commit c9271de
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| モジュール/プラグイン名 | Description | 最新のバージョン |
| :-- | :-- | :-- |
| core | イベントトラッキング機能を提供します。 | 2.20.0 |
| core | イベントトラッキング機能を提供します。 | 2.21.0 |
| inappmessaging | アプリ内メッセージ機能を提供します。 | 2.14.0 |
| notifications | プッシュ通知の受信および効果測定機能を提供します。 | 2.9.1 |
| variables | 設定値配信機能を提供します。 | 2.3.1 |
Expand All @@ -18,6 +18,13 @@
- 解消するにはAndroid Gradle Pluginを7.0.0以降にする必要があります。
- Kotlin 1.7.20 で一部の操作ログが送信されない不具合を修正しました。

# Releases - xxxx.xx.xx

### Core 2.21.0
** 🎉 FEATURE**
- KARTEプロジェクトのAPIキーをSDKに設定できるようになりました。
- Inboxモジュールを使用する場合のみ設定が必要です。

# Releases - 2022.09.09

### Notifications 2.9.1
Expand Down
29 changes: 28 additions & 1 deletion core/src/main/java/io/karte/android/core/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import io.karte.android.core.library.LibraryConfig
*/
open class Config protected constructor(
appKey: String,
apiKey: String,
val baseUrl: String,
internal val logCollectionUrl: String,
val isDryRun: Boolean,
Expand All @@ -75,11 +76,22 @@ open class Config protected constructor(

internal val isValidAppKey get() = appKey.length == 32

/**
* @property[apiKey] APIキーの取得・設定を行います。
*
* 設定ファイルから自動でロードされるAPIキー以外を利用したい場合にのみ設定します。
*/
var apiKey: String = apiKey
internal set

/** [Config]クラスの生成を行うためのクラスです。 */
open class Builder {
/**[Config.appKey]を変更します。*/
var appKey: String = "" @JvmSynthetic set

/**[Config.apiKey]を変更します。*/
var apiKey: String = "" @JvmSynthetic set

/**[Config.baseUrl]を変更します。*/
var baseUrl: String = "https://api.karte.io/v0/native" @JvmSynthetic set

Expand Down Expand Up @@ -128,6 +140,7 @@ open class Config protected constructor(
/**[Config]クラスのインスタンスを生成します。*/
open fun build(): Config = Config(
appKey,
apiKey,
baseUrl,
logCollectionUrl,
isDryRun,
Expand All @@ -150,7 +163,10 @@ open class Config protected constructor(

internal fun withAppKey(context: Context, config: Config?): Config {
if (config != null && config.appKey.isNotEmpty()) return config
return (config ?: build()).apply { this.appKey = appKeyFromResource(context) }
return (config ?: build()).apply {
this.appKey = appKeyFromResource(context)
this.apiKey = apiKeyFromResource(context)
}
}

private fun appKeyFromResource(context: Context): String {
Expand All @@ -167,5 +183,16 @@ open class Config protected constructor(
res.getString(id)
}
}

private fun apiKeyFromResource(context: Context): String {
val res = context.resources
val pkg = res.getResourcePackageName(R.id.karte_resources)
val id = res.getIdentifier("karte_api_key", "string", pkg)
return if (id == 0) {
""
} else {
res.getString(id)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import io.karte.android.core.library.LibraryConfig
class ExperimentalConfig private constructor(
val operationMode: OperationMode,
appKey: String,
apiKey: String,
baseUrl: String,
logCollectionUrl: String,
isDryRun: Boolean,
Expand All @@ -39,6 +40,7 @@ class ExperimentalConfig private constructor(
libraryConfigs: List<LibraryConfig>
) : Config(
appKey,
apiKey,
baseUrl,
logCollectionUrl,
isDryRun,
Expand All @@ -60,6 +62,7 @@ class ExperimentalConfig private constructor(
override fun build(): ExperimentalConfig = ExperimentalConfig(
operationMode,
appKey,
apiKey,
baseUrl,
logCollectionUrl,
isDryRun,
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/io/karte/android/integration/SetupTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ class SetupTest {
.isEqualTo(expected)
}

@Test
fun apiKey_リソースまたはconfig経由で設定されること() {
val expected = if (pattern == SetupPattern.FROM_RESOURCE) {
setup()
application.getString(R.string.karte_api_key)
} else {
setup(Config.Builder().apply { apiKey = "test_api_key" })
"test_api_key"
}
assertThat(KarteApp.self.config.apiKey).isEqualTo(expected)
}

@Test
fun baseUrl_指定したendpointに対してリクエストが行われること() {
setup()
Expand Down
1 change: 1 addition & 0 deletions core/src/test/res/values/karte.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="karte_app_key">sampleappkey_1234567890123456789</string>
<string name="karte_api_key">sampleapikey_1234567890123456789</string>
</resources>
2 changes: 1 addition & 1 deletion core/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.20.0
2.21.0

0 comments on commit c9271de

Please sign in to comment.