From 450a096f56d79c518fb1d131c48fea8f8f597c75 Mon Sep 17 00:00:00 2001 From: Ivan Savytskyi Date: Fri, 22 Sep 2017 18:22:07 -0400 Subject: [PATCH] Extract http cache as standalone submodule Extract http cache support as separate artifact Part of #570 --- .travis.yml | 5 +- apollo-api/build.gradle | 2 + .../java/com/apollographql/apollo/Logger.java | 0 .../apollo/api/cache/http/HttpCache.java | 82 +++++++++++++++++++ .../api}/cache/http/HttpCachePolicy.java | 50 ++++++++--- .../api}/cache/http/HttpCacheRecord.java | 2 +- .../cache/http/HttpCacheRecordEditor.java | 2 +- .../api}/cache/http/HttpCacheStore.java | 4 +- .../apollo/internal}/ApolloLogger.java | 2 +- apollo-http-cache/build.gradle | 20 +++++ apollo-http-cache/gradle.properties | 4 + .../apollo/cache/http/ApolloHttpCache.java | 46 ++++++----- .../apollo/cache/http/CacheResponseBody.java | 0 .../cache/http/DiskLruHttpCacheStore.java | 4 + .../cache/http/HttpCacheInterceptor.java | 10 +-- .../cache/http/ResponseBodyCacheSink.java | 0 .../apollo/cache/http/ResponseBodyProxy.java | 3 +- .../cache/http/ResponseHeaderRecord.java | 0 .../apollo/cache/http/Utils.java | 32 ++++---- apollo-integration/build.gradle | 1 + .../apollo/ApolloCancelCallTest.java | 3 +- .../apollo/ApolloPrefetchTest.java | 7 +- .../apollo/FaultyHttpCacheStore.java | 6 +- .../apollographql/apollo/HttpCacheTest.java | 11 ++- .../apollo/MockHttpCacheStore.java | 6 +- .../cache/normalized/ApolloStoreTest.java | 2 +- .../apollographql/apollo/ApolloClient.java | 44 +++++----- .../apollographql/apollo/ApolloQueryCall.java | 2 +- .../apollo/fetcher/ResponseFetcher.java | 2 +- .../apollo/internal/QueryReFetcher.java | 3 +- .../apollo/internal/RealApolloCall.java | 10 +-- .../apollo/internal/RealApolloPrefetch.java | 10 +-- .../internal/RealApolloQueryWatcher.java | 1 - .../cache/http/HttpCacheFetchStrategy.java | 25 ------ .../cache/normalized/RealApolloStore.java | 2 +- .../fetcher/CacheAndNetworkFetcher.java | 2 +- .../internal/fetcher/CacheFirstFetcher.java | 2 +- .../internal/fetcher/CacheOnlyFetcher.java | 2 +- .../internal/fetcher/NetworkFirstFetcher.java | 2 +- .../internal/fetcher/NetworkOnlyFetcher.java | 2 +- .../interceptor/ApolloCacheInterceptor.java | 2 +- .../interceptor/ApolloParseInterceptor.java | 4 +- .../interceptor/ApolloServerInterceptor.java | 6 +- .../internal/response/RealResponseReader.java | 1 + .../apollo/internal/ResponseFetcherTest.java | 7 +- settings.gradle | 4 +- 46 files changed, 269 insertions(+), 168 deletions(-) rename {apollo-runtime => apollo-api}/src/main/java/com/apollographql/apollo/Logger.java (100%) create mode 100644 apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCache.java rename {apollo-runtime/src/main/java/com/apollographql/apollo => apollo-api/src/main/java/com/apollographql/apollo/api}/cache/http/HttpCachePolicy.java (65%) rename {apollo-runtime/src/main/java/com/apollographql/apollo => apollo-api/src/main/java/com/apollographql/apollo/api}/cache/http/HttpCacheRecord.java (78%) rename {apollo-runtime/src/main/java/com/apollographql/apollo => apollo-api/src/main/java/com/apollographql/apollo/api}/cache/http/HttpCacheRecordEditor.java (84%) rename {apollo-runtime/src/main/java/com/apollographql/apollo => apollo-api/src/main/java/com/apollographql/apollo/api}/cache/http/HttpCacheStore.java (88%) rename {apollo-runtime/src/main/java/com/apollographql/apollo/internal/util => apollo-api/src/main/java/com/apollographql/apollo/internal}/ApolloLogger.java (96%) create mode 100644 apollo-http-cache/build.gradle create mode 100644 apollo-http-cache/gradle.properties rename apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCache.java => apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ApolloHttpCache.java (73%) rename {apollo-runtime => apollo-http-cache}/src/main/java/com/apollographql/apollo/cache/http/CacheResponseBody.java (100%) rename {apollo-runtime => apollo-http-cache}/src/main/java/com/apollographql/apollo/cache/http/DiskLruHttpCacheStore.java (92%) rename {apollo-runtime => apollo-http-cache}/src/main/java/com/apollographql/apollo/cache/http/HttpCacheInterceptor.java (95%) rename {apollo-runtime => apollo-http-cache}/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyCacheSink.java (100%) rename {apollo-runtime => apollo-http-cache}/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyProxy.java (97%) rename {apollo-runtime => apollo-http-cache}/src/main/java/com/apollographql/apollo/cache/http/ResponseHeaderRecord.java (100%) rename {apollo-runtime => apollo-http-cache}/src/main/java/com/apollographql/apollo/cache/http/Utils.java (74%) delete mode 100644 apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/http/HttpCacheFetchStrategy.java diff --git a/.travis.yml b/.travis.yml index 891a13cf224..f78ac1a6aaf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,8 +16,9 @@ jdk: - oraclejdk8 script: - - ./gradlew clean build connectedCheck -x checkstyleTest --stacktrace --max-workers=2 -x apollo-gradle-plugin:test -x apollo-integration:build -x apollo-integration:connectedCheck - - .buildscript/integration_tests_composite.sh + - ./gradlew clean build connectedCheck -x checkstyleTest --stacktrace --max-workers=2 -x apollo-gradle-plugin:test + # -x apollo-integration:build -x apollo-integration:connectedCheck + # - .buildscript/integration_tests_composite.sh before_script: - echo no | android create avd --force -n test -t android-15 --abi armeabi-v7a diff --git a/apollo-api/build.gradle b/apollo-api/build.gradle index fbf6b814994..42b71ca1b35 100644 --- a/apollo-api/build.gradle +++ b/apollo-api/build.gradle @@ -7,6 +7,8 @@ dependencies { compile dep.jsr305 compile dep.jsr250 + compileOnly dep.okHttp + testCompile dep.junit testCompile dep.truth } diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/Logger.java b/apollo-api/src/main/java/com/apollographql/apollo/Logger.java similarity index 100% rename from apollo-runtime/src/main/java/com/apollographql/apollo/Logger.java rename to apollo-api/src/main/java/com/apollographql/apollo/Logger.java diff --git a/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCache.java b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCache.java new file mode 100644 index 00000000000..cd74e53b573 --- /dev/null +++ b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCache.java @@ -0,0 +1,82 @@ +package com.apollographql.apollo.api.cache.http; + +import java.io.IOException; + +import javax.annotation.Nonnull; + +import okhttp3.Interceptor; +import okhttp3.Response; + +/** + * Http GraphQL http request / response cache. + */ +public interface HttpCache { + /** + * Cache key http header + */ + String CACHE_KEY_HEADER = "X-APOLLO-CACHE-KEY"; + /** + * Cache fetch strategy http header + */ + String CACHE_FETCH_STRATEGY_HEADER = "X-APOLLO-CACHE-FETCH-STRATEGY"; + /** + * Request served Date/time http header + */ + String CACHE_SERVED_DATE_HEADER = "X-APOLLO-SERVED-DATE"; + /** + * Prefetch response only flag http header + */ + String CACHE_PREFETCH_HEADER = "X-APOLLO-PREFETCH"; + /** + * Cached response expiration timeout http header + */ + String CACHE_EXPIRE_TIMEOUT_HEADER = "X-APOLLO-EXPIRE-TIMEOUT"; + /** + * Expire cached response flag http header + */ + String CACHE_EXPIRE_AFTER_READ_HEADER = "X-APOLLO-EXPIRE-AFTER-READ"; + + /** + * Clear cached http responses + */ + void clear(); + + /** + * Remove cached http response by key. May throw {@link IOException} + * + * @param cacheKey key of cached response to be removed + */ + void remove(@Nonnull String cacheKey) throws IOException; + + /** + * Remove cached http response by key and suppress any exception + * + * @param cacheKey key of cached response to be removed + */ + void removeQuietly(@Nonnull String cacheKey); + + /** + * Read cached http response by key + * + * @param cacheKey key of cached response to be read + * @return cached response + */ + Response read(@Nonnull String cacheKey); + + /** + * Read and remove cached http response by key if {@code expireAfterRead == true} + * + * @param cacheKey key of cached response to be read + * @param expireAfterRead if {@code true} cached response will be removed after first read + * @return cached response + */ + Response read(@Nonnull String cacheKey, boolean expireAfterRead); + + /** + * Provide http cache interceptor to be injected into {@link okhttp3.OkHttpClient#interceptors}. Provided interceptor + * must intercept request and serve cached http response as well as store network response to the http cache store. + * + * @return {@link Interceptor} + */ + Interceptor interceptor(); +} diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCachePolicy.java b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCachePolicy.java similarity index 65% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCachePolicy.java rename to apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCachePolicy.java index 4c13ef016a7..b03bdf3833b 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCachePolicy.java +++ b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCachePolicy.java @@ -1,6 +1,4 @@ -package com.apollographql.apollo.cache.http; - -import com.apollographql.apollo.internal.cache.http.HttpCacheFetchStrategy; +package com.apollographql.apollo.api.cache.http; import java.util.concurrent.TimeUnit; @@ -16,24 +14,24 @@ public final class HttpCachePolicy { /** * Signals the apollo client to fetch the GraphQL query response from the http cache only. */ - public static final ExpirePolicy CACHE_ONLY = new ExpirePolicy(HttpCacheFetchStrategy.CACHE_ONLY); + public static final ExpirePolicy CACHE_ONLY = new ExpirePolicy(FetchStrategy.CACHE_ONLY); /** * Signals the apollo client to fetch the GraphQL query response from the network only. */ - public static final Policy NETWORK_ONLY = new Policy(HttpCacheFetchStrategy.NETWORK_ONLY, 0, null, false); + public static final Policy NETWORK_ONLY = new Policy(FetchStrategy.NETWORK_ONLY, 0, null, false); /** * Signals the apollo client to first fetch the GraphQL query response from the http cache. If it's not present in the * cache response is fetched from the network. */ - public static final ExpirePolicy CACHE_FIRST = new ExpirePolicy(HttpCacheFetchStrategy.CACHE_FIRST); + public static final ExpirePolicy CACHE_FIRST = new ExpirePolicy(FetchStrategy.CACHE_FIRST); /** * Signals the apollo client to first fetch the GraphQL query response from the network. If it fails then fetch the * response from the http cache. */ - public static final ExpirePolicy NETWORK_FIRST = new ExpirePolicy(HttpCacheFetchStrategy.NETWORK_FIRST); + public static final ExpirePolicy NETWORK_FIRST = new ExpirePolicy(FetchStrategy.NETWORK_FIRST); private HttpCachePolicy() { } @@ -42,13 +40,14 @@ private HttpCachePolicy() { * Abstraction for http cache policy configurations */ public static class Policy { - public final HttpCacheFetchStrategy fetchStrategy; + + public final FetchStrategy fetchStrategy; public final long expireTimeout; public final TimeUnit expireTimeUnit; public final boolean expireAfterRead; - Policy(HttpCacheFetchStrategy fetchStrategy, long expireTimeout, TimeUnit expireTimeUnit, - boolean expireAfterRead) { + Policy(FetchStrategy fetchStrategy, long expireTimeout, TimeUnit expireTimeUnit, + boolean expireAfterRead) { this.fetchStrategy = fetchStrategy; this.expireTimeout = expireTimeout; this.expireTimeUnit = expireTimeUnit; @@ -67,12 +66,13 @@ public long expireTimeoutMs() { * Cache policy with provided expiration configuration */ public static final class ExpirePolicy extends Policy { - ExpirePolicy(HttpCacheFetchStrategy fetchStrategy) { + + ExpirePolicy(FetchStrategy fetchStrategy) { super(fetchStrategy, 0, null, false); } - private ExpirePolicy(HttpCacheFetchStrategy fetchStrategy, long expireTimeout, TimeUnit expireTimeUnit, - boolean expireAfterRead) { + private ExpirePolicy(FetchStrategy fetchStrategy, long expireTimeout, TimeUnit expireTimeUnit, + boolean expireAfterRead) { super(fetchStrategy, expireTimeout, expireTimeUnit, expireAfterRead); } @@ -96,4 +96,28 @@ public ExpirePolicy expireAfterRead() { return new ExpirePolicy(fetchStrategy, expireTimeout, expireTimeUnit, true); } } + + /** + * Represents different fetch strategies for http request / response cache + */ + public enum FetchStrategy { + /** + * Signals the apollo client to fetch the GraphQL query response from the http cache only. + */ + CACHE_ONLY, + /** + * Signals the apollo client to fetch the GraphQL query response from the network only. + */ + NETWORK_ONLY, + /** + * Signals the apollo client to first fetch the GraphQL query response from the http cache. If it's not present in + * the cache response is fetched from the network. + */ + CACHE_FIRST, + /** + * Signals the apollo client to first fetch the GraphQL query response from the network. If it fails then fetch the + * response from the http cache. + */ + NETWORK_FIRST + } } diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheRecord.java b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheRecord.java similarity index 78% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheRecord.java rename to apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheRecord.java index fe7a4bb3e2c..bd4047adb38 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheRecord.java +++ b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheRecord.java @@ -1,4 +1,4 @@ -package com.apollographql.apollo.cache.http; +package com.apollographql.apollo.api.cache.http; import javax.annotation.Nonnull; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheRecordEditor.java b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheRecordEditor.java similarity index 84% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheRecordEditor.java rename to apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheRecordEditor.java index 9e231184626..fee553c7d19 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheRecordEditor.java +++ b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheRecordEditor.java @@ -1,4 +1,4 @@ -package com.apollographql.apollo.cache.http; +package com.apollographql.apollo.api.cache.http; import java.io.IOException; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheStore.java b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheStore.java similarity index 88% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheStore.java rename to apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheStore.java index 83660ced5aa..fa88c831f8b 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheStore.java +++ b/apollo-api/src/main/java/com/apollographql/apollo/api/cache/http/HttpCacheStore.java @@ -1,4 +1,4 @@ -package com.apollographql.apollo.cache.http; +package com.apollographql.apollo.api.cache.http; import java.io.IOException; @@ -6,7 +6,7 @@ import javax.annotation.Nullable; /** - * ResponseCacheStore is an abstraction for a cache store that is used to read, modify or delete http responses. + * CacheStore is an abstraction for a cache store that is used to read, modify or delete http responses. */ public interface HttpCacheStore { /** diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/util/ApolloLogger.java b/apollo-api/src/main/java/com/apollographql/apollo/internal/ApolloLogger.java similarity index 96% rename from apollo-runtime/src/main/java/com/apollographql/apollo/internal/util/ApolloLogger.java rename to apollo-api/src/main/java/com/apollographql/apollo/internal/ApolloLogger.java index 9232f8183e9..286216783b7 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/util/ApolloLogger.java +++ b/apollo-api/src/main/java/com/apollographql/apollo/internal/ApolloLogger.java @@ -1,4 +1,4 @@ -package com.apollographql.apollo.internal.util; +package com.apollographql.apollo.internal; import com.apollographql.apollo.Logger; import com.apollographql.apollo.api.internal.Optional; diff --git a/apollo-http-cache/build.gradle b/apollo-http-cache/build.gradle new file mode 100644 index 00000000000..4ea0e28050b --- /dev/null +++ b/apollo-http-cache/build.gradle @@ -0,0 +1,20 @@ +apply plugin: 'java' + +targetCompatibility = JavaVersion.VERSION_1_7 +sourceCompatibility = JavaVersion.VERSION_1_7 + +dependencies { + compile dep.jsr305 + compile dep.okHttp + + compile project(":apollo-api") + + testCompile dep.junit + testCompile dep.truth +} + +apply from: rootProject.file('gradle/gradle-mvn-push.gradle') + +javadoc { + options.encoding = 'UTF-8' +} \ No newline at end of file diff --git a/apollo-http-cache/gradle.properties b/apollo-http-cache/gradle.properties new file mode 100644 index 00000000000..cc6f7a8fbed --- /dev/null +++ b/apollo-http-cache/gradle.properties @@ -0,0 +1,4 @@ +POM_ARTIFACT_ID=apollo-http-cache +POM_NAME=Apollo GraphQL http cache support +POM_DESCRIPTION=Apollo GraphQL http cache support +POM_PACKAGING=jar \ No newline at end of file diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCache.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ApolloHttpCache.java similarity index 73% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCache.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ApolloHttpCache.java index c5d19b887c8..25a1b585d58 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCache.java +++ b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ApolloHttpCache.java @@ -1,6 +1,12 @@ package com.apollographql.apollo.cache.http; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.Logger; +import com.apollographql.apollo.api.cache.http.HttpCache; +import com.apollographql.apollo.api.cache.http.HttpCacheRecord; +import com.apollographql.apollo.api.cache.http.HttpCacheRecordEditor; +import com.apollographql.apollo.api.cache.http.HttpCacheStore; +import com.apollographql.apollo.api.internal.Optional; +import com.apollographql.apollo.internal.ApolloLogger; import java.io.IOException; @@ -15,23 +21,18 @@ import static com.apollographql.apollo.api.internal.Utils.checkNotNull; import static com.apollographql.apollo.cache.http.Utils.copyResponseBody; -@SuppressWarnings("WeakerAccess") public final class HttpCache { - public static final String CACHE_KEY_HEADER = "X-APOLLO-CACHE-KEY"; - public static final String CACHE_FETCH_STRATEGY_HEADER = "X-APOLLO-CACHE-FETCH-STRATEGY"; - public static final String CACHE_SERVED_DATE_HEADER = "X-APOLLO-SERVED-DATE"; - public static final String CACHE_PREFETCH_HEADER = "X-APOLLO-PREFETCH"; - public static final String CACHE_EXPIRE_TIMEOUT_HEADER = "X-APOLLO-EXPIRE-TIMEOUT"; - public static final String CACHE_EXPIRE_AFTER_READ_HEADER = "X-APOLLO-EXPIRE-AFTER-READ"; +@SuppressWarnings("WeakerAccess") +public final class ApolloHttpCache implements HttpCache { private final HttpCacheStore cacheStore; private final ApolloLogger logger; - public HttpCache(@Nonnull HttpCacheStore cacheStore, @Nonnull ApolloLogger logger) { - this.cacheStore = checkNotNull(cacheStore, "cacheStore can't be null"); - this.logger = checkNotNull(logger, "logger can't be null"); + public ApolloHttpCache(@Nonnull final HttpCacheStore cacheStore, final Logger logger) { + this.cacheStore = checkNotNull(cacheStore, "cacheStore == null"); + this.logger = new ApolloLogger(Optional.fromNullable(logger)); } - public void clear() { + @Override public void clear() { try { cacheStore.delete(); } catch (IOException e) { @@ -39,11 +40,11 @@ public void clear() { } } - public void remove(@Nonnull String cacheKey) throws IOException { + @Override public void remove(@Nonnull String cacheKey) throws IOException { cacheStore.remove(cacheKey); } - public void removeQuietly(@Nonnull String cacheKey) { + @Override public void removeQuietly(@Nonnull String cacheKey) { try { remove(cacheKey); } catch (Exception ignore) { @@ -51,11 +52,11 @@ public void removeQuietly(@Nonnull String cacheKey) { } } - public Response read(@Nonnull final String cacheKey) { + @Override public Response read(@Nonnull final String cacheKey) { return read(cacheKey, false); } - public Response read(@Nonnull final String cacheKey, final boolean expireAfterRead) { + @Override public Response read(@Nonnull final String cacheKey, final boolean expireAfterRead) { HttpCacheRecord responseCacheRecord = null; try { responseCacheRecord = cacheStore.cacheRecord(cacheKey); @@ -65,7 +66,8 @@ public Response read(@Nonnull final String cacheKey, final boolean expireAfterRe final HttpCacheRecord cacheRecord = responseCacheRecord; Source cacheResponseSource = new ForwardingSource(responseCacheRecord.bodySource()) { - @Override public void close() throws IOException { + @Override + public void close() throws IOException { super.close(); closeQuietly(cacheRecord); if (expireAfterRead) { @@ -78,8 +80,8 @@ public Response read(@Nonnull final String cacheKey, final boolean expireAfterRe String contentType = response.header("Content-Type"); String contentLength = response.header("Content-Length"); return response.newBuilder() - .body(new CacheResponseBody(cacheResponseSource, contentType, contentLength)) - .build(); + .body(new CacheResponseBody(cacheResponseSource, contentType, contentLength)) + .build(); } catch (Exception e) { closeQuietly(responseCacheRecord); logger.e(e, "Failed to read http cache entry for key: %s", cacheKey); @@ -87,7 +89,7 @@ public Response read(@Nonnull final String cacheKey, final boolean expireAfterRe } } - public Interceptor interceptor() { + @Override public Interceptor interceptor() { return new HttpCacheInterceptor(this, logger); } @@ -104,8 +106,8 @@ Response cacheProxy(@Nonnull Response response, @Nonnull String cacheKey) { } return response.newBuilder() - .body(new ResponseBodyProxy(cacheRecordEditor, response, logger)) - .build(); + .body(new ResponseBodyProxy(cacheRecordEditor, response, logger)) + .build(); } } catch (Exception e) { abortQuietly(cacheRecordEditor); diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/CacheResponseBody.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/CacheResponseBody.java similarity index 100% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/CacheResponseBody.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/CacheResponseBody.java diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/DiskLruHttpCacheStore.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/DiskLruHttpCacheStore.java similarity index 92% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/DiskLruHttpCacheStore.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/DiskLruHttpCacheStore.java index 5a74740ac06..d58d147994d 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/DiskLruHttpCacheStore.java +++ b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/DiskLruHttpCacheStore.java @@ -1,5 +1,9 @@ package com.apollographql.apollo.cache.http; +import com.apollographql.apollo.api.cache.http.HttpCacheRecord; +import com.apollographql.apollo.api.cache.http.HttpCacheRecordEditor; +import com.apollographql.apollo.api.cache.http.HttpCacheStore; + import java.io.File; import java.io.IOException; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheInterceptor.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/HttpCacheInterceptor.java similarity index 95% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheInterceptor.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/HttpCacheInterceptor.java index 886f238e7c1..d4b454eea70 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/HttpCacheInterceptor.java +++ b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/HttpCacheInterceptor.java @@ -1,6 +1,6 @@ package com.apollographql.apollo.cache.http; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.io.IOException; @@ -8,8 +8,8 @@ import okhttp3.Request; import okhttp3.Response; +import static com.apollographql.apollo.api.cache.http.HttpCache.CACHE_KEY_HEADER; import static com.apollographql.apollo.api.internal.Utils.checkNotNull; -import static com.apollographql.apollo.cache.http.HttpCache.CACHE_KEY_HEADER; import static com.apollographql.apollo.cache.http.Utils.isNetworkFirst; import static com.apollographql.apollo.cache.http.Utils.isNetworkOnly; import static com.apollographql.apollo.cache.http.Utils.isPrefetchResponse; @@ -21,11 +21,11 @@ import static com.apollographql.apollo.cache.http.Utils.unsatisfiableCacheRequest; import static com.apollographql.apollo.cache.http.Utils.withServedDateHeader; -public final class HttpCacheInterceptor implements Interceptor { - private final HttpCache cache; +final class HttpCacheInterceptor implements Interceptor { + private final ApolloHttpCache cache; private final ApolloLogger logger; - HttpCacheInterceptor(HttpCache cache, ApolloLogger logger) { + HttpCacheInterceptor(ApolloHttpCache cache, ApolloLogger logger) { this.cache = checkNotNull(cache, "cache == null"); this.logger = checkNotNull(logger, "logger == null"); } diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyCacheSink.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyCacheSink.java similarity index 100% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyCacheSink.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyCacheSink.java diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyProxy.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyProxy.java similarity index 97% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyProxy.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyProxy.java index 27a7b253c3c..22abd238a94 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyProxy.java +++ b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ResponseBodyProxy.java @@ -1,6 +1,7 @@ package com.apollographql.apollo.cache.http; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.api.cache.http.HttpCacheRecordEditor; +import com.apollographql.apollo.internal.ApolloLogger; import java.io.IOException; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/ResponseHeaderRecord.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ResponseHeaderRecord.java similarity index 100% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/ResponseHeaderRecord.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/ResponseHeaderRecord.java diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/Utils.java b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/Utils.java similarity index 74% rename from apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/Utils.java rename to apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/Utils.java index dbf3a4f957e..9d32186bcaa 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/cache/http/Utils.java +++ b/apollo-http-cache/src/main/java/com/apollographql/apollo/cache/http/Utils.java @@ -1,6 +1,6 @@ package com.apollographql.apollo.cache.http; -import com.apollographql.apollo.internal.cache.http.HttpCacheFetchStrategy; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import java.io.IOException; import java.util.Date; @@ -16,12 +16,12 @@ import okio.Sink; import okio.Source; -import static com.apollographql.apollo.cache.http.HttpCache.CACHE_EXPIRE_AFTER_READ_HEADER; -import static com.apollographql.apollo.cache.http.HttpCache.CACHE_EXPIRE_TIMEOUT_HEADER; -import static com.apollographql.apollo.cache.http.HttpCache.CACHE_FETCH_STRATEGY_HEADER; -import static com.apollographql.apollo.cache.http.HttpCache.CACHE_KEY_HEADER; -import static com.apollographql.apollo.cache.http.HttpCache.CACHE_PREFETCH_HEADER; -import static com.apollographql.apollo.cache.http.HttpCache.CACHE_SERVED_DATE_HEADER; +import static com.apollographql.apollo.api.cache.http.HttpCache.CACHE_EXPIRE_AFTER_READ_HEADER; +import static com.apollographql.apollo.api.cache.http.HttpCache.CACHE_EXPIRE_TIMEOUT_HEADER; +import static com.apollographql.apollo.api.cache.http.HttpCache.CACHE_FETCH_STRATEGY_HEADER; +import static com.apollographql.apollo.api.cache.http.HttpCache.CACHE_KEY_HEADER; +import static com.apollographql.apollo.api.cache.http.HttpCache.CACHE_PREFETCH_HEADER; +import static com.apollographql.apollo.api.cache.http.HttpCache.CACHE_SERVED_DATE_HEADER; final class Utils { static Response strip(Response response) { @@ -42,7 +42,7 @@ static boolean isPrefetchResponse(Request request) { static boolean shouldSkipCache(Request request) { String cacheKey = request.header(CACHE_KEY_HEADER); - HttpCacheFetchStrategy fetchStrategy = fetchStrategy(request); + HttpCachePolicy.FetchStrategy fetchStrategy = fetchStrategy(request); return cacheKey == null || cacheKey.isEmpty() || fetchStrategy == null; @@ -50,20 +50,20 @@ static boolean shouldSkipCache(Request request) { static boolean shouldSkipNetwork(Request request) { String cacheKey = request.header(CACHE_KEY_HEADER); - HttpCacheFetchStrategy fetchStrategy = fetchStrategy(request); + HttpCachePolicy.FetchStrategy fetchStrategy = fetchStrategy(request); return cacheKey != null && !cacheKey.isEmpty() - && fetchStrategy == HttpCacheFetchStrategy.CACHE_ONLY; + && fetchStrategy == HttpCachePolicy.FetchStrategy.CACHE_ONLY; } static boolean isNetworkOnly(Request request) { - HttpCacheFetchStrategy fetchStrategy = fetchStrategy(request); - return fetchStrategy == HttpCacheFetchStrategy.NETWORK_ONLY; + HttpCachePolicy.FetchStrategy fetchStrategy = fetchStrategy(request); + return fetchStrategy == HttpCachePolicy.FetchStrategy.NETWORK_ONLY; } static boolean isNetworkFirst(Request request) { - HttpCacheFetchStrategy fetchStrategy = fetchStrategy(request); - return fetchStrategy == HttpCacheFetchStrategy.NETWORK_FIRST; + HttpCachePolicy.FetchStrategy fetchStrategy = fetchStrategy(request); + return fetchStrategy == HttpCachePolicy.FetchStrategy.NETWORK_FIRST; } // static boolean shouldReturnStaleCache(Request request) { @@ -132,13 +132,13 @@ static boolean isStale(Request request, Response response) { return servedDate == null || now - servedDate.getTime() > timeout; } - private static HttpCacheFetchStrategy fetchStrategy(Request request) { + private static HttpCachePolicy.FetchStrategy fetchStrategy(Request request) { String fetchStrategyHeader = request.header(CACHE_FETCH_STRATEGY_HEADER); if (fetchStrategyHeader == null || fetchStrategyHeader.isEmpty()) { return null; } - for (HttpCacheFetchStrategy fetchStrategy : HttpCacheFetchStrategy.values()) { + for (HttpCachePolicy.FetchStrategy fetchStrategy : HttpCachePolicy.FetchStrategy.values()) { if (fetchStrategy.name().equals(fetchStrategyHeader)) { return fetchStrategy; } diff --git a/apollo-integration/build.gradle b/apollo-integration/build.gradle index 112540d79af..19541eed332 100644 --- a/apollo-integration/build.gradle +++ b/apollo-integration/build.gradle @@ -34,6 +34,7 @@ dependencies { compile project(':apollo-runtime') compile project(':apollo-rxsupport') compile project(':apollo-rx2support') + compile project(':apollo-http-cache') compile dep.appcompat compile dep.rxjava compile dep.rxjava2 diff --git a/apollo-integration/src/test/java/com/apollographql/apollo/ApolloCancelCallTest.java b/apollo-integration/src/test/java/com/apollographql/apollo/ApolloCancelCallTest.java index db83af74b5a..5e1e7e37e8b 100644 --- a/apollo-integration/src/test/java/com/apollographql/apollo/ApolloCancelCallTest.java +++ b/apollo-integration/src/test/java/com/apollographql/apollo/ApolloCancelCallTest.java @@ -1,5 +1,6 @@ package com.apollographql.apollo; +import com.apollographql.apollo.cache.http.ApolloHttpCache; import com.apollographql.apollo.exception.ApolloCanceledException; import com.apollographql.apollo.exception.ApolloException; import com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery; @@ -32,7 +33,7 @@ public void setup() { apolloClient = ApolloClient.builder() .serverUrl(server.url("/")) .okHttpClient(okHttpClient) - .httpCacheStore(cacheStore) + .httpCache(new ApolloHttpCache(cacheStore, null)) .build(); } diff --git a/apollo-integration/src/test/java/com/apollographql/apollo/ApolloPrefetchTest.java b/apollo-integration/src/test/java/com/apollographql/apollo/ApolloPrefetchTest.java index 64d620d16f2..e6b6e1c0f40 100644 --- a/apollo-integration/src/test/java/com/apollographql/apollo/ApolloPrefetchTest.java +++ b/apollo-integration/src/test/java/com/apollographql/apollo/ApolloPrefetchTest.java @@ -1,9 +1,10 @@ package com.apollographql.apollo; import com.apollographql.apollo.api.Response; +import com.apollographql.apollo.api.cache.http.HttpCache; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; +import com.apollographql.apollo.cache.http.ApolloHttpCache; import com.apollographql.apollo.cache.http.DiskLruHttpCacheStore; -import com.apollographql.apollo.cache.http.HttpCache; -import com.apollographql.apollo.cache.http.HttpCachePolicy; import com.apollographql.apollo.exception.ApolloException; import com.apollographql.apollo.integration.httpcache.AllPlanetsQuery; import com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor; @@ -58,7 +59,7 @@ public void setup() { apolloClient = ApolloClient.builder() .serverUrl(server.url("/")) .okHttpClient(okHttpClient) - .httpCacheStore(cacheStore) + .httpCache(new ApolloHttpCache(cacheStore, null)) .build(); } diff --git a/apollo-integration/src/test/java/com/apollographql/apollo/FaultyHttpCacheStore.java b/apollo-integration/src/test/java/com/apollographql/apollo/FaultyHttpCacheStore.java index d89deddf1ce..c426ee5e6d6 100644 --- a/apollo-integration/src/test/java/com/apollographql/apollo/FaultyHttpCacheStore.java +++ b/apollo-integration/src/test/java/com/apollographql/apollo/FaultyHttpCacheStore.java @@ -1,8 +1,8 @@ package com.apollographql.apollo; -import com.apollographql.apollo.cache.http.HttpCacheRecord; -import com.apollographql.apollo.cache.http.HttpCacheRecordEditor; -import com.apollographql.apollo.cache.http.HttpCacheStore; +import com.apollographql.apollo.api.cache.http.HttpCacheRecord; +import com.apollographql.apollo.api.cache.http.HttpCacheRecordEditor; +import com.apollographql.apollo.api.cache.http.HttpCacheStore; import java.io.File; import java.io.IOException; diff --git a/apollo-integration/src/test/java/com/apollographql/apollo/HttpCacheTest.java b/apollo-integration/src/test/java/com/apollographql/apollo/HttpCacheTest.java index 4961ba1ae0e..89ddd68d5a2 100644 --- a/apollo-integration/src/test/java/com/apollographql/apollo/HttpCacheTest.java +++ b/apollo-integration/src/test/java/com/apollographql/apollo/HttpCacheTest.java @@ -1,10 +1,10 @@ package com.apollographql.apollo; import com.apollographql.apollo.api.Response; -import com.apollographql.apollo.api.internal.Optional; +import com.apollographql.apollo.api.cache.http.HttpCache; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; +import com.apollographql.apollo.cache.http.ApolloHttpCache; import com.apollographql.apollo.cache.http.DiskLruHttpCacheStore; -import com.apollographql.apollo.cache.http.HttpCache; -import com.apollographql.apollo.cache.http.HttpCachePolicy; import com.apollographql.apollo.exception.ApolloException; import com.apollographql.apollo.exception.ApolloHttpException; import com.apollographql.apollo.integration.httpcache.AllFilmsQuery; @@ -12,7 +12,6 @@ import com.apollographql.apollo.integration.httpcache.DroidDetailsQuery; import com.apollographql.apollo.integration.httpcache.type.CustomType; import com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor; -import com.apollographql.apollo.internal.util.ApolloLogger; import com.apollographql.apollo.rx2.Rx2Apollo; import org.junit.After; @@ -69,7 +68,7 @@ public class HttpCacheTest { cacheStore = new MockHttpCacheStore(); cacheStore.delegate = new DiskLruHttpCacheStore(inMemoryFileSystem, new File("/cache/"), Integer.MAX_VALUE); - HttpCache cache = new HttpCache(cacheStore, new ApolloLogger(Optional.absent())); + HttpCache cache = new ApolloHttpCache(cacheStore, null); okHttpClient = new OkHttpClient.Builder() .addInterceptor(new TrackingInterceptor()) .addInterceptor(cache.interceptor()) @@ -81,7 +80,7 @@ public class HttpCacheTest { .serverUrl(server.url("/")) .okHttpClient(okHttpClient) .addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter) - .httpCacheStore(cacheStore) + .httpCache(cache) .build(); } diff --git a/apollo-integration/src/test/java/com/apollographql/apollo/MockHttpCacheStore.java b/apollo-integration/src/test/java/com/apollographql/apollo/MockHttpCacheStore.java index 481974fdb12..14d45f4fe81 100644 --- a/apollo-integration/src/test/java/com/apollographql/apollo/MockHttpCacheStore.java +++ b/apollo-integration/src/test/java/com/apollographql/apollo/MockHttpCacheStore.java @@ -1,8 +1,8 @@ package com.apollographql.apollo; -import com.apollographql.apollo.cache.http.HttpCacheRecord; -import com.apollographql.apollo.cache.http.HttpCacheRecordEditor; -import com.apollographql.apollo.cache.http.HttpCacheStore; +import com.apollographql.apollo.api.cache.http.HttpCacheRecord; +import com.apollographql.apollo.api.cache.http.HttpCacheRecordEditor; +import com.apollographql.apollo.api.cache.http.HttpCacheStore; import java.io.IOException; diff --git a/apollo-integration/src/test/java/com/apollographql/apollo/internal/cache/normalized/ApolloStoreTest.java b/apollo-integration/src/test/java/com/apollographql/apollo/internal/cache/normalized/ApolloStoreTest.java index 63877cf57c3..f8381f4e364 100644 --- a/apollo-integration/src/test/java/com/apollographql/apollo/internal/cache/normalized/ApolloStoreTest.java +++ b/apollo-integration/src/test/java/com/apollographql/apollo/internal/cache/normalized/ApolloStoreTest.java @@ -9,7 +9,7 @@ import com.apollographql.apollo.cache.normalized.NormalizedCache; import com.apollographql.apollo.cache.normalized.Record; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import org.junit.Test; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloClient.java b/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloClient.java index e9dceb61d6e..b6c519aaa54 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloClient.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloClient.java @@ -5,12 +5,10 @@ import com.apollographql.apollo.api.OperationName; import com.apollographql.apollo.api.Query; import com.apollographql.apollo.api.ScalarType; +import com.apollographql.apollo.api.cache.http.HttpCache; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import com.apollographql.apollo.api.internal.Optional; import com.apollographql.apollo.cache.CacheHeaders; -import com.apollographql.apollo.cache.http.HttpCache; -import com.apollographql.apollo.cache.http.HttpCacheInterceptor; -import com.apollographql.apollo.cache.http.HttpCachePolicy; -import com.apollographql.apollo.cache.http.HttpCacheStore; import com.apollographql.apollo.cache.normalized.ApolloStore; import com.apollographql.apollo.cache.normalized.ApolloStoreOperation; import com.apollographql.apollo.cache.normalized.CacheKeyResolver; @@ -21,12 +19,12 @@ import com.apollographql.apollo.fetcher.ResponseFetcher; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.internal.ApolloCallTracker; +import com.apollographql.apollo.internal.ApolloLogger; import com.apollographql.apollo.internal.RealApolloCall; import com.apollographql.apollo.internal.RealApolloPrefetch; import com.apollographql.apollo.internal.ResponseFieldMapperFactory; import com.apollographql.apollo.internal.cache.normalized.RealApolloStore; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; import java.io.IOException; import java.util.ArrayList; @@ -136,8 +134,8 @@ public ApolloQueryCall q @Override public ApolloPrefetch prefetch( @Nonnull Operation operation) { - return new RealApolloPrefetch(operation, serverUrl, httpCallFactory, httpCache, scalarTypeAdapters, dispatcher, - logger, tracker, sendOperationIdentifiers); + return new RealApolloPrefetch(operation, serverUrl, httpCallFactory, scalarTypeAdapters, dispatcher, logger, + tracker, sendOperationIdentifiers); } /** @@ -221,7 +219,7 @@ private RealApolloC @SuppressWarnings("WeakerAccess") public static class Builder { Call.Factory callFactory; HttpUrl serverUrl; - HttpCacheStore httpCacheStore; + HttpCache httpCache; ApolloStore apolloStore = ApolloStore.NO_APOLLO_STORE; Optional cacheFactory = Optional.absent(); Optional cacheKeyResolver = Optional.absent(); @@ -281,11 +279,11 @@ public Builder serverUrl(@Nonnull String serverUrl) { /** * Set the configuration to be used for request/response http cache. * - * @param cacheStore The store to use for reading and writing cached response. + * @param httpCache The to use for reading and writing cached response. * @return The {@link Builder} object to be used for chaining method calls */ - public Builder httpCacheStore(@Nonnull HttpCacheStore cacheStore) { - this.httpCacheStore = checkNotNull(cacheStore, "cacheStore == null"); + public Builder httpCache(@Nonnull HttpCache httpCache) { + this.httpCache = checkNotNull(httpCache, "httpCache == null"); return this; } @@ -419,18 +417,13 @@ public ApolloClient build() { ApolloLogger apolloLogger = new ApolloLogger(logger); okhttp3.Call.Factory callFactory = this.callFactory; - boolean localClient = this.callFactory == null; - if (localClient) { + if (callFactory == null) { callFactory = new OkHttpClient(); } - HttpCache httpCache = null; - if (httpCacheStore != null) { - httpCache = new HttpCache(httpCacheStore, apolloLogger); - if (localClient || shouldAddHttpCacheInterceptor(callFactory)) { - apolloLogger.w("Created local client, adding passed in HttpCache"); - callFactory = ((OkHttpClient) callFactory).newBuilder().addInterceptor(httpCache.interceptor()).build(); - } + HttpCache httpCache = this.httpCache; + if (httpCache != null) { + callFactory = addHttCacheInterceptorIfNeeded(callFactory, httpCache.interceptor()); } Executor dispatcher = this.dispatcher; @@ -470,17 +463,18 @@ private Executor defaultDispatcher() { }); } - private static boolean shouldAddHttpCacheInterceptor(Call.Factory callFactory) { + private static okhttp3.Call.Factory addHttCacheInterceptorIfNeeded(Call.Factory callFactory, + Interceptor httCacheInterceptor) { if (callFactory instanceof OkHttpClient) { OkHttpClient client = (OkHttpClient) callFactory; for (Interceptor interceptor : client.interceptors()) { - if (interceptor instanceof HttpCacheInterceptor) { - return false; + if (interceptor.getClass().equals(httCacheInterceptor.getClass())) { + return callFactory; } } - return true; + return client.newBuilder().addInterceptor(httCacheInterceptor).build(); } - return false; + return callFactory; } } } diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloQueryCall.java b/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloQueryCall.java index ea99183f804..c26d6e5bc0a 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloQueryCall.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/ApolloQueryCall.java @@ -3,7 +3,7 @@ import com.apollographql.apollo.api.OperationName; import com.apollographql.apollo.api.Query; import com.apollographql.apollo.cache.CacheHeaders; -import com.apollographql.apollo.cache.http.HttpCachePolicy; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import com.apollographql.apollo.fetcher.ResponseFetcher; import javax.annotation.Nonnull; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/fetcher/ResponseFetcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/fetcher/ResponseFetcher.java index 05c8ec7d62f..190345e11d6 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/fetcher/ResponseFetcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/fetcher/ResponseFetcher.java @@ -1,7 +1,7 @@ package com.apollographql.apollo.fetcher; import com.apollographql.apollo.interceptor.ApolloInterceptor; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; /** * A ResponseFetcher is an {@link ApolloInterceptor} inserted at the beginning of a request chain. diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/QueryReFetcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/QueryReFetcher.java index 1c523ca54df..a16be941189 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/QueryReFetcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/QueryReFetcher.java @@ -5,14 +5,13 @@ import com.apollographql.apollo.api.OperationName; import com.apollographql.apollo.api.Query; import com.apollographql.apollo.api.Response; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import com.apollographql.apollo.cache.CacheHeaders; -import com.apollographql.apollo.cache.http.HttpCachePolicy; import com.apollographql.apollo.cache.normalized.ApolloStore; import com.apollographql.apollo.exception.ApolloException; import com.apollographql.apollo.fetcher.ApolloResponseFetchers; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; import java.util.ArrayList; import java.util.Collections; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloCall.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloCall.java index 3d67bb81e91..a6b5020de54 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloCall.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloCall.java @@ -9,8 +9,8 @@ import com.apollographql.apollo.api.internal.Action; import com.apollographql.apollo.api.internal.Optional; import com.apollographql.apollo.cache.CacheHeaders; -import com.apollographql.apollo.cache.http.HttpCache; -import com.apollographql.apollo.cache.http.HttpCachePolicy; +import com.apollographql.apollo.api.cache.http.HttpCache; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import com.apollographql.apollo.cache.normalized.ApolloStore; import com.apollographql.apollo.exception.ApolloCanceledException; import com.apollographql.apollo.exception.ApolloException; @@ -25,7 +25,6 @@ import com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor; import com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; import java.util.ArrayList; import java.util.Arrays; @@ -437,11 +436,6 @@ public Builder cacheHeaders(CacheHeaders cacheHeaders) { return this; } - public Builder interceptorChain(ApolloInterceptorChain interceptorChain) { - this.interceptorChain = interceptorChain; - return this; - } - public Builder dispatcher(Executor dispatcher) { this.dispatcher = dispatcher; return this; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloPrefetch.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloPrefetch.java index e745f3e95ec..e48acf8b114 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloPrefetch.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloPrefetch.java @@ -2,9 +2,8 @@ import com.apollographql.apollo.ApolloPrefetch; import com.apollographql.apollo.api.Operation; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import com.apollographql.apollo.api.internal.Optional; -import com.apollographql.apollo.cache.http.HttpCache; -import com.apollographql.apollo.cache.http.HttpCachePolicy; import com.apollographql.apollo.exception.ApolloCanceledException; import com.apollographql.apollo.exception.ApolloException; import com.apollographql.apollo.exception.ApolloHttpException; @@ -14,7 +13,6 @@ import com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor; import com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; import java.util.Collections; import java.util.concurrent.Executor; @@ -36,7 +34,6 @@ final Operation operation; final HttpUrl serverUrl; final Call.Factory httpCallFactory; - final HttpCache httpCache; final ScalarTypeAdapters scalarTypeAdapters; final Executor dispatcher; final ApolloLogger logger; @@ -46,13 +43,12 @@ final AtomicReference state = new AtomicReference<>(IDLE); final AtomicReference originalCallback = new AtomicReference<>(); - public RealApolloPrefetch(Operation operation, HttpUrl serverUrl, Call.Factory httpCallFactory, HttpCache httpCache, + public RealApolloPrefetch(Operation operation, HttpUrl serverUrl, Call.Factory httpCallFactory, ScalarTypeAdapters scalarTypeAdapters, Executor dispatcher, ApolloLogger logger, ApolloCallTracker callTracker, boolean sendOperationIds) { this.operation = operation; this.serverUrl = serverUrl; this.httpCallFactory = httpCallFactory; - this.httpCache = httpCache; this.scalarTypeAdapters = scalarTypeAdapters; this.dispatcher = dispatcher; this.logger = logger; @@ -131,7 +127,7 @@ private ApolloInterceptor.CallBack interceptorCallbackProxy() { } @Override public ApolloPrefetch clone() { - return new RealApolloPrefetch(operation, serverUrl, httpCallFactory, httpCache, scalarTypeAdapters, dispatcher, + return new RealApolloPrefetch(operation, serverUrl, httpCallFactory, scalarTypeAdapters, dispatcher, logger, tracker, sendOperationIds); } diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloQueryWatcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloQueryWatcher.java index 4595d2fa423..e305405bdb9 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloQueryWatcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/RealApolloQueryWatcher.java @@ -14,7 +14,6 @@ import com.apollographql.apollo.exception.ApolloParseException; import com.apollographql.apollo.fetcher.ApolloResponseFetchers; import com.apollographql.apollo.fetcher.ResponseFetcher; -import com.apollographql.apollo.internal.util.ApolloLogger; import java.util.Collections; import java.util.Set; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/http/HttpCacheFetchStrategy.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/http/HttpCacheFetchStrategy.java deleted file mode 100644 index 500e894d8c8..00000000000 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/http/HttpCacheFetchStrategy.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.apollographql.apollo.internal.cache.http; - -/** - * Represents different fetch strategies for http request / response cache - */ -public enum HttpCacheFetchStrategy { - /** - * Signals the apollo client to fetch the GraphQL query response from the http cache only. - */ - CACHE_ONLY, - /** - * Signals the apollo client to fetch the GraphQL query response from the network only. - */ - NETWORK_ONLY, - /** - * Signals the apollo client to first fetch the GraphQL query response from the http cache. If it's not present in the - * cache response is fetched from the network. - */ - CACHE_FIRST, - /** - * Signals the apollo client to first fetch the GraphQL query response from the network. If it fails then fetch the - * response from the http cache. - */ - NETWORK_FIRST -} diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/normalized/RealApolloStore.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/normalized/RealApolloStore.java index 9ce0178accd..cd762fef0cc 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/normalized/RealApolloStore.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/cache/normalized/RealApolloStore.java @@ -16,7 +16,7 @@ import com.apollographql.apollo.internal.field.CacheFieldValueResolver; import com.apollographql.apollo.internal.response.RealResponseReader; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.util.ArrayList; import java.util.Collection; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheAndNetworkFetcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheAndNetworkFetcher.java index f24b4227f1d..19178f6ec73 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheAndNetworkFetcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheAndNetworkFetcher.java @@ -5,7 +5,7 @@ import com.apollographql.apollo.fetcher.ResponseFetcher; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.interceptor.ApolloInterceptorChain; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.util.concurrent.Executor; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheFirstFetcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheFirstFetcher.java index 9ae4a314404..cece01e50c5 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheFirstFetcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheFirstFetcher.java @@ -4,7 +4,7 @@ import com.apollographql.apollo.fetcher.ResponseFetcher; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.interceptor.ApolloInterceptorChain; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.util.concurrent.Executor; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheOnlyFetcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheOnlyFetcher.java index 0047ec981e0..513a5633126 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheOnlyFetcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/CacheOnlyFetcher.java @@ -6,7 +6,7 @@ import com.apollographql.apollo.fetcher.ResponseFetcher; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.interceptor.ApolloInterceptorChain; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.util.concurrent.Executor; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkFirstFetcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkFirstFetcher.java index 8d1addac88d..6bd70764107 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkFirstFetcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkFirstFetcher.java @@ -4,7 +4,7 @@ import com.apollographql.apollo.fetcher.ResponseFetcher; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.interceptor.ApolloInterceptorChain; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.util.concurrent.Executor; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkOnlyFetcher.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkOnlyFetcher.java index f4eab2b5114..dd6b8eec79f 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkOnlyFetcher.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/fetcher/NetworkOnlyFetcher.java @@ -3,7 +3,7 @@ import com.apollographql.apollo.fetcher.ResponseFetcher; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.interceptor.ApolloInterceptorChain; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.util.concurrent.Executor; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloCacheInterceptor.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloCacheInterceptor.java index 20884bd44da..6e49549e1a2 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloCacheInterceptor.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloCacheInterceptor.java @@ -14,7 +14,7 @@ import com.apollographql.apollo.internal.cache.normalized.ResponseNormalizer; import com.apollographql.apollo.internal.cache.normalized.Transaction; import com.apollographql.apollo.internal.cache.normalized.WriteableStore; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.util.ArrayList; import java.util.Collection; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloParseInterceptor.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloParseInterceptor.java index e43bb39dde6..c2aaccd88b0 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloParseInterceptor.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloParseInterceptor.java @@ -3,7 +3,7 @@ import com.apollographql.apollo.api.Operation; import com.apollographql.apollo.api.Response; import com.apollographql.apollo.api.ResponseFieldMapper; -import com.apollographql.apollo.cache.http.HttpCache; +import com.apollographql.apollo.api.cache.http.HttpCache; import com.apollographql.apollo.exception.ApolloException; import com.apollographql.apollo.exception.ApolloHttpException; import com.apollographql.apollo.exception.ApolloParseException; @@ -12,7 +12,7 @@ import com.apollographql.apollo.internal.cache.normalized.ResponseNormalizer; import com.apollographql.apollo.internal.response.OperationResponseParser; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.io.Closeable; import java.util.Map; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloServerInterceptor.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloServerInterceptor.java index 9ccf1391cfc..542579584e2 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloServerInterceptor.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/interceptor/ApolloServerInterceptor.java @@ -2,15 +2,15 @@ import com.apollographql.apollo.api.Operation; import com.apollographql.apollo.api.internal.Optional; -import com.apollographql.apollo.cache.http.HttpCache; -import com.apollographql.apollo.cache.http.HttpCachePolicy; +import com.apollographql.apollo.api.cache.http.HttpCache; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import com.apollographql.apollo.exception.ApolloNetworkException; import com.apollographql.apollo.interceptor.ApolloInterceptor; import com.apollographql.apollo.interceptor.ApolloInterceptorChain; import com.apollographql.apollo.internal.json.InputFieldJsonWriter; import com.apollographql.apollo.internal.json.JsonWriter; import com.apollographql.apollo.internal.response.ScalarTypeAdapters; -import com.apollographql.apollo.internal.util.ApolloLogger; +import com.apollographql.apollo.internal.ApolloLogger; import java.io.IOException; import java.util.concurrent.Executor; diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/response/RealResponseReader.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/response/RealResponseReader.java index f56a62e2467..618e66ac4fa 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/response/RealResponseReader.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/response/RealResponseReader.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; @SuppressWarnings("WeakerAccess") public final class RealResponseReader implements ResponseReader { private final Operation.Variables operationVariables; diff --git a/apollo-runtime/src/test/java/com/apollographql/apollo/internal/ResponseFetcherTest.java b/apollo-runtime/src/test/java/com/apollographql/apollo/internal/ResponseFetcherTest.java index f7fa86e34a0..cc5866d8e76 100644 --- a/apollo-runtime/src/test/java/com/apollographql/apollo/internal/ResponseFetcherTest.java +++ b/apollo-runtime/src/test/java/com/apollographql/apollo/internal/ResponseFetcherTest.java @@ -5,8 +5,7 @@ import com.apollographql.apollo.api.Query; import com.apollographql.apollo.api.ResponseFieldMapper; import com.apollographql.apollo.api.ResponseReader; -import com.apollographql.apollo.cache.http.HttpCachePolicy; -import com.apollographql.apollo.internal.cache.http.HttpCacheFetchStrategy; +import com.apollographql.apollo.api.cache.http.HttpCachePolicy; import org.junit.Before; import org.junit.Test; @@ -71,7 +70,7 @@ public class ResponseFetcherTest { .build(); RealApolloCall realApolloCall = (RealApolloCall) apolloClient.query(emptyQuery); - assertThat(realApolloCall.httpCachePolicy.fetchStrategy).isEqualTo(HttpCacheFetchStrategy.CACHE_ONLY); + assertThat(realApolloCall.httpCachePolicy.fetchStrategy).isEqualTo(HttpCachePolicy.FetchStrategy.CACHE_ONLY); assertThat(realApolloCall.responseFetcher).isEqualTo(NETWORK_ONLY); } @@ -82,7 +81,7 @@ public class ResponseFetcherTest { .build(); RealApolloCall realApolloCall = (RealApolloCall) apolloClient.query(emptyQuery); - assertThat(realApolloCall.httpCachePolicy.fetchStrategy).isEqualTo(HttpCacheFetchStrategy.NETWORK_ONLY); + assertThat(realApolloCall.httpCachePolicy.fetchStrategy).isEqualTo(HttpCachePolicy.FetchStrategy.NETWORK_ONLY); assertThat(realApolloCall.responseFetcher).isEqualTo(CACHE_FIRST); } } diff --git a/settings.gradle b/settings.gradle index 4c67eec86e8..f4e29bed645 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,4 +9,6 @@ include 'apollo-android-support' include ':apollo-integration' include ':apollo-rxsupport' include ':apollo-rx2support' -include 'apollo-espresso-support' \ No newline at end of file +include 'apollo-espresso-support' +include 'apollo-http-cache' +