-
Notifications
You must be signed in to change notification settings - Fork 16
Initial implementation of the flag caching and defaults #19
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
Merged
matthewelwell
merged 30 commits into
Flagsmith:main
from
foresightmobile:feature/cache-flags
Jul 13, 2023
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
710122b
Compiles and test, need to add some tests then get it into a sample app
gazreese 104b318
Should be testing but not getting the errors through Fuse so can't ru…
gazreese fbddca4
Probably gone as far as I can with 2.x fuel, let's try the 3.x
gazreese 2d31deb
Move to Retrofit - seems to be going well so far, test runs
gazreese 5e259b5
Tidying up, setTrait test not working
gazreese 76942b1
All the tests are passing so will finish the retrofit migration
gazreese 70d21e2
Updated some of the logic and added setTraits
gazreese 049a0f6
Checkpoint commit before trying generic converter
gazreese e555685
Generics working fine
gazreese 297642d
All passing for flags and such with the new generic caching
gazreese 90622ae
Mostly swapped to Retrofit, now need to do the analytics
gazreese 9536809
Analytics now over to retrofit
gazreese 817b73c
Add caching for the getFlags endpoint
gazreese 7e6d429
Get rid of the last of Fuel
gazreese ef4674b
Another clear-out and all working fine on the tests
gazreese 3b64e5f
Now using Retrofit cache, remove the old stuff
gazreese 46ee338
Now just using HTTP caching
gazreese dd6c7ca
Delete the old caching logic
gazreese a9a74f8
Finishing off, should be done for defaults and caching
gazreese ffb3cc6
Remove unneeded todo
gazreese 83d058d
Remove some more code
gazreese 94f8167
Move cache configuration to its own data class
gazreese b480ea4
Tidy up the cache config and the tests
gazreese a56afe0
Update the comments
gazreese 6d87253
Now covers the caching tests
gazreese 790ce47
Tidy up some more of the tests
gazreese 3aa02ed
Some more tidying up
gazreese a5200d7
Default to caching disabled
gazreese cb1542a
Last few PR comments
gazreese 340d4f3
Split the read and write timeout for HTTP
gazreese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -15,3 +15,4 @@ build | |
local.properties | ||
|
||
.idea | ||
FlagsmithClient/cache |
This file contains hidden or 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
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
FlagsmithClient/src/main/java/com/flagsmith/FlagsmithCacheConfig.kt
This file contains hidden or 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,7 @@ | ||
package com.flagsmith | ||
|
||
data class FlagsmithCacheConfig ( | ||
val enableCache: Boolean = false, | ||
val cacheTTLSeconds: Long = 3600L, // Default to 1 hour | ||
val cacheSize: Long = 10L * 1024L * 1024L, // 10 MB | ||
) |
9 changes: 0 additions & 9 deletions
9
FlagsmithClient/src/main/java/com/flagsmith/endpoints/FlagsEndpoint.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
9 changes: 0 additions & 9 deletions
9
FlagsmithClient/src/main/java/com/flagsmith/entities/IdentityFlagsAndTraits.kt
This file contains hidden or 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
This file contains hidden or 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
17 changes: 0 additions & 17 deletions
17
FlagsmithClient/src/main/java/com/flagsmith/internal/Deserializer.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
45 changes: 0 additions & 45 deletions
45
FlagsmithClient/src/main/java/com/flagsmith/internal/FlagsmithClient.kt
This file was deleted.
Oops, something went wrong.
104 changes: 104 additions & 0 deletions
104
FlagsmithClient/src/main/java/com/flagsmith/internal/FlagsmithRetrofitService.kt
This file contains hidden or 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,104 @@ | ||
package com.flagsmith.internal; | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import com.flagsmith.FlagsmithCacheConfig | ||
import com.flagsmith.entities.Flag | ||
import com.flagsmith.entities.IdentityFlagsAndTraits | ||
import com.flagsmith.entities.TraitWithIdentity | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import retrofit2.* | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.POST | ||
import retrofit2.http.Query | ||
|
||
interface FlagsmithRetrofitService { | ||
|
||
@GET("identities/") | ||
fun getIdentityFlagsAndTraits(@Query("identity") identity: String) : Call<IdentityFlagsAndTraits> | ||
|
||
@GET("flags/") | ||
fun getFlags() : Call<List<Flag>> | ||
|
||
@POST("traits/") | ||
fun postTraits(@Body trait: TraitWithIdentity) : Call<TraitWithIdentity> | ||
|
||
@POST("analytics/flags/") | ||
fun postAnalytics(@Body eventMap: Map<String, Int?>) : Call<Unit> | ||
|
||
companion object { | ||
fun create( | ||
baseUrl: String, | ||
environmentKey: String, | ||
context: Context?, | ||
cacheConfig: FlagsmithCacheConfig, | ||
requestTimeoutSeconds: Long, | ||
readTimeoutSeconds: Long, | ||
writeTimeoutSeconds: Long, | ||
): FlagsmithRetrofitService { | ||
fun cacheControlInterceptor(): Interceptor { | ||
return Interceptor { chain -> | ||
val response = chain.proceed(chain.request()) | ||
response.newBuilder() | ||
.header("Cache-Control", "public, max-age=${cacheConfig.cacheTTLSeconds}") | ||
.removeHeader("Pragma") | ||
.build() | ||
} | ||
} | ||
|
||
fun envKeyInterceptor(environmentKey: String): Interceptor { | ||
return Interceptor { chain -> | ||
val request = chain.request().newBuilder() | ||
.addHeader("X-environment-key", environmentKey) | ||
.build() | ||
chain.proceed(request) | ||
} | ||
} | ||
|
||
val client = OkHttpClient.Builder() | ||
.addInterceptor(envKeyInterceptor(environmentKey)) | ||
.let { if (cacheConfig.enableCache) it.addNetworkInterceptor(cacheControlInterceptor()) else it } | ||
.callTimeout(requestTimeoutSeconds, java.util.concurrent.TimeUnit.SECONDS) | ||
.readTimeout(readTimeoutSeconds, java.util.concurrent.TimeUnit.SECONDS) | ||
.writeTimeout(writeTimeoutSeconds, java.util.concurrent.TimeUnit.SECONDS) | ||
.cache(if (context != null && cacheConfig.enableCache) okhttp3.Cache(context.cacheDir, cacheConfig.cacheSize) else null) | ||
.build() | ||
|
||
val retrofit = Retrofit.Builder() | ||
.baseUrl(baseUrl) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.client(client) | ||
.build() | ||
|
||
return retrofit.create(FlagsmithRetrofitService::class.java) | ||
} | ||
} | ||
} | ||
|
||
// Convert a Retrofit Call to a standard Kotlin Result by extending the Call class | ||
// This avoids having to use the suspend keyword in the FlagsmithClient to break the API | ||
// And also avoids a lot of code duplication | ||
fun <T> Call<T>.enqueueWithResult(defaults: T? = null, result: (Result<T>) -> Unit) { | ||
this.enqueue(object : Callback<T> { | ||
override fun onResponse(call: Call<T>, response: Response<T>) { | ||
if (response.isSuccessful && response.body() != null) { | ||
result(Result.success(response.body()!!)) | ||
} else { | ||
onFailure(call, HttpException(response)) | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<T>, t: Throwable) { | ||
// If we've got defaults to return, return them | ||
if (defaults != null) { | ||
result(Result.success(defaults)) | ||
} else { | ||
result(Result.failure(t)) | ||
} | ||
} | ||
}) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.