Skip to content

Commit

Permalink
Merge pull request #678 from braintree/kotlin_authorization_loader
Browse files Browse the repository at this point in the history
Refactor `AuthorizationLoader` to Kotlin
  • Loading branch information
sshropshire authored Feb 13, 2023
2 parents 6fa5189 + b19f52c commit 7145eec
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 302 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.braintreepayments.api

import androidx.annotation.RestrictTo
import java.lang.Exception

/**
* @suppress
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
interface AuthorizationCallback {
fun interface AuthorizationCallback {
fun onAuthorizationResult(authorization: Authorization?, error: Exception?)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.braintreepayments.api

import androidx.annotation.VisibleForTesting

internal class AuthorizationLoader(
initialAuthString: String?,
private val clientTokenProvider: ClientTokenProvider?
) {
// cache initial auth if available
@VisibleForTesting
var authorizationFromCache = initialAuthString?.let { Authorization.fromString(it) }

fun loadAuthorization(callback: AuthorizationCallback) {
if (authorizationFromCache != null) {
callback.onAuthorizationResult(authorizationFromCache, null)
} else if (clientTokenProvider != null) {
clientTokenProvider.getClientToken(object : ClientTokenCallback {
override fun onSuccess(clientToken: String) {
authorizationFromCache = Authorization.fromString(clientToken)
callback.onAuthorizationResult(authorizationFromCache, null)
}

override fun onFailure(error: Exception) {
callback.onAuthorizationResult(null, error)
}
})
} else {
val clientSDKSetupURL =
"https://developer.paypal.com/braintree/docs/guides/client-sdk/setup/android/v4#initialization"
val message = "Authorization required. See $clientSDKSetupURL for more info."
callback.onAuthorizationResult(null, BraintreeException(message))
}
}

fun invalidateClientToken() {
// only invalidate client token cache if we can fetch a new one with a client token provider
if (clientTokenProvider != null) {
authorizationFromCache = null
}
}
}

This file was deleted.

Loading

0 comments on commit 7145eec

Please sign in to comment.