Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v7] Make APIClient Non-optional & Update BTAmericanExpressClient Init #1527

Open
wants to merge 15 commits into
base: v7
Choose a base branch
from

Conversation

jaxdesmarais
Copy link
Contributor

Summary of changes

  • Update initializer to BTAmericanExpressClient(authorization:)
  • Add InvalidAuthorization type - this matches the pattern used on Android
  • Update BTAPIClient initializer to require an authorization string
  • Add new BTAPIClientError.invalidAuthorization
  • Update PaymentButtonBaseViewController with new authorization string
  • Update AmexViewController demo app with new initializer
  • Update unit and integration tests
  • Add TODOs for final PR cleanup

Checklist

  • Added a changelog entry
  • Tested and confirmed payment flows affected by this change are functioning as expected

Authors

@jaxdesmarais jaxdesmarais requested a review from a team as a code owner February 20, 2025 17:04
@jaxdesmarais jaxdesmarais changed the title [v7] Make APIClient Non-optional & Update BTAmexClient [v7] Make APIClient Non-optional & Update BTAmericanExpressClient Init Feb 20, 2025
@@ -3,7 +3,9 @@ import BraintreeCore

class PaymentButtonBaseViewController: BaseViewController {

// TODO: remove API client in final PR
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some TODOs throughout this PR to update in our final PR for this work

} catch {
completion(nil, error)

if authorization.type == .invalidAuthorization {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On android we do this check in each request (POST, GET, etc). Since fetchOrReturnRemoteConfiguration is the first API call in our SDK it made sense to me to throw the error here instead vs adding to each of the HTTP/GraphQLHTTP methods. This will allow us to return an error before making any API calls in the SDK if the auth is invalid.

Copy link
Contributor

@scannillo scannillo Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could miss something by nesting it within fetchOrReturnRemoteConfiguration. Actually, this PR as-is misses doing auth string validation for shopperInsightsClient.getRecommendedPaymentMethods() calls, since it hits apiClient.post() directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it will be better to call the auth string checks explicitly, vs relying on fetchOrReturnRemoteConfiguration(), which is already kind of confusing

Copy link
Contributor Author

@jaxdesmarais jaxdesmarais Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch, yes we do miss that currently. By call the auth string checks explicitly what do you mean? We want to make the network request in fetchOrReturnRemoteConfiguration before returning an error and move these to the get/post/etc requests? ignore me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nvm we can still do them before that network call. Updated here: 15bed08 and checked that getRecommendedPaymentMethods would hit that block as well.

@jaxdesmarais
Copy link
Contributor Author

UI tests are expected to fail currently, we have ticket DTMOBILES-1372 to address those failures.

Comment on lines +6 to +9
let type = AuthorizationType.invalidAuthorization
let configURL: URL
let bearer: String
let originalValue: String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these values are actually going to be used / we're just setting these properties to conform to the protocol but never using them functionally.

I crossed this bridge when messing around with this in this branch. I wonder if there's a cleaner pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially but I do think there is some value in enforcing them for the valid auth types. Since they won't be used it seems harmless. We also do something similar on Android.

if authorization.type == .invalidAuthorization {
completion(nil, BTAPIClientError.invalidAuthorization(authorization.originalValue))
} else {
Task { @MainActor in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we're dispatching to MainActor in our network layer? I feel like that should be up to the merchant when they get our async result from tokenize() with either an error or success

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a change in code, it's just moving this logic into an else. But our code expects fetch or return config to be run on the main thread according to this test:

Copy link
Contributor

@richherrera richherrera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!! 🚀

Copy link
Contributor

@agedd agedd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants