Search in sources :

Example 36 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class QueryRefetchTest method refetchNoPreCachedQuery.

@Test
@SuppressWarnings("CheckReturnValue")
public void refetchNoPreCachedQuery() throws Exception {
    CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
    server.enqueue(mockResponse("CreateReviewResponse.json"));
    server.enqueue(mockResponse("ReviewsEmpireEpisodeResponse.json"));
    RealApolloCall call = (RealApolloCall) apolloClient.mutate(mutation).refetchQueries(new ReviewsByEpisodeQuery(Episode.EMPIRE));
    Rx2Apollo.from(call).test();
    assertThat(server.getRequestCount()).isEqualTo(2);
    assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {

        @Override
        public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
            assertThat(response.data().reviews()).hasSize(3);
            assertThat(response.data().reviews().get(2).stars()).isEqualTo(5);
            assertThat(response.data().reviews().get(2).commentary()).isEqualTo("Amazing");
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) ReviewsByEpisodeQuery(com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery) CreateReviewMutation(com.apollographql.apollo.integration.normalizer.CreateReviewMutation) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 37 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class QueryRefetchTest method refetchPreCachedQuery.

@Test
@SuppressWarnings("CheckReturnValue")
public void refetchPreCachedQuery() throws Exception {
    enqueueAndAssertResponse(server, "ReviewsEmpireEpisodeResponse.json", apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(NETWORK_FIRST), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {

        @Override
        public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {

        @Override
        public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
            assertThat(response.data().reviews()).hasSize(3);
            assertThat(response.data().reviews().get(2).stars()).isEqualTo(5);
            assertThat(response.data().reviews().get(2).commentary()).isEqualTo("Amazing");
            return true;
        }
    });
    CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
    server.enqueue(mockResponse("CreateReviewResponse.json"));
    server.enqueue(mockResponse("ReviewsEmpireEpisodeResponseUpdated.json"));
    RealApolloCall call = (RealApolloCall) apolloClient.mutate(mutation).refetchQueries(new ReviewsByEpisodeQuery(Episode.EMPIRE));
    Rx2Apollo.from(call).test();
    assertThat(server.getRequestCount()).isEqualTo(3);
    assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {

        @Override
        public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
            assertThat(response.data().reviews()).hasSize(4);
            assertThat(response.data().reviews().get(3).stars()).isEqualTo(5);
            assertThat(response.data().reviews().get(3).commentary()).isEqualTo("Awesome");
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) ReviewsByEpisodeQuery(com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery) CreateReviewMutation(com.apollographql.apollo.integration.normalizer.CreateReviewMutation) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 38 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class ApolloParseInterceptor method parse.

@SuppressWarnings("unchecked")
InterceptorResponse parse(Operation operation, okhttp3.Response httpResponse) throws ApolloHttpException, ApolloParseException {
    String cacheKey = httpResponse.request().header(HttpCache.CACHE_KEY_HEADER);
    if (httpResponse.isSuccessful()) {
        try {
            OperationResponseParser parser = new OperationResponseParser(operation, responseFieldMapper, scalarTypeAdapters, normalizer);
            Response parsedResponse = parser.parse(httpResponse.body().source()).toBuilder().fromCache(httpResponse.cacheResponse() != null).build();
            if (parsedResponse.hasErrors() && httpCache != null) {
                httpCache.removeQuietly(cacheKey);
            }
            return new InterceptorResponse(httpResponse, parsedResponse, normalizer.records());
        } catch (Exception rethrown) {
            logger.e(rethrown, "Failed to parse network response for operation: %s", operation);
            closeQuietly(httpResponse);
            if (httpCache != null) {
                httpCache.removeQuietly(cacheKey);
            }
            throw new ApolloParseException("Failed to parse http response", rethrown);
        }
    } else {
        logger.e("Failed to parse network response: %s", httpResponse);
        throw new ApolloHttpException(httpResponse);
    }
}
Also used : Response(com.apollographql.apollo.api.Response) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) OperationResponseParser(com.apollographql.apollo.response.OperationResponseParser) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ApolloException(com.apollographql.apollo.exception.ApolloException)

Example 39 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class QueryReFetcher method refetchQueries.

private void refetchQueries() {
    final OnCompleteCallback completeCallback = onCompleteCallback;
    final AtomicInteger callsLeft = new AtomicInteger(calls.size());
    for (final RealApolloCall call : calls) {
        // noinspection unchecked
        call.enqueue(new ApolloCall.Callback() {

            @Override
            public void onResponse(@Nonnull Response response) {
                if (callsLeft.decrementAndGet() == 0 && completeCallback != null) {
                    completeCallback.onFetchComplete();
                }
            }

            @Override
            public void onFailure(@Nonnull ApolloException e) {
                if (logger != null) {
                    logger.e(e, "Failed to fetch query: %s", call.operation);
                }
                if (callsLeft.decrementAndGet() == 0 && completeCallback != null) {
                    completeCallback.onFetchComplete();
                }
            }
        });
    }
}
Also used : Response(com.apollographql.apollo.api.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloCall(com.apollographql.apollo.ApolloCall)

Example 40 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class RxApollo method from.

/**
 * Converts an {@link ApolloQueryWatcher} into an Observable.
 *
 * @param watcher          the ApolloQueryWatcher to convert
 * @param backpressureMode the back pressure strategy to apply to the observable source.
 * @param <T>              the value type
 * @return the converted Observable
 */
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloQueryWatcher<T> watcher, @Nonnull Emitter.BackpressureMode backpressureMode) {
    checkNotNull(backpressureMode, "backpressureMode == null");
    checkNotNull(watcher, "watcher == null");
    return Observable.create(new Action1<Emitter<Response<T>>>() {

        @Override
        public void call(final Emitter<Response<T>> emitter) {
            final AtomicBoolean canceled = new AtomicBoolean();
            emitter.setCancellation(new Cancellable() {

                @Override
                public void cancel() throws Exception {
                    canceled.set(true);
                    watcher.cancel();
                }
            });
            watcher.enqueueAndWatch(new ApolloCall.Callback<T>() {

                @Override
                public void onResponse(@Nonnull Response<T> response) {
                    if (!canceled.get()) {
                        emitter.onNext(response);
                    }
                }

                @Override
                public void onFailure(@Nonnull ApolloException e) {
                    Exceptions.throwIfFatal(e);
                    if (!canceled.get()) {
                        emitter.onError(e);
                    }
                }
            });
        }
    }, backpressureMode);
}
Also used : Response(com.apollographql.apollo.api.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Emitter(rx.Emitter) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Cancellable(rx.functions.Cancellable) Nonnull(javax.annotation.Nonnull)

Aggregations

Response (com.apollographql.apollo.api.Response)67 Test (org.junit.Test)55 ApolloException (com.apollographql.apollo.exception.ApolloException)34 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)29 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)26 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)26 IOException (java.io.IOException)23 MockResponse (okhttp3.mockwebserver.MockResponse)22 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)18 ParseException (java.text.ParseException)17 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)12 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)12 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)11 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Nonnull (javax.annotation.Nonnull)7 TestObserver (io.reactivex.observers.TestObserver)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 TestSubscriber (rx.observers.TestSubscriber)5 Handler (android.os.Handler)4