Skip to content

Commit

Permalink
Add ApolloClient.Builder(ApolloHttpCache) (#5638) (#5640)
Browse files Browse the repository at this point in the history
* add CachingHttpInterceptor(ApolloHttpCache)

* Add ApolloClient.Builder(ApolloHttpCache)
  • Loading branch information
martinbonnin committed Feb 22, 2024
1 parent 6a4fdc2 commit 3acfc47
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions libraries/apollo-http-cache/api/apollo-http-cache.api
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class com/apollographql/apollo3/cache/http/DiskLruHttpCache$Compani
}

public final class com/apollographql/apollo3/cache/http/HttpCache {
public static final fun configureApolloClientBuilder (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/http/ApolloHttpCache;)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun configureApolloClientBuilder (Lcom/apollographql/apollo3/ApolloClient$Builder;Ljava/io/File;J)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun getHttpCache (Lcom/apollographql/apollo3/ApolloClient;)Lcom/apollographql/apollo3/cache/http/ApolloHttpCache;
public static final fun httpDoNotStore (Lcom/apollographql/apollo3/api/MutableExecutionOptions;Z)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import java.io.File
import java.time.Instant
import java.time.format.DateTimeParseException

class CachingHttpInterceptor(
directory: File,
maxSize: Long,
fileSystem: FileSystem = FileSystem.SYSTEM,
class CachingHttpInterceptor internal constructor(
private val lruHttpCache: ApolloHttpCache
) : HttpInterceptor {
private val lruHttpCache = DiskLruHttpCache(fileSystem, directory, maxSize)

constructor(
directory: File,
maxSize: Long,
fileSystem: FileSystem = FileSystem.SYSTEM,
): this(DiskLruHttpCache(fileSystem, directory, maxSize))

val cache: ApolloHttpCache = lruHttpCache

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import okio.FileSystem
import java.io.File

enum class HttpFetchPolicy {
Expand Down Expand Up @@ -72,10 +73,15 @@ fun ApolloClient.Builder.httpCache(
directory: File,
maxSize: Long,
): ApolloClient.Builder {
val cachingHttpInterceptor = CachingHttpInterceptor(
directory = directory,
maxSize = maxSize,
)
return httpCache(DiskLruHttpCache(FileSystem.SYSTEM, directory, maxSize))
}

@JvmName("configureApolloClientBuilder")
fun ApolloClient.Builder.httpCache(
apolloHttpCache: ApolloHttpCache,
): ApolloClient.Builder {
val cachingHttpInterceptor = CachingHttpInterceptor(apolloHttpCache)

val apolloRequestToCacheKey = mutableMapOf<String, String>()
return addHttpInterceptor(object : HttpInterceptor {
override suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain): HttpResponse {
Expand Down

0 comments on commit 3acfc47

Please sign in to comment.