Skip to content

Commit

Permalink
Refactor code for improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle committed Aug 9, 2024
1 parent 6cf1bef commit c1ad77b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand All @@ -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())
}

Expand All @@ -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())
}

Expand All @@ -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)
}

Expand All @@ -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())
}

Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 + "/";
Expand All @@ -178,15 +183,15 @@ 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()
.scheme(BuildConfig.SCHEME)
.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;
}
Expand All @@ -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()) {
Expand Down

0 comments on commit c1ad77b

Please sign in to comment.