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);
}
}
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);
}
}
Aggregations