-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #678 from braintree/kotlin_authorization_loader
Refactor `AuthorizationLoader` to Kotlin
- Loading branch information
Showing
7 changed files
with
263 additions
and
302 deletions.
There are no files selected for viewing
3 changes: 1 addition & 2 deletions
3
BraintreeCore/src/main/java/com/braintreepayments/api/AuthorizationCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) | ||
} |
52 changes: 0 additions & 52 deletions
52
BraintreeCore/src/main/java/com/braintreepayments/api/AuthorizationLoader.java
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
BraintreeCore/src/main/java/com/braintreepayments/api/AuthorizationLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
194 changes: 0 additions & 194 deletions
194
BraintreeCore/src/test/java/com/braintreepayments/api/AuthorizationLoaderUnitTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.