Skip to content

Commit

Permalink
Refactor BraintreeClient Constructors (#1199)
Browse files Browse the repository at this point in the history
* Refactor BraintreeClient constructors and remove BraintreeClientParams and BraintreeOptions

* Fix doc typo
  • Loading branch information
tdchow authored Oct 31, 2024
1 parent 0dd7b58 commit 124eedc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.content.pm.ActivityInfo
import android.net.Uri
import androidx.annotation.RestrictTo
import androidx.annotation.VisibleForTesting
import com.braintreepayments.api.sharedutils.HttpResponseCallback
import com.braintreepayments.api.sharedutils.HttpResponseTiming
import com.braintreepayments.api.sharedutils.ManifestValidator
Expand All @@ -17,7 +16,7 @@ import org.json.JSONObject
*/
@Suppress("LargeClass", "LongParameterList", "TooManyFunctions")
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
class BraintreeClient @VisibleForTesting internal constructor(
class BraintreeClient internal constructor(

/**
* @suppress
Expand All @@ -34,41 +33,26 @@ class BraintreeClient @VisibleForTesting internal constructor(
*/
val authorization: Authorization,

private val analyticsClient: AnalyticsClient,
private val httpClient: BraintreeHttpClient,
private val graphQLClient: BraintreeGraphQLClient,
private val configurationLoader: ConfigurationLoader,
private val manifestValidator: ManifestValidator,
private val time: Time,
private val returnUrlScheme: String,
private val braintreeDeepLinkReturnUrlScheme: String,

/**
* @suppress
*/
val appLinkReturnUri: Uri?,

private val analyticsClient: AnalyticsClient = AnalyticsClient(applicationContext),
private val httpClient: BraintreeHttpClient = BraintreeHttpClient(),
private val graphQLClient: BraintreeGraphQLClient = BraintreeGraphQLClient(),
private val configurationLoader: ConfigurationLoader = ConfigurationLoader(applicationContext, httpClient),
private val manifestValidator: ManifestValidator = ManifestValidator(),
private val time: Time = Time(),
) {

private val crashReporter: CrashReporter
private var launchesBrowserSwitchAsNewTask: Boolean = false

// NOTE: this constructor is used to make dependency injection easy
internal constructor(
params: BraintreeClientParams,
time: Time = Time()
) : this(
applicationContext = params.applicationContext,
integrationType = params.integrationType,
authorization = params.authorization,
analyticsClient = params.analyticsClient,
httpClient = params.httpClient,
graphQLClient = params.graphQLClient,
configurationLoader = params.configurationLoader,
manifestValidator = params.manifestValidator,
time = time,
returnUrlScheme = params.returnUrlScheme,
braintreeDeepLinkReturnUrlScheme = params.braintreeReturnUrlScheme,
appLinkReturnUri = params.appLinkReturnUri
)
private val braintreeDeepLinkReturnUrlScheme: String =
"${getAppPackageNameWithoutUnderscores(applicationContext)}.braintree.deeplinkhandler"

/**
* @suppress
Expand All @@ -78,27 +62,14 @@ class BraintreeClient @VisibleForTesting internal constructor(
authorization: String,
returnUrlScheme: String? = null,
appLinkReturnUri: Uri? = null,
integrationType: IntegrationType? = null,
) : this(
BraintreeOptions(
context = context,
authorization = Authorization.fromString(authorization),
returnUrlScheme = returnUrlScheme,
appLinkReturnUri = appLinkReturnUri
)
)

internal constructor(options: BraintreeOptions) : this(BraintreeClientParams(options))

internal constructor(
context: Context,
authorization: Authorization,
integrationType: IntegrationType
) : this(
BraintreeOptions(
context = context,
authorization = authorization,
integrationType = integrationType,
)
applicationContext = context.applicationContext,
authorization = Authorization.fromString(authorization),
returnUrlScheme = returnUrlScheme
?: "${getAppPackageNameWithoutUnderscores(context.applicationContext)}.braintree",
appLinkReturnUri = appLinkReturnUri,
integrationType = integrationType ?: IntegrationType.CUSTOM,
)

init {
Expand Down Expand Up @@ -370,4 +341,10 @@ class BraintreeClient @VisibleForTesting internal constructor(
val message = "Valid authorization required. See $clientSDKSetupURL for more info."
return BraintreeException(message)
}

companion object {
private fun getAppPackageNameWithoutUnderscores(context: Context): String {
return context.applicationContext.packageName.replace("_", "")
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 124eedc

Please sign in to comment.