Search in sources :

Example 11 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable 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();
}
Also used : Amplify(com.amplifyframework.core.Amplify) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Model(com.amplifyframework.core.model.Model) Action(com.amplifyframework.core.Action) ApiException(com.amplifyframework.api.ApiException) Logger(com.amplifyframework.logging.Logger) GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Cancelable(com.amplifyframework.core.async.Cancelable) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthModeStrategyType(com.amplifyframework.api.aws.AuthModeStrategyType) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) ApiCategoryBehavior(com.amplifyframework.api.ApiCategoryBehavior) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) DataStoreException(com.amplifyframework.datastore.DataStoreException) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ApiException(com.amplifyframework.api.ApiException) NonNull(androidx.annotation.NonNull)

Example 12 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable 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();
}
Also used : Amplify(com.amplifyframework.core.Amplify) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Model(com.amplifyframework.core.model.Model) Action(com.amplifyframework.core.Action) ApiException(com.amplifyframework.api.ApiException) Logger(com.amplifyframework.logging.Logger) GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Cancelable(com.amplifyframework.core.async.Cancelable) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthModeStrategyType(com.amplifyframework.api.aws.AuthModeStrategyType) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) ApiCategoryBehavior(com.amplifyframework.api.ApiCategoryBehavior) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) DataStoreException(com.amplifyframework.datastore.DataStoreException) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ApiException(com.amplifyframework.api.ApiException)

Example 13 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observe behavior is an Observable. It should
 * fail with an exception when the DataStore observe method calls back its error consumer.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange for observer() to callback failure
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = clazz, 1 = onStart, 2 = onNext, 3 = onFailure, 4 = onComplete
        final int positionOfOnStart = 1;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
        onStart.accept(new NoOpCancelable());
        final int positionOfOnFailure = 3;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: observe the DataStore via Rx binding
    TestObserver<DataStoreItemChange<Model>> observer = rxDataStore.observe(Model.class).test();
    // Assert: failure is propagated
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) DataStoreItemChange(com.amplifyframework.datastore.DataStoreItemChange) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Test(org.junit.Test)

Example 14 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeQueryFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's observeQuery behavior is an Observable. It should
 * fail with an exception when the DataStore observe method calls back its error consumer.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void observeQueryFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange for observer() to callback failure
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = clazz, 1 = options, 2 = start consumer, 3 = item consumer, 4 = failure consumer, 5 = onComplete
        final int positionOfOnStart = 3;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
        onStart.accept(new NoOpCancelable());
        final int positionOfOnFailure = 4;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: observe the DataStore via Rx binding
    TestObserver<DataStoreQuerySnapshot<Model>> observer = rxDataStore.observeQuery(Model.class, new ObserveQueryOptions()).test();
    // Assert: failure is propagated
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Example 15 with NoOpCancelable

use of com.amplifyframework.core.async.NoOpCancelable in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method observeReturnsCategoryBehaviorChanges.

/**
 * The Rx binding for observing the DataStore should be an Observable stream
 * of DataStore changes. It should emit events whenever they are observed
 * on the observe behavior.
 */
@Test
public void observeReturnsCategoryBehaviorChanges() {
    // Arrange: observe(Class<?>) will spit out some values from category behavior.
    Model model = RandomModel.model();
    DataStoreItemChange<Model> changeEvent = DataStoreItemChange.builder().uuid(model.getId()).itemClass(Model.class).item(model).type(Type.CREATE).initiator(Initiator.LOCAL).build();
    doAnswer(invocation -> {
        // 0 = clazz, 1 = start consumer, 2 = item consumer, 3 = failure consumer, 4 = onComplete
        final int positionOfStartConsumer = 1;
        Consumer<Cancelable> onStart = invocation.getArgument(positionOfStartConsumer);
        onStart.accept(new NoOpCancelable());
        final int positionOfValueConsumer = 2;
        Consumer<DataStoreItemChange<Model>> onNext = invocation.getArgument(positionOfValueConsumer);
        onNext.accept(changeEvent);
        // "void"
        return null;
    }).when(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
    // Act: Observe the DataStore via Rx binding
    TestObserver<DataStoreItemChange<Model>> observer = rxDataStore.observe(Model.class).test();
    // Assert: event is observed
    observer.awaitCount(1).assertValue(changeEvent);
    verify(delegate).observe(eq(Model.class), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
Also used : DataStoreItemChange(com.amplifyframework.datastore.DataStoreItemChange) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) Test(org.junit.Test)

Aggregations

NoOpCancelable (com.amplifyframework.core.async.NoOpCancelable)15 Cancelable (com.amplifyframework.core.async.Cancelable)14 Model (com.amplifyframework.core.model.Model)12 Test (org.junit.Test)12 Action (com.amplifyframework.core.Action)8 DataStoreException (com.amplifyframework.datastore.DataStoreException)8 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)6 SubscriptionType (com.amplifyframework.api.graphql.SubscriptionType)6 Consumer (com.amplifyframework.core.Consumer)6 ModelSchema (com.amplifyframework.core.model.ModelSchema)6 RandomModel (com.amplifyframework.testutils.random.RandomModel)6 RandomString (com.amplifyframework.testutils.random.RandomString)6 Completable (io.reactivex.rxjava3.core.Completable)5 Observable (io.reactivex.rxjava3.core.Observable)5 Assert.assertTrue (org.junit.Assert.assertTrue)4 Pair (android.util.Pair)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3 AmplifyException (com.amplifyframework.AmplifyException)3 ApiCategoryBehavior (com.amplifyframework.api.ApiCategoryBehavior)3