Search in sources :

Example 36 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class RxApollo method from.

/**
 * Converts an {@link ApolloCall} to a Observable. The number of emissions this Observable will have is based on the
 * {@link ResponseFetcher} used with the call.
 *
 * @param call             the ApolloCall to convert
 * @param <T>              the value type
 * @param backpressureMode The {@link rx.Emitter.BackpressureMode} to use.
 * @return the converted Observable
 */
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloCall<T> call, Emitter.BackpressureMode backpressureMode) {
    checkNotNull(call, "call == 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);
                    call.cancel();
                }
            });
            call.enqueue(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);
                    }
                }

                @Override
                public void onStatusEvent(@Nonnull ApolloCall.StatusEvent event) {
                    if (!canceled.get()) {
                        if (event == ApolloCall.StatusEvent.COMPLETED) {
                            emitter.onCompleted();
                        }
                    }
                }
            });
        }
    }, 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)

Example 37 with ApolloException

use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.

the class RxApollo method from.

@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloSubscriptionCall<T> call, Emitter.BackpressureMode backpressureMode) {
    checkNotNull(call, "call == 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);
                    call.cancel();
                }
            });
            call.execute(new ApolloSubscriptionCall.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);
                    }
                }

                @Override
                public void onCompleted() {
                    if (!canceled.get()) {
                        emitter.onCompleted();
                    }
                }
            });
        }
    }, 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

ApolloException (com.apollographql.apollo.exception.ApolloException)37 Test (org.junit.Test)31 Response (com.apollographql.apollo.api.Response)20 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)16 Nonnull (javax.annotation.Nonnull)15 MockResponse (okhttp3.mockwebserver.MockResponse)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Handler (android.os.Handler)7 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)6 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)6 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)6 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)5 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)5 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)5 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)5 Executor (java.util.concurrent.Executor)5 TimeoutException (java.util.concurrent.TimeoutException)5