Skip to content

Commit

Permalink
baseUrl from xml (#247)
Browse files Browse the repository at this point in the history
* core: config rename baseUrl -> serviceUrl
iam: add config
* add api update

* add iam test

* fix: add config api for java

* add iam config builder

* vup ruby
  • Loading branch information
wasnot authored Jan 12, 2024
1 parent 318fcc4 commit efd285f
Show file tree
Hide file tree
Showing 18 changed files with 509 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/danger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
ruby-version: 3.3
bundler-cache: true
- name: Setup danger
run: gem install danger
Expand Down
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

| モジュール/プラグイン名 | Description | 最新のバージョン |
| :-- | :-- | :-- |
| core | イベントトラッキング機能を提供します。 | 2.22.0 |
| inappmessaging | アプリ内メッセージ機能を提供します。 | 2.17.0 |
| core | イベントトラッキング機能を提供します。 | 2.23.0 |
| inappmessaging | アプリ内メッセージ機能を提供します。 | 2.18.0 |
| notifications | プッシュ通知の受信および効果測定機能を提供します。 | 2.10.0 |
| variables | 設定値配信機能を提供します。 | 2.4.0 |
| visualtracking | ビジュアルトラッキング機能を提供します。| 2.9.0 |
| inbox | Push通知の送信履歴を取得する機能を提供します(β版)。 | 0.1.0 |
| Karte Gradle Plugin | ビジュアルトラッキング機能に必要なプラグインです。| 2.5.1 |

# Releases - xxxx.xx.xx

### Core 2.23.0
** 🔨CHANGED**
- KARTEのベースURLの指定可能な値をサブパスを含まないものに変更しました。
- リソースファイルからの初期化時には、リソースのベースURLを確認し読み込むようになりました。

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

# Releases - 2023.11.24

### Karte Gradle Plugin 2.5.1
Expand Down
2 changes: 2 additions & 0 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public class io/karte/android/core/config/Config {

public class io/karte/android/core/config/Config$Builder {
public fun <init> ()V
public final fun apiKey (Ljava/lang/String;)Lio/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 enabledTrackingAaid (Z)Lio/karte/android/core/config/Config$Builder;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/io/karte/android/KarteApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class KarteApp private constructor() : ActivityLifecycleCallback() {
Logger.w(LOG_TAG, "APP_KEY is already exists.")
return
}
val configWithAppKey = Config.withAppKey(context, config)
val configWithAppKey = Config.fillFromResource(context, config)
if (!configWithAppKey.isValidAppKey) {
Logger.w(LOG_TAG, "Invalid APP_KEY is set. ${configWithAppKey.appKey}")
return
Expand Down
141 changes: 94 additions & 47 deletions core/src/main/java/io/karte/android/core/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.karte.android.core.config

import android.content.Context
import android.net.Uri
import io.karte.android.BuildConfig
import io.karte.android.KarteException
import io.karte.android.R
Expand All @@ -27,11 +28,11 @@ import io.karte.android.core.library.LibraryConfig
* - Kotlinでは[Config.build]関数でインスタンスを生成します。
* - Javaでは[Config.Builder.build]関数でインスタンスを生成します。
*
* @property[baseUrl] ベースURLの取得・設定を行います。
*
* **SDK内部で利用するプロパティであり、通常のSDK利用でこちらのプロパティを利用することはありません。**
* @param[baseUrl] ベースURLの取得・設定を行います。
* URLを変更することで、地域や環境を設定することができます。
*
* @property[logCollectionUrl] ログ収集URLの取得・設定を行います。
* ログ収集機能は廃止されたため、この設定は使用されません。
*
* **SDK内部で利用するプロパティであり、通常のSDK利用でこちらのプロパティを利用することはありません。**
*
Expand Down Expand Up @@ -59,46 +60,72 @@ import io.karte.android.core.library.LibraryConfig
open class Config protected constructor(
appKey: String,
apiKey: String,
val baseUrl: String,
baseUrl: String,
@Deprecated("No longer used")
internal val logCollectionUrl: String,
val isDryRun: Boolean,
val isOptOut: Boolean,
val enabledTrackingAaid: Boolean,
val libraryConfigs: List<LibraryConfig>
) {
/**
* @property[appKey] アプリケーションキーの取得・設定を行います。
*
* 設定ファイルから自動でロードされるアプリケーションキー以外を利用したい場合にのみ設定します。
* @property[appKey] アプリケーションキーの取得を行います。
*/
var appKey: String = appKey
internal set

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

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

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

/**
* @property[baseUrl] ベースURLの取得を行います。
* 設定されたURLにサブパスを付与したものを返します。
*/
var baseUrl: String
private set(value) {
if (value.isEmpty())
return
_baseUrl = Uri.withAppendedPath(Uri.parse(value), "v0/native").toString()
}
get() {
if (_baseUrl.isEmpty())
return "https://b.karte.io/v0/native"
return _baseUrl
}

init {
this.baseUrl = baseUrl
}

/** [Config]クラスの生成を行うためのクラスです。 */
open class Builder {
/**[Config.appKey]を変更します。*/
/**
* [Config.appKey]を変更します。
* 設定ファイルから自動でロードされるアプリケーションキー以外を利用したい場合にのみ設定します。
*/
var appKey: String = "" @JvmSynthetic set

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

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

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

/**[Config.isDryRun]を変更します。*/
var isDryRun: Boolean = false @JvmSynthetic set
Expand All @@ -112,12 +139,25 @@ open class Config protected constructor(
/**[Config.libraryConfigs]を変更します。*/
var libraryConfigs: List<LibraryConfig> = listOf() @JvmSynthetic set

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

/**[Config.logCollectionUrl]を変更します。*/
internal fun logCollectionUrl(logCollectionUrl: String): Builder =
apply { this.logCollectionUrl = logCollectionUrl }
/**
* [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.isDryRun]を変更します。*/
fun isDryRun(isDryRun: Boolean): Builder = apply { this.isDryRun = isDryRun }
Expand All @@ -142,7 +182,7 @@ open class Config protected constructor(
appKey,
apiKey,
baseUrl,
logCollectionUrl,
"",
isDryRun,
isOptOut,
enabledTrackingAaid,
Expand All @@ -161,35 +201,42 @@ open class Config protected constructor(
return builder.build()
}

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

private fun appKeyFromResource(context: Context): String {
val res = context.resources
val pkg = res.getResourcePackageName(R.id.karte_resources)
val id = res.getIdentifier("karte_app_key", "string", pkg)
return if (id == 0) {
if (BuildConfig.DEBUG) {
/**
* 初期化済みのconfigのパラメータをresourceから補完する
*
* * configがnullならデフォルト値で初期化する
* * パラメータに空文字以外が設定済みなら上書きしない
* * 設定ファイルに存在しなければ補完しない
* * **app_keyがここまでで与えられなかった場合、デバッグビルドでは例外をスローする**
*/
internal fun fillFromResource(context: Context, config: Config?): Config {
val cfg = config ?: build()
if (cfg.appKey.isEmpty()) {
val appKeyFromResource = readStringFromResource(context, "karte_app_key")
// appKeyに限り 未設定&resourceにない&debugビルド 時に例外スロー
if (appKeyFromResource == null && BuildConfig.DEBUG)
throw KarteException("karte_resources.xml not found.")
} else {
""
appKeyFromResource?.let { cfg.appKey = it }
}
if (cfg.apiKey.isEmpty()) {
readStringFromResource(context, "karte_api_key")?.let {
cfg.apiKey = it
}
}
if (cfg._baseUrl.isEmpty()) {
readStringFromResource(context, "karte_base_url")?.let {
cfg.baseUrl = it
}
} else {
res.getString(id)
}
return cfg
}

private fun apiKeyFromResource(context: Context): String {
private fun readStringFromResource(context: Context, name: String): String? {
val res = context.resources
val pkg = res.getResourcePackageName(R.id.karte_resources)
val id = res.getIdentifier("karte_api_key", "string", pkg)
val id = res.getIdentifier(name, "string", pkg)
return if (id == 0) {
""
null
} else {
res.getString(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ExperimentalConfig private constructor(
appKey,
apiKey,
baseUrl,
logCollectionUrl,
"",
isDryRun,
isOptOut,
enabledTrackingAaid,
Expand Down
9 changes: 5 additions & 4 deletions core/src/test/java/io/karte/android/integration/SetupTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,13 @@ class SetupTest {
private val overwriteAppKey = "overwriteappkey_1234567890123456"

private fun setup(configBuilder: Config.Builder = Config.Builder()) {
val config = configBuilder.baseUrl(server.url("/native").toString()).build()
val config = configBuilder.baseUrl(server.url("").toString()).build()
when (pattern) {
SetupPattern.FROM_RESOURCE -> KarteApp.setup(application, config)
SetupPattern.BY_CONFIG -> KarteApp.setup(
application,
config.apply { appKey = overwriteAppKey })

SetupPattern.BY_METHOD -> KarteApp.setup(application, overwriteAppKey, config)
}
}
Expand Down Expand Up @@ -239,7 +240,7 @@ class SetupTest {
Robolectric.buildActivity(Activity::class.java).create()
proceedBufferedCall()
assertThat(dispatcher.trackedRequests().first().requestUrl)
.isEqualTo(server.url("/native/track"))
.isEqualTo(server.url("/v0/native/track"))
}

/** [isLimitAdTrackingEnabled] がfalseの時、trackingを許可している. */
Expand Down Expand Up @@ -315,7 +316,7 @@ class SetupTest {
Robolectric.buildActivity(Activity::class.java).create()
proceedBufferedCall()
assertThat(dispatcher.trackedRequests().first().requestUrl)
.isEqualTo(server.url("/native/track"))
.isEqualTo(server.url("/v0/native/track"))
}

@Test
Expand All @@ -324,7 +325,7 @@ class SetupTest {
Robolectric.buildActivity(Activity::class.java).create()
proceedBufferedCall()
assertThat(dispatcher.ingestRequests().first().requestUrl)
.isEqualTo(server.url("/native/ingest"))
.isEqualTo(server.url("/v0/native/ingest"))
}

class TestLibraryConfig(val enableTest: Boolean) : LibraryConfig
Expand Down
Loading

0 comments on commit efd285f

Please sign in to comment.