use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class AppSyncClient method mutation.
private <T extends Model> Cancelable mutation(final GraphQLRequest<ModelWithMetadata<T>> request, final Consumer<GraphQLResponse<ModelWithMetadata<T>>> onResponse, final Consumer<DataStoreException> onFailure) {
final Consumer<GraphQLResponse<ModelWithMetadata<T>>> responseConsumer = response -> {
if (response.hasErrors()) {
onResponse.accept(new GraphQLResponse<>(null, response.getErrors()));
} else {
onResponse.accept(response);
}
};
final Consumer<ApiException> failureConsumer = failure -> onFailure.accept(new DataStoreException("Failure during mutation.", failure, "Check details."));
final Cancelable cancelable = api.mutate(request, responseConsumer, failureConsumer);
if (cancelable != null) {
return cancelable;
}
return new NoOpCancelable();
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class AppSyncClient method sync.
@NonNull
@Override
public <T extends Model> Cancelable sync(@NonNull GraphQLRequest<PaginatedResult<ModelWithMetadata<T>>> request, @NonNull Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<T>>>> onResponse, @NonNull Consumer<DataStoreException> onFailure) {
final Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<T>>>> responseConsumer = apiQueryResponse -> {
if (apiQueryResponse.hasErrors()) {
onFailure.accept(new DataStoreException("Failure performing sync query to AppSync: " + apiQueryResponse.getErrors().toString(), AmplifyException.TODO_RECOVERY_SUGGESTION));
} else {
onResponse.accept(apiQueryResponse);
}
};
final Consumer<ApiException> failureConsumer = failure -> onFailure.accept(new DataStoreException("Failure performing sync query to AppSync.", failure, AmplifyException.TODO_RECOVERY_SUGGESTION));
final Cancelable cancelable = api.query(request, responseConsumer, failureConsumer);
if (cancelable != null) {
return cancelable;
}
return new NoOpCancelable();
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class AppSyncClient method subscription.
private <T extends Model> Cancelable subscription(SubscriptionType subscriptionType, ModelSchema modelSchema, Consumer<String> onSubscriptionStarted, Consumer<GraphQLResponse<ModelWithMetadata<T>>> onNextResponse, Consumer<DataStoreException> onSubscriptionFailure, Action onSubscriptionCompleted) {
final GraphQLRequest<ModelWithMetadata<T>> request;
try {
request = AppSyncRequestFactory.buildSubscriptionRequest(modelSchema, subscriptionType, authModeStrategyType);
} catch (DataStoreException requestGenerationException) {
onSubscriptionFailure.accept(requestGenerationException);
return new NoOpCancelable();
}
final Consumer<GraphQLResponse<ModelWithMetadata<T>>> responseConsumer = response -> {
if (response.hasErrors()) {
onSubscriptionFailure.accept(new DataStoreException.GraphQLResponseException("Subscription error for " + modelSchema.getName() + ": " + response.getErrors(), response.getErrors()));
} else {
onNextResponse.accept(response);
}
};
final Consumer<ApiException> failureConsumer = failure -> onSubscriptionFailure.accept(new DataStoreException("Error during subscription.", failure, "Evaluate details."));
final Cancelable cancelable = api.subscribe(request, onSubscriptionStarted, responseConsumer, failureConsumer, onSubscriptionCompleted);
if (cancelable != null) {
return cancelable;
}
return new NoOpCancelable();
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class RxApiBindingTest method queryEmitsFailure.
/**
* When the API behavior emits a failure for a query, so too should the Rx binding.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void queryEmitsFailure() throws InterruptedException {
// Arrange: category behavior emits a failure
ApiException expectedFailure = new ApiException("Expected", "Failure");
GraphQLRequest<Iterable<Model>> listRequest = createMockListRequest(Model.class);
doAnswer(invocation -> {
// 0 = clazz, 1 = onResponse, 2 = onFailure
final int positionOfOnFailure = 2;
Consumer<ApiException> onFailure = invocation.getArgument(positionOfOnFailure);
onFailure.accept(expectedFailure);
return null;
}).when(delegate).query(eq(listRequest), anyConsumer(), anyConsumer());
// Act: access query() method via Rx binding
TestObserver<GraphQLResponse<Iterable<Model>>> observer = rxApi.query(listRequest).test();
// Assert: failure bubbles up to Rx
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertError(expectedFailure);
verify(delegate).query(eq(listRequest), anyConsumer(), anyConsumer());
}
use of com.amplifyframework.api.ApiException in project amplify-android by aws-amplify.
the class RxApiBindingTest method httpGetEmitsFailure.
/**
* When the REST GET behavior emits a failure, the Rx binding should
* emit that same failure as well.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void httpGetEmitsFailure() throws InterruptedException {
RestOptions options = RestOptions.builder().addPath("/api/v1/movies").build();
ApiException expectedFailure = new ApiException("Expected", "Failure");
doAnswer(invocation -> {
final int positionOfFailureConsumer = 2;
Consumer<ApiException> onFailure = invocation.getArgument(positionOfFailureConsumer);
onFailure.accept(expectedFailure);
return null;
}).when(delegate).get(eq(options), anyConsumer(), anyConsumer());
TestObserver<RestResponse> observer = rxApi.get(options).test();
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertError(expectedFailure);
verify(delegate).get(eq(options), anyConsumer(), anyConsumer());
}
Aggregations