From c1ad77bdaea7c400ec0d27deaee38123d37d473e Mon Sep 17 00:00:00 2001 From: Mansi Pandya Date: Fri, 9 Aug 2024 16:32:52 -0400 Subject: [PATCH] Refactor code for improved readability --- .../networking/MParticleBaseClientImplTest.kt | 60 +++++++++++++++++-- .../networking/MParticleBaseClientImpl.java | 45 ++++++++------ 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt index 9202b2d42..61e8bd59b 100644 --- a/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt +++ b/android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt @@ -245,7 +245,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory us1-foo/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, "nativesdks.mparticle.com", "v2/us1-akshd324uajbhg123OIASI/events") + val result = baseClientImpl.generateDefaultURL( + false, + uri, + "nativesdks.mparticle.com", + "v2/us1-akshd324uajbhg123OIASI/events" + ) assertEquals("https://nativesdks.mparticle.com/v2/us1-akshd324uajbhg123OIASI/events", result.toString()) } @@ -263,7 +268,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory eu1-fooapi/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, "", "v2/us1-asjdjasdgjhasgdjhas/events") + val result = baseClientImpl.generateDefaultURL( + false, + uri, + "", + "v2/us1-asjdjasdgjhasgdjhas/events" + ) assertEquals("https://nativesdks.us1.mparticle.com/v2/us1-asjdjasdgjhasgdjhas/events", result.toString()) } @@ -281,7 +291,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory us1-foo/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, null, "v2/us1-asjdjasdgjhasgdjhas/events") + val result = baseClientImpl.generateDefaultURL( + false, + uri, + null, + "v2/us1-asjdjasdgjhasgdjhas/events" + ) assertEquals("https://nativesdks.us1.mparticle.com/v2/us1-asjdjasdgjhasgdjhas/events", result.toString()) } @@ -293,7 +308,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .build() MParticle.start(options) val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl - val result = baseClientImpl.generateDefaultURL(null, "nativesdks.mparticle.com", "v2/us1-bee5781b649a7a40a592c2000bc892d0/events") + val result = baseClientImpl.generateDefaultURL( + false, + null, + "nativesdks.mparticle.com", + "v2/us1-bee5781b649a7a40a592c2000bc892d0/events" + ) assertEquals(null, result) } @@ -311,7 +331,12 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .encodedAuthority("nativesdks.us1.mparticle.com") .path("$subdirectory us1-foo/events") .build() - val result = baseClientImpl.generateDefaultURL(uri, "nativesdks.mparticle.com", null) + val result = baseClientImpl.generateDefaultURL( + false, + uri, + "nativesdks.mparticle.com", + null + ) assertEquals("https://nativesdks.mparticle.com/v2/%20us1-foo/events", result.toString()) } @@ -323,7 +348,30 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() { .build() MParticle.start(options) val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl - val result = baseClientImpl.generateDefaultURL(null, null, null) + val result = baseClientImpl.generateDefaultURL(false, null, null, null) + assertEquals(null, result) + } + + @Test + @Throws(MalformedURLException::class) + fun testGenerateDefaultURL_When_defaultDomain_FLAG_IS_TRUE() { + val options = MParticleOptions.builder(mContext) + .credentials(apiKey, "secret") + .build() + MParticle.start(options) + val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl + val subdirectory = "/v2/" + val uri = Uri.Builder() + .scheme(BuildConfig.SCHEME) + .encodedAuthority("nativesdks.us1.mparticle.com") + .path("$subdirectory us1-foo/events") + .build() + val result = baseClientImpl.generateDefaultURL( + true, + uri, + "nativesdks.mparticle.com", + "v2/us1-akshd324uajbhg123OIASI/events" + ) assertEquals(null, result) } } diff --git a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java index abdd0037c..bd935d45a 100644 --- a/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java +++ b/android-core/src/main/java/com/mparticle/networking/MParticleBaseClientImpl.java @@ -23,10 +23,10 @@ public class MParticleBaseClientImpl implements MParticleBaseClient { - private Context mContext; - private ConfigManager mConfigManager; + private final Context mContext; + private final ConfigManager mConfigManager; private BaseNetworkConnection mRequestHandler; - private SharedPreferences mPreferences; + private final SharedPreferences mPreferences; String mApiKey; private static final String SERVICE_VERSION_1 = "/v1"; @@ -103,24 +103,29 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean NetworkOptions networkOptions = mConfigManager.getNetworkOptions(); DomainMapping domainMapping = networkOptions.getDomain(endpoint); String url = NetworkOptionsManager.getDefaultUrl(endpoint); - // Default domain to use for URL generation when domain mapping is specified + + // `defaultDomain` variable is for URL generation when domain mapping is specified. String defaultDomain = url; - boolean isDefaultUrl = true; + boolean isDefaultDomain = true; + + //Check if domain mapping is specified and update the URL based on domain mapping String domainMappingUrl = domainMapping != null ? domainMapping.getUrl() : null; if (!MPUtility.isEmpty(domainMappingUrl)) { - isDefaultUrl = url.equals(domainMappingUrl); + isDefaultDomain = url.equals(domainMappingUrl); url = domainMappingUrl; } - Uri uri; - // If domain mapping is not specified, need to set the prefix when it is enabled in the configuration. If EndPoint is config, there's no need to set the prefix. + if (endpoint != Endpoint.CONFIG) { - if (isDefaultUrl) { + // Set URL with pod prefix if it’s the default domain and endpoint is not CONFIG + if (isDefaultDomain) { url = getPodUrl(url, mConfigManager.getPodPrefix(), mConfigManager.isDirectUrlRoutingEnabled()); } else { // When domain mapping is specified, generate the default domain. Whether podRedirection is enabled or not, always use the original URL. defaultDomain = getPodUrl(defaultDomain, null, false); } } + + Uri uri; String subdirectory; String pathPrefix; String pathPostfix; @@ -147,7 +152,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean } } } - return MPUrl.getUrl(builder.build().toString(), !isDefaultUrl ? generateDefaultURL(builder.build(), defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(builder.build().toString(), generateDefaultURL(isDefaultDomain, builder.build(), defaultDomain, (pathPrefix + pathPostfix))); case EVENTS: pathPrefix = SERVICE_VERSION_2 + "/"; subdirectory = overridesSubdirectory ? "" : pathPrefix; @@ -158,7 +163,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, (pathPrefix + pathPostfix))); case ALIAS: pathPrefix = SERVICE_VERSION_1 + "/identity/"; subdirectory = overridesSubdirectory ? "" : pathPrefix; @@ -168,7 +173,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .encodedAuthority(url) .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, (pathPrefix + pathPostfix))); case IDENTITY: pathPrefix = SERVICE_VERSION_1 + "/"; subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_1 + "/"; @@ -178,7 +183,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .encodedAuthority(url) .path(subdirectory + pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, (pathPrefix + pathPostfix)) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, (pathPrefix + pathPostfix))); case AUDIENCE: pathPostfix = SERVICE_VERSION_2 + "/" + mApiKey + "/audience?mpID=" + mConfigManager.getMpid(); uri = new Uri.Builder() @@ -186,7 +191,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean .encodedAuthority(url) .path(pathPostfix) .build(); - return MPUrl.getUrl(uri.toString(), !isDefaultUrl ? generateDefaultURL(uri, defaultDomain, pathPostfix) : null); + return MPUrl.getUrl(uri.toString(), generateDefaultURL(isDefaultDomain, uri, defaultDomain, pathPostfix)); default: return null; } @@ -196,12 +201,16 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean * Generates a new URL using the default domain when domain mapping is specified. * This method creates a new URI based on the existing URI, but replaces the domain with the default domain. * - * @param uri The existing URI which includes the domain mapping. - * @param defaultDomain The default domain name to be used in the new URI. - * @param path path to be used in the new URI. + * @param isDefaultDomain If the default domain is true, return null without generating a new URL. If the default domain is false, generate and return a new URL. + * @param uri The existing URI which includes the domain mapping. + * @param defaultDomain The default domain name to be used in the new URI. + * @param path path to be used in the new URI. * @return A new URI with the same path and scheme as the original URI, but with the default domain. */ - protected MPUrl generateDefaultURL(Uri uri, String defaultDomain, String path) throws MalformedURLException { + protected MPUrl generateDefaultURL(boolean isDefaultDomain, Uri uri, String defaultDomain, String path) throws MalformedURLException { + if (isDefaultDomain) { + return null; + } if (uri != null) { Uri.Builder uriBuilder = Uri.parse(uri.toString()).buildUpon(); if (defaultDomain != null && !defaultDomain.isEmpty()) {