Skip to content

Commit

Permalink
Feature/92801 2 (#248)
Browse files Browse the repository at this point in the history
* revert: iam config load from resource

* add dataLocation config

* update changelog
  • Loading branch information
wasnot authored Jan 17, 2024
1 parent efd285f commit df398a6
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 150 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
** 🔨CHANGED**
- KARTEのベースURLの指定可能な値をサブパスを含まないものに変更しました。
- リソースファイルからの初期化時には、リソースのベースURLを確認し読み込むようになりました。
- KARTEプロジェクトのデータロケーション設定を追加しました。

### InAppMessaging 2.18.0
** 🔨CHANGED**
- 接客表示用URLを変更可能にしました。
- 未設定時には、リソースの接客表示用URLを確認し読み込むようになりました。

# Releases - 2023.11.24

Expand Down
4 changes: 4 additions & 0 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class io/karte/android/core/config/Config {
public final fun getApiKey ()Ljava/lang/String;
public final fun getAppKey ()Ljava/lang/String;
public final fun getBaseUrl ()Ljava/lang/String;
public final fun getDataLocation ()Ljava/lang/String;
public final fun getEnabledTrackingAaid ()Z
public final fun getLibraryConfigs ()Ljava/util/List;
public final fun isDryRun ()Z
Expand All @@ -93,10 +94,12 @@ public class io/karte/android/core/config/Config$Builder {
public final fun appKey (Ljava/lang/String;)Lio/karte/android/core/config/Config$Builder;
public final fun baseUrl (Ljava/lang/String;)Lio/karte/android/core/config/Config$Builder;
public fun build ()Lio/karte/android/core/config/Config;
public final fun dataLocation (Ljava/lang/String;)Lio/karte/android/core/config/Config$Builder;
public final fun enabledTrackingAaid (Z)Lio/karte/android/core/config/Config$Builder;
public final fun getApiKey ()Ljava/lang/String;
public final fun getAppKey ()Ljava/lang/String;
public final fun getBaseUrl ()Ljava/lang/String;
public final fun getDataLocation ()Ljava/lang/String;
public final fun getEnabledTrackingAaid ()Z
public final fun getLibraryConfigs ()Ljava/util/List;
public final fun isDryRun ()Z
Expand All @@ -108,6 +111,7 @@ public class io/karte/android/core/config/Config$Builder {
public final synthetic fun setApiKey (Ljava/lang/String;)V
public final synthetic fun setAppKey (Ljava/lang/String;)V
public final synthetic fun setBaseUrl (Ljava/lang/String;)V
public final synthetic fun setDataLocation (Ljava/lang/String;)V
public final synthetic fun setDryRun (Z)V
public final synthetic fun setEnabledTrackingAaid (Z)V
public final synthetic fun setLibraryConfigs (Ljava/util/List;)V
Expand Down
55 changes: 41 additions & 14 deletions core/src/main/java/io/karte/android/core/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,21 @@ open class Config protected constructor(
val enabledTrackingAaid: Boolean,
val libraryConfigs: List<LibraryConfig>
) {
/**
* @property[appKey] アプリケーションキーの取得を行います。
*/
/** アプリケーションキーの取得を行います。 */
var appKey: String = appKey
internal set

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

/**
* @property[apiKey] APIキーの取得を行います。
*/
/** APIキーの取得を行います。 */
var apiKey: String = apiKey
private set

/** 未設定確認用変数 */
private var _baseUrl: String = ""

/**
* @property[baseUrl] ベースURLの取得を行います。
* ベースURLの取得を行います。
* 設定されたURLにサブパスを付与したものを返します。
*/
var baseUrl: String
Expand All @@ -101,6 +97,18 @@ open class Config protected constructor(
return _baseUrl
}

private var _dataLocation: String = ""
/** KARTEプロジェクトのデータロケーションを取得します。 */
var dataLocation: String
private set(value) {
_dataLocation = value
}
get() {
if (_dataLocation.isEmpty())
return "tw"
return _dataLocation
}

init {
this.baseUrl = baseUrl
}
Expand All @@ -109,24 +117,30 @@ open class Config protected constructor(
open class Builder {
/**
* [Config.appKey]を変更します。
* 設定ファイルから自動でロードされるアプリケーションキー以外を利用したい場合にのみ設定します
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します
*/
var appKey: String = "" @JvmSynthetic set

/**
* [Config.apiKey]を変更します。
* 設定ファイルから自動でロードされるAPIキー以外を利用したい場合にのみ設定します
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します
*/
var apiKey: String = "" @JvmSynthetic set

/**
* [Config.baseUrl]を変更します。
* URLを変更することで、地域や環境を設定することができます。
*
* 設定ファイルから自動でロードされるベースURL以外を利用したい場合にのみ設定します
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します
*/
var baseUrl: String = "" @JvmSynthetic set

/**
* [Config.dataLocation]を変更します。
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します。
*/
var dataLocation: String = "" @JvmSynthetic set

/**[Config.isDryRun]を変更します。*/
var isDryRun: Boolean = false @JvmSynthetic set

Expand All @@ -141,24 +155,30 @@ open class Config protected constructor(

/**
* [Config.appKey]を変更します。
* 設定ファイルから自動でロードされるアプリケーションキー以外を利用したい場合にのみ設定します
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します
*/
fun appKey(appKey: String): Builder = apply { this.appKey = appKey }

/**
* [Config.apiKey]を変更します。
* 設定ファイルから自動でロードされるAPIキー以外を利用したい場合にのみ設定します
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します
*/
fun apiKey(apiKey: String): Builder = apply { this.apiKey = apiKey }

/**
* [Config.baseUrl]を変更します。
* URLを変更することで、地域や環境を設定することができます。
*
* 設定ファイルから自動でロードされるベースURL以外を利用したい場合にのみ設定します
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します
*/
fun baseUrl(baseUrl: String): Builder = apply { this.baseUrl = baseUrl }

/**
* [Config.dataLocation]を変更します。
* 設定ファイルから自動でロードされる値以外を利用したい場合にのみ設定します。
*/
fun dataLocation(dataLocation: String): Builder = apply { this.dataLocation = dataLocation }

/**[Config.isDryRun]を変更します。*/
fun isDryRun(isDryRun: Boolean): Builder = apply { this.isDryRun = isDryRun }

Expand Down Expand Up @@ -187,7 +207,9 @@ open class Config protected constructor(
isOptOut,
enabledTrackingAaid,
libraryConfigs
)
).also { config ->
config.dataLocation = dataLocation
}
}

companion object {
Expand Down Expand Up @@ -228,6 +250,11 @@ open class Config protected constructor(
cfg.baseUrl = it
}
}
if (cfg._dataLocation.isEmpty()) {
readStringFromResource(context, "karte_data_location")?.let {
cfg.dataLocation = it
}
}
return cfg
}

Expand Down
15 changes: 15 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 @@ -237,12 +237,27 @@ class SetupTest {
@Test
fun baseUrl_指定したendpointに対してリクエストが行われること() {
setup()
// mockに向ける必要があるため、baseUrlは必ずcode経由
assertThat(KarteApp.self.config.baseUrl).isEqualTo(server.url("/v0/native").toString())

Robolectric.buildActivity(Activity::class.java).create()
proceedBufferedCall()
assertThat(dispatcher.trackedRequests().first().requestUrl)
.isEqualTo(server.url("/v0/native/track"))
}

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

/** [isLimitAdTrackingEnabled] がfalseの時、trackingを許可している. */
private fun mockAdvertisingId(isLimitAdTrackingEnabled: Boolean) {
mockkStatic(AdvertisingIdClient::class)
Expand Down
124 changes: 62 additions & 62 deletions core/src/test/java/io/karte/android/unit/ConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ConfigTest {
assertThat(config.appKey).isEmpty()
assertThat(config.isValidAppKey).isFalse()
assertThat(config.baseUrl).isEqualTo("https://b.karte.io/v0/native")
assertThat(config.dataLocation).isEqualTo("tw")
assertThat(config.isDryRun).isFalse()
assertThat(config.isOptOut).isFalse()
assertThat(config.enabledTrackingAaid).isFalse()
Expand All @@ -51,6 +52,7 @@ class ConfigTest {
assertThat(config.appKey).isEqualTo("sampleappkey_1234567890123456789")
assertThat(config.isValidAppKey).isTrue()
assertThat(config.baseUrl).isEqualTo("https://b-rs.karte.io/v0/native")
assertThat(config.dataLocation).isEqualTo("rs")
assertThat(config.isDryRun).isFalse()
assertThat(config.isOptOut).isFalse()
assertThat(config.enabledTrackingAaid).isFalse()
Expand All @@ -63,6 +65,7 @@ class ConfigTest {
apiKey = "dummy_api_key"
appKey = "dummy_application_key_1234567890"
baseUrl = "https://b-jp.karte.io"
dataLocation = "jp"
isDryRun = true
isOptOut = true
enabledTrackingAaid = true
Expand All @@ -73,101 +76,98 @@ class ConfigTest {
assertThat(config.appKey).isEqualTo("dummy_application_key_1234567890")
assertThat(config.isValidAppKey).isTrue()
assertThat(config.baseUrl).isEqualTo("https://b-jp.karte.io/v0/native")
assertThat(config.dataLocation).isEqualTo("jp")
assertThat(config.isDryRun).isTrue()
assertThat(config.isOptOut).isTrue()
assertThat(config.enabledTrackingAaid).isTrue()
assertThat(config.libraryConfigs).isNotEmpty()
}

@Test
fun config_from_resource_with_custom() {
// app keyのみ指定
fun config_from_resource_or_code() {
fun assertFromResource(config: Config, vararg targets: String) {
if (targets.contains("appKey"))
assertThat(config.appKey).isEqualTo("sampleappkey_1234567890123456789")
if (targets.contains("apiKey"))
assertThat(config.apiKey).isEqualTo("sampleapikey_1234567890123456789")
if (targets.contains("baseUrl"))
assertThat(config.baseUrl).isEqualTo("https://b-rs.karte.io/v0/native")
if (targets.contains("dataLocation"))
assertThat(config.dataLocation).isEqualTo("rs")
}

fun assertFromCode(config: Config, vararg targets: String) {
if (targets.contains("appKey"))
assertThat(config.appKey).isEqualTo("dummy_application_key_1234567890")
if (targets.contains("apiKey"))
assertThat(config.apiKey).isEqualTo("dummy_api_key")
if (targets.contains("baseUrl"))
assertThat(config.baseUrl).isEqualTo("https://b-jp.karte.io/v0/native")
if (targets.contains("dataLocation"))
assertThat(config.dataLocation).isEqualTo("jp")
}

// appKey
Config.fillFromResource(application(), Config.build {
appKey = "dummy_application_key_1234567890"
}).let { config ->
// from code
assertThat(config.appKey).isEqualTo("dummy_application_key_1234567890")
assertThat(config.isValidAppKey).isTrue()
// from resource
assertThat(config.apiKey).isEqualTo("sampleapikey_1234567890123456789")
assertThat(config.baseUrl).isEqualTo("https://b-rs.karte.io/v0/native")
assertFromCode(config, "appKey")
assertFromResource(config, "apiKey", "baseUrl", "dataLocation")
}
// api keyのみ指定
// 空文字は上書き
Config.fillFromResource(application(), Config.build {
appKey = ""
}).let { config ->
assertFromResource(config, "appKey", "apiKey", "baseUrl", "dataLocation")
}

// apiKey
Config.fillFromResource(application(), Config.build {
apiKey = "dummy_api_key"
}).let { config ->
// from code
assertThat(config.apiKey).isEqualTo("dummy_api_key")
// from resource
assertThat(config.appKey).isEqualTo("sampleappkey_1234567890123456789")
assertThat(config.isValidAppKey).isTrue()
assertThat(config.baseUrl).isEqualTo("https://b-rs.karte.io/v0/native")
assertFromCode(config, "apiKey")
assertFromResource(config, "appKey", "baseUrl", "dataLocation")
}
// 空文字は上書き
Config.fillFromResource(application(), Config.build {
apiKey = ""
}).let { config ->
assertFromResource(config, "appKey", "apiKey", "baseUrl", "dataLocation")
}
// baseUrlのみ指定

// baseUrl
Config.fillFromResource(application(), Config.build {
baseUrl = "https://b-jp.karte.io"
}).let { config ->
// from code
assertThat(config.baseUrl).isEqualTo("https://b-jp.karte.io/v0/native")
// from resource
assertThat(config.apiKey).isEqualTo("sampleapikey_1234567890123456789")
assertThat(config.appKey).isEqualTo("sampleappkey_1234567890123456789")
assertThat(config.isValidAppKey).isTrue()
assertFromCode(config, "baseUrl")
assertFromResource(config, "appKey", "apiKey", "dataLocation")
}
// baseUrlにdefault値を指定しても、resourceで上書きされない
Config.fillFromResource(application(), Config.build {
baseUrl = "https://b.karte.io"
}).let { config ->
// from code
assertThat(config.baseUrl).isEqualTo("https://b.karte.io/v0/native")
assertFromResource(config, "appKey", "apiKey", "dataLocation")
}
// app keyとapi key指定
// 空文字は上書き
Config.fillFromResource(application(), Config.build {
apiKey = "dummy_api_key"
appKey = "dummy_application_key_1234567890"
}).let { config ->
// from code
assertThat(config.apiKey).isEqualTo("dummy_api_key")
assertThat(config.appKey).isEqualTo("dummy_application_key_1234567890")
assertThat(config.isValidAppKey).isTrue()
// from resource
assertThat(config.baseUrl).isEqualTo("https://b-rs.karte.io/v0/native")
}
// app keyとbaseUrl指定
Config.fillFromResource(application(), Config.build {
appKey = "dummy_application_key_1234567890"
baseUrl = "https://b-jp.karte.io"
baseUrl = ""
}).let { config ->
// from code
assertThat(config.appKey).isEqualTo("dummy_application_key_1234567890")
assertThat(config.isValidAppKey).isTrue()
assertThat(config.baseUrl).isEqualTo("https://b-jp.karte.io/v0/native")
// from resource
assertThat(config.apiKey).isEqualTo("sampleapikey_1234567890123456789")
assertFromResource(config, "appKey", "apiKey", "baseUrl", "dataLocation")
}
// api keyとbaseUrl指定

// dataLocation
Config.fillFromResource(application(), Config.build {
apiKey = "dummy_api_key"
baseUrl = "https://b-jp.karte.io"
dataLocation = "jp"
}).let { config ->
// from code
assertThat(config.apiKey).isEqualTo("dummy_api_key")
assertThat(config.baseUrl).isEqualTo("https://b-jp.karte.io/v0/native")
// from resource
assertThat(config.appKey).isEqualTo("sampleappkey_1234567890123456789")
assertThat(config.isValidAppKey).isTrue()
assertFromCode(config, "dataLocation")
assertFromResource(config, "appKey", "apiKey", "baseUrl")
}
// 全部指定
// 空文字は上書き
Config.fillFromResource(application(), Config.build {
apiKey = "dummy_api_key"
appKey = "dummy_application_key_1234567890"
baseUrl = "https://b-jp.karte.io"
dataLocation = ""
}).let { config ->
// from code
assertThat(config.apiKey).isEqualTo("dummy_api_key")
assertThat(config.appKey).isEqualTo("dummy_application_key_1234567890")
assertThat(config.isValidAppKey).isTrue()
assertThat(config.baseUrl).isEqualTo("https://b-jp.karte.io/v0/native")
assertFromResource(config, "appKey", "apiKey", "baseUrl", "dataLocation")
}
}
}
1 change: 1 addition & 0 deletions core/src/test/res/values/karte.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="karte_app_key">sampleappkey_1234567890123456789</string>
<string name="karte_api_key">sampleapikey_1234567890123456789</string>
<string name="karte_base_url">https://b-rs.karte.io</string>
<string name="karte_data_location">rs</string>
</resources>
Loading

0 comments on commit df398a6

Please sign in to comment.