-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: use meta-data for user agent client #439
Changes from 4 commits
8047db3
6539358
d6cc10f
c5689c5
77eb849
8570108
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<application> | ||
<meta-data | ||
android:name="io.customer.sdk.android.core.USER_AGENT" | ||
mrehan27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
android:value="TestUserAgent" /> | ||
<meta-data | ||
android:name="io.customer.sdk.android.core.SDK_VERSION" | ||
android:value="1.3.5" /> | ||
</application> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only for tests. But this is how we can modify these values in wrapper SDKs. |
||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.customer.sdk.data.store | ||
|
||
import io.customer.commontest.core.AndroidTest | ||
import io.customer.sdk.core.extensions.applicationMetaData | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.Test | ||
|
||
class AndroidManifestClientTest : AndroidTest() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be great if we can test meta data merging here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added multiple manifests, test will not change because the changes have to be made when merging Manifest |
||
@Test | ||
fun fromManifest_givenTestMetaData_expectClientWithTestMetaData() { | ||
val client = Client.fromMetadata(application.applicationMetaData()) | ||
|
||
client.toString() shouldBeEqualTo "TestUserAgent Client/1.3.5" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.customer.sdk.core.extensions | ||
|
||
import android.content.Context | ||
import android.content.pm.ApplicationInfo | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.os.Bundle | ||
import io.customer.sdk.core.di.SDKComponent | ||
|
||
/** | ||
* Gets meta-data from AndroidManifest.xml file. | ||
* | ||
* @return The meta-data bundle from application info. | ||
*/ | ||
fun Context.applicationMetaData(): Bundle? = runCatching { | ||
val appInfo: ApplicationInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
packageManager.getApplicationInfo( | ||
packageName, | ||
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()) | ||
) | ||
} else { | ||
@Suppress("DEPRECATION") | ||
packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA) | ||
} | ||
|
||
return@runCatching appInfo.metaData | ||
}.onFailure { ex -> | ||
SDKComponent.logger.error("Failed to get application meta-data: ${ex.message}") | ||
}.getOrNull() | ||
mrehan27 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,45 @@ | ||
package io.customer.sdk.data.store | ||
|
||
import android.os.Bundle | ||
import io.customer.sdk.Version | ||
|
||
/** | ||
* Sealed class to hold information about the SDK wrapper and package that the | ||
* client app is using. | ||
* Represents the client information to append with user-agent. | ||
* | ||
* @property source name of the client to append with user-agent. | ||
* @property sdkVersion version of the SDK used. | ||
*/ | ||
sealed class Client( | ||
@Suppress("MemberVisibilityCanBePrivate") | ||
class Client( | ||
val source: String, | ||
val sdkVersion: String | ||
) { | ||
override fun toString(): String = "$source Client/$sdkVersion" | ||
|
||
/** | ||
* Simpler class for Android clients. | ||
*/ | ||
class Android(sdkVersion: String) : Client(source = SOURCE_ANDROID, sdkVersion = sdkVersion) | ||
|
||
/** | ||
* Simpler class for ReactNative clients. | ||
*/ | ||
class ReactNative(sdkVersion: String) : Client( | ||
source = SOURCE_REACT_NATIVE, | ||
sdkVersion = sdkVersion | ||
) | ||
|
||
/** | ||
* Simpler class for Expo clients. | ||
*/ | ||
class Expo(sdkVersion: String) : Client(source = SOURCE_EXPO, sdkVersion = sdkVersion) | ||
|
||
/** | ||
* Simpler class for Flutter clients. | ||
*/ | ||
class Flutter(sdkVersion: String) : Client(source = SOURCE_FLUTTER, sdkVersion = sdkVersion) | ||
|
||
/** | ||
* Other class to allow adding custom sources for clients that are not | ||
* supported above. | ||
* <p/> | ||
* Use this only if the client platform is not available in the above list. | ||
*/ | ||
class Other internal constructor( | ||
source: String, | ||
sdkVersion: String | ||
) : Client(source = source, sdkVersion = sdkVersion) | ||
|
||
companion object { | ||
internal const val SOURCE_ANDROID = "Android" | ||
internal const val SOURCE_REACT_NATIVE = "ReactNative" | ||
internal const val SOURCE_EXPO = "Expo" | ||
internal const val SOURCE_FLUTTER = "Flutter" | ||
private const val SOURCE_ANDROID = "Android" | ||
internal const val META_DATA_USER_AGENT = "io.customer.sdk.android.core.USER_AGENT" | ||
mrehan27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
internal const val META_DATA_SDK_VERSION = "io.customer.sdk.android.core.SDK_VERSION" | ||
|
||
/** | ||
* Helper method to create client from raw values | ||
* Creates a new [Client] instance from the manifest meta-data. | ||
* If the user-agent or SDK version is not found, the default client is returned. | ||
* Default client is created with [SOURCE_ANDROID] and SDK version mentioned in [Version] class. | ||
* | ||
* @param source raw string of client source (case insensitive) | ||
* @param sdkVersion version of the SDK used | ||
* @return [Client] created from provided values | ||
* @param metadata Android application meta-data to retrieve the user-agent and SDK version from. | ||
* @return The client instance created from the manifest meta-data. | ||
* If not found, the default client is returned. | ||
*/ | ||
fun fromRawValue(source: String, sdkVersion: String): Client = when { | ||
source.equals( | ||
other = SOURCE_ANDROID, | ||
ignoreCase = true | ||
) -> Android(sdkVersion = sdkVersion) | ||
source.equals( | ||
other = SOURCE_REACT_NATIVE, | ||
ignoreCase = true | ||
) -> ReactNative(sdkVersion = sdkVersion) | ||
source.equals( | ||
other = SOURCE_EXPO, | ||
ignoreCase = true | ||
) -> Expo(sdkVersion = sdkVersion) | ||
source.equals( | ||
other = SOURCE_FLUTTER, | ||
ignoreCase = true | ||
) -> Flutter(sdkVersion = sdkVersion) | ||
else -> Other(source = source, sdkVersion = sdkVersion) | ||
fun fromMetadata(metadata: Bundle?): Client { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what would happen if both android SDK and react native SDK manifest have same values? shouldn't we override or handle that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added values in common test to help verify manifest merger challenges |
||
val userAgent = metadata?.getString(META_DATA_USER_AGENT) | ||
val sdkVersion = metadata?.getString(META_DATA_SDK_VERSION) | ||
|
||
// If either value is null or blank, return the default client | ||
return if (userAgent.isNullOrBlank() || sdkVersion.isNullOrBlank()) { | ||
Client(source = SOURCE_ANDROID, sdkVersion = Version.version) | ||
} else { | ||
Client(source = userAgent, sdkVersion = sdkVersion) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.customer.sdk.data.store | ||
|
||
import android.os.Bundle | ||
import io.customer.commontest.core.RobolectricTest | ||
import io.customer.sdk.Version | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class ClientTest : RobolectricTest() { | ||
private val defaultClientString: String = "Android Client/${Version.version}" | ||
|
||
private fun createMetadata( | ||
userAgent: String?, | ||
sdkVersion: String? | ||
) = Bundle().apply { | ||
userAgent?.let { putString(Client.META_DATA_USER_AGENT, it) } | ||
sdkVersion?.let { putString(Client.META_DATA_SDK_VERSION, it) } | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenValidMetaData_expectClientWithMetaData() { | ||
val metadata = createMetadata("ReactNative", "1.2.3") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo "ReactNative Client/1.2.3" | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenNullUserAgent_expectDefaultSourceUsed() { | ||
val metadata = createMetadata(null, "1.2.3") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenEmptyUserAgent_expectDefaultSourceUsed() { | ||
val metadata = createMetadata("", "1.2.3") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenNullSdkVersion_expectDefaultSdkVersionUsed() { | ||
val metadata = createMetadata("ReactNative", null) | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenEmptySdkVersion_expectDefaultSdkVersionUsed() { | ||
val metadata = createMetadata("ReactNative", "") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenNullMetaData_expectDefaultValuesUsed() { | ||
val metadata = createMetadata(null, null) | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenEmptyMetaData_expectDefaultValuesUsed() { | ||
val metadata = createMetadata("", "") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now no longer need Client to register and initialize
AndroidSDKComponent