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

[FC] Unify sync API calls and add refetch strategy #8317

Merged
merged 7 commits into from May 9, 2024

Conversation

carlosmuvi-stripe
Copy link
Collaborator

@carlosmuvi-stripe carlosmuvi-stripe commented Apr 22, 2024

Summary

We've many sync calls / usecases and it's usually confusing to decide which to use. Additionally, there's situations where we want to force a refresh, even if there's a sync response cached, when certain conditions apply.

Removed Added
SynchronizeFinancialConnectionsSession GetOrFetchSync(fetch = Always)
GetManifest GetOrFetchSync().manifest

Also adds an expandable system to add re-fetch conditions. Added a MissingActiveAuthSession condition that will attempt a refresh when retrieving the sync object in PartnerAuth if the auth session does not exist in the local sync response.

Motivation

Testing

  • Added tests
  • Modified tests
  • Manually verified

Screenshots

Before After
before screenshot after screenshot

Changelog

Copy link
Contributor

github-actions bot commented Apr 22, 2024

Diffuse output:

OLD: identity-example-release-base.apk (signature: V1, V2)
NEW: identity-example-release-pr.apk (signature: V1, V2)

          │          compressed          │         uncompressed         
          ├───────────┬───────────┬──────┼───────────┬───────────┬──────
 APK      │ old       │ new       │ diff │ old       │ new       │ diff 
──────────┼───────────┼───────────┼──────┼───────────┼───────────┼──────
      dex │     2 MiB │     2 MiB │  0 B │   4.3 MiB │   4.3 MiB │  0 B 
     arsc │     1 MiB │     1 MiB │  0 B │     1 MiB │     1 MiB │  0 B 
 manifest │   2.3 KiB │   2.3 KiB │  0 B │   8.1 KiB │   8.1 KiB │  0 B 
      res │ 301.5 KiB │ 301.5 KiB │  0 B │   455 KiB │   455 KiB │  0 B 
   native │   7.3 MiB │   7.3 MiB │  0 B │  18.4 MiB │  18.4 MiB │  0 B 
    asset │   1.5 MiB │   1.5 MiB │  0 B │   1.5 MiB │   1.5 MiB │  0 B 
    other │    87 KiB │    87 KiB │ +2 B │ 161.5 KiB │ 161.5 KiB │  0 B 
──────────┼───────────┼───────────┼──────┼───────────┼───────────┼──────
    total │  12.2 MiB │  12.2 MiB │ +2 B │  25.8 MiB │  25.8 MiB │  0 B 

 DEX     │ old   │ new   │ diff      
─────────┼───────┼───────┼───────────
   files │     1 │     1 │ 0         
 strings │ 21686 │ 21686 │ 0 (+0 -0) 
   types │  6869 │  6869 │ 0 (+0 -0) 
 classes │  5634 │  5634 │ 0 (+0 -0) 
 methods │ 31447 │ 31447 │ 0 (+0 -0) 
  fields │ 18315 │ 18315 │ 0 (+0 -0) 

 ARSC    │ old  │ new  │ diff 
─────────┼──────┼──────┼──────
 configs │  164 │  164 │  0   
 entries │ 3404 │ 3404 │  0
APK
   compressed    │  uncompressed   │                     
──────────┬──────┼──────────┬──────┤                     
 size     │ diff │ size     │ diff │ path                
──────────┼──────┼──────────┼──────┼─────────────────────
  1.2 KiB │ +3 B │  1.2 KiB │  0 B │ ∆ META-INF/CERT.RSA 
 29.1 KiB │ -1 B │   64 KiB │  0 B │ ∆ META-INF/CERT.SF  
──────────┼──────┼──────────┼──────┼─────────────────────
 30.2 KiB │ +2 B │ 65.2 KiB │  0 B │ (total)

Copy link
Collaborator

@tillh-stripe tillh-stripe left a comment

Choose a reason for hiding this comment

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

Love the simplification! Only a few small comments.

)
}

sealed interface FetchCondition {
fun check(response: SynchronizeSessionResponse): Boolean
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: The naming doesn’t tell what the result indicates. Can we rename this to shouldForceRefresh or shouldLoadFromBackend or something like that?

This also makes it easier to understand at the call site:

cachedSync.takeUnless { shouldLoadFromBackend(it) }

Comment on lines 36 to 40
data object IfMissing : FetchCondition {
override fun check(response: SynchronizeSessionResponse): Boolean {
return false
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Should we rename this NoFetch or something along those lines? The case itself doesn’t have any “reload if missing” logic in it.

): SynchronizeSessionResponse = mutex.withLock {
val financialConnectionsRequest =
synchronizeRequest(
return when (val cachedSync = cachedSynchronizeSessionResponse) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: We can make this a bit more succinct.

val cachedSync = cachedSynchronizeSessionResponse?.takeUnless(fetchCondition)
return cachedSync ?: synchronize(applicationId, clientSecret)

Comment on lines 234 to 235
// TODO REMOVE BEFORE MERGING INTEGRATION BRANCH
"forced_authflow_version" to "v3",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we remove this now? 🤔

…muvi/unify-syncs

# Conflicts:
#	financial-connections/src/test/java/com/stripe/android/financialconnections/features/success/SuccessViewModelTest.kt
@carlosmuvi-stripe carlosmuvi-stripe marked this pull request as ready for review May 9, 2024 21:27
@carlosmuvi-stripe carlosmuvi-stripe requested review from a team as code owners May 9, 2024 21:27
@@ -74,7 +74,7 @@ internal class SuccessViewModelTest {
activeAuthSession = ApiKeyFixtures.authorizationSession(),
activeInstitution = ApiKeyFixtures.institution()
)
whenever(getCachedAccounts()).thenReturn(accounts.data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not necessary in this pull request, but a good illustration of why we want fakes instead of mocks. We should get in the habit of modeling our use cases as interfaces and then injecting a fake in the test with some hard-coded data.

@carlosmuvi-stripe carlosmuvi-stripe enabled auto-merge (squash) May 9, 2024 21:34
@carlosmuvi-stripe carlosmuvi-stripe merged commit 6e68fac into master May 9, 2024
12 checks passed
@carlosmuvi-stripe carlosmuvi-stripe deleted the carlosmuvi/unify-syncs branch May 9, 2024 23:05
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.

None yet

2 participants