Search in sources :

Example 1 with OperationResponseParser

use of com.apollographql.apollo.response.OperationResponseParser 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 2 with OperationResponseParser

use of com.apollographql.apollo.response.OperationResponseParser 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)

Aggregations

Response (com.apollographql.apollo.api.Response)2 OperationResponseParser (com.apollographql.apollo.response.OperationResponseParser)2 ResponseFieldMapper (com.apollographql.apollo.api.ResponseFieldMapper)1 ApolloException (com.apollographql.apollo.exception.ApolloException)1 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)1 ApolloNetworkException (com.apollographql.apollo.exception.ApolloNetworkException)1 ApolloParseException (com.apollographql.apollo.exception.ApolloParseException)1