Search in sources :

Example 1 with DiskLruHttpCacheStore

use of com.apollographql.apollo.cache.http.DiskLruHttpCacheStore in project apollo-android by apollographql.

the class ApolloPrefetchTest method setup.

@Before
public void setup() {
    cacheStore = new MockHttpCacheStore();
    cacheStore.delegate = new DiskLruHttpCacheStore(inMemoryFileSystem, new File("/cache/"), Integer.MAX_VALUE);
    okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            lastHttRequest = chain.request();
            lastHttResponse = chain.proceed(lastHttRequest);
            return lastHttResponse;
        }
    }).dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).dispatcher(Utils.immediateExecutor()).httpCache(new ApolloHttpCache(cacheStore, null)).build();
}
Also used : ApolloHttpCache(com.apollographql.apollo.cache.http.ApolloHttpCache) DiskLruHttpCacheStore(com.apollographql.apollo.cache.http.DiskLruHttpCacheStore) Dispatcher(okhttp3.Dispatcher) File(java.io.File) ApolloServerInterceptor(com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor) Interceptor(okhttp3.Interceptor) Before(org.junit.Before)

Example 2 with DiskLruHttpCacheStore

use of com.apollographql.apollo.cache.http.DiskLruHttpCacheStore in project apollo-android by apollographql.

the class HttpCacheTest method setUp.

@Before
public void setUp() {
    CustomTypeAdapter<Date> dateCustomTypeAdapter = new CustomTypeAdapter<Date>() {

        @Override
        public Date decode(CustomTypeValue value) {
            try {
                return DATE_FORMAT.parse(value.value.toString());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public CustomTypeValue encode(Date value) {
            return new CustomTypeValue.GraphQLString(DATE_FORMAT.format(value));
        }
    };
    cacheStore = new MockHttpCacheStore();
    cacheStore.delegate = new DiskLruHttpCacheStore(inMemoryFileSystem, new File("/cache/"), Integer.MAX_VALUE);
    HttpCache cache = new ApolloHttpCache(cacheStore, null);
    okHttpClient = new OkHttpClient.Builder().addInterceptor(new TrackingInterceptor()).addInterceptor(cache.interceptor()).dispatcher(new Dispatcher(Utils.immediateExecutorService())).readTimeout(2, TimeUnit.SECONDS).writeTimeout(2, TimeUnit.SECONDS).build();
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).dispatcher(Utils.immediateExecutor()).addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter).httpCache(cache).build();
}
Also used : ApolloHttpCache(com.apollographql.apollo.cache.http.ApolloHttpCache) OkHttpClient(okhttp3.OkHttpClient) ApolloHttpCache(com.apollographql.apollo.cache.http.ApolloHttpCache) HttpCache(com.apollographql.apollo.api.cache.http.HttpCache) Dispatcher(okhttp3.Dispatcher) Date(java.util.Date) CustomTypeAdapter(com.apollographql.apollo.response.CustomTypeAdapter) CustomTypeValue(com.apollographql.apollo.response.CustomTypeValue) DiskLruHttpCacheStore(com.apollographql.apollo.cache.http.DiskLruHttpCacheStore) ParseException(java.text.ParseException) File(java.io.File) Before(org.junit.Before)

Example 3 with DiskLruHttpCacheStore

use of com.apollographql.apollo.cache.http.DiskLruHttpCacheStore in project apollo-android by apollographql.

the class HttpCacheTest method fileSystemUnavailable.

@Test
public void fileSystemUnavailable() throws IOException, ApolloException {
    cacheStore.delegate = new DiskLruHttpCacheStore(new NoFileSystem(), new File("/cache/"), Integer.MAX_VALUE);
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkNoCachedResponse();
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) DiskLruHttpCacheStore(com.apollographql.apollo.cache.http.DiskLruHttpCacheStore) File(java.io.File) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ParseException(java.text.ParseException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Aggregations

DiskLruHttpCacheStore (com.apollographql.apollo.cache.http.DiskLruHttpCacheStore)3 File (java.io.File)3 ApolloHttpCache (com.apollographql.apollo.cache.http.ApolloHttpCache)2 ParseException (java.text.ParseException)2 Dispatcher (okhttp3.Dispatcher)2 Before (org.junit.Before)2 Response (com.apollographql.apollo.api.Response)1 HttpCache (com.apollographql.apollo.api.cache.http.HttpCache)1 ApolloException (com.apollographql.apollo.exception.ApolloException)1 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)1 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)1 ApolloServerInterceptor (com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor)1 CustomTypeAdapter (com.apollographql.apollo.response.CustomTypeAdapter)1 CustomTypeValue (com.apollographql.apollo.response.CustomTypeValue)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Interceptor (okhttp3.Interceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Test (org.junit.Test)1