Search in sources :

Example 41 with Response

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

the class GitHuntEntryDetailActivity method subscribeRepoCommentAdded.

private void subscribeRepoCommentAdded() {
    ApolloSubscriptionCall<RepoCommentAddedSubscription.Data> subscriptionCall = application.apolloClient().subscribe(new RepoCommentAddedSubscription(repoFullName));
    disposables.add(Rx2Apollo.from(subscriptionCall).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Response<RepoCommentAddedSubscription.Data>>() {

        @Override
        public void onNext(Response<RepoCommentAddedSubscription.Data> response) {
            commentsListViewAdapter.addItem(response.data().commentAdded().content());
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, e.getMessage(), e);
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "Subscription exhausted");
        }
    }));
}
Also used : Response(com.apollographql.apollo.api.Response) RepoCommentAddedSubscription(com.apollographql.apollo.sample.RepoCommentAddedSubscription) DisposableSubscriber(io.reactivex.subscribers.DisposableSubscriber)

Example 42 with Response

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

the class RealSubscriptionManager method onOperationDataServerMessage.

@SuppressWarnings("unchecked")
private void onOperationDataServerMessage(OperationServerMessage.Data message) {
    String subscriptionId = message.id != null ? message.id : "";
    SubscriptionRecord subscriptionRecord;
    synchronized (this) {
        subscriptionRecord = subscriptions.get(subscriptionId);
    }
    if (subscriptionRecord != null) {
        ResponseFieldMapper responseFieldMapper = responseFieldMapperFactory.create(subscriptionRecord.subscription);
        OperationResponseParser parser = new OperationResponseParser(subscriptionRecord.subscription, responseFieldMapper, scalarTypeAdapters);
        Response response;
        try {
            response = parser.parse(message.payload);
        } catch (Exception e) {
            subscriptionRecord = removeSubscriptionById(subscriptionId);
            if (subscriptionRecord != null) {
                subscriptionRecord.notifyOnError(new ApolloSubscriptionException("Failed to parse server message", e));
            }
            return;
        }
        subscriptionRecord.notifyOnResponse(response);
    }
}
Also used : ResponseFieldMapper(com.apollographql.apollo.api.ResponseFieldMapper) Response(com.apollographql.apollo.api.Response) OperationResponseParser(com.apollographql.apollo.response.OperationResponseParser) ApolloNetworkException(com.apollographql.apollo.exception.ApolloNetworkException)

Example 43 with Response

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

the class ApolloCallbackTest method onHttpError.

@Test
public void onHttpError() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
    server.enqueue(new MockResponse().setResponseCode(401).setBody("Unauthorized request!"));
    apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {

        @Override
        public void onResponse(@Nonnull Response response) {
            countDownLatch.countDown();
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            exceptionRef.set(e);
            countDownLatch.countDown();
        }

        @Override
        public void onHttpError(@Nonnull ApolloHttpException e) {
            exceptionRef.set(e);
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
    assertThat(exceptionRef.get()).isInstanceOf(ApolloHttpException.class);
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) ApolloException(com.apollographql.apollo.exception.ApolloException) Nonnull(javax.annotation.Nonnull) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 44 with Response

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

the class ApolloCallbackTest method onParseError.

@Test
public void onParseError() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
    server.enqueue(new MockResponse().setResponseCode(200).setBody("nonsense"));
    apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {

        @Override
        public void onResponse(@Nonnull Response response) {
            countDownLatch.countDown();
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            countDownLatch.countDown();
        }

        @Override
        public void onParseError(@Nonnull ApolloParseException e) {
            exceptionRef.set(e);
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
    assertThat(exceptionRef.get()).isInstanceOf(ApolloParseException.class);
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) ApolloException(com.apollographql.apollo.exception.ApolloException) Nonnull(javax.annotation.Nonnull) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApolloParseException(com.apollographql.apollo.exception.ApolloParseException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 45 with Response

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

the class ApolloCallbackTest method onResponse.

@Test
public void onResponse() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final AtomicBoolean invoked = new AtomicBoolean();
    final Handler callbackHandler = mockCallbackHandler(invoked);
    final AtomicReference<Response> responseRef = new AtomicReference<>();
    server.enqueue(new MockResponse().setResponseCode(200).setBody("{" + "  \"errors\": [" + "    {" + "      \"message\": \"Cannot query field \\\"names\\\" on type \\\"Species\\\".\"," + "      \"locations\": [" + "        {" + "          \"line\": 3," + "          \"column\": 5" + "        }" + "      ]" + "    }" + "  ]" + "}"));
    apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {

        @Override
        public void onResponse(@Nonnull Response response) {
            responseRef.set(response);
            countDownLatch.countDown();
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            countDownLatch.countDown();
        }
    }, callbackHandler));
    countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    assertThat(invoked.get()).isTrue();
    assertThat(responseRef.get()).isNotNull();
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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