use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class AWSAppSyncClients method withUserPools2FromAWSConfiguration.
@NonNull
public static AWSAppSyncClient withUserPools2FromAWSConfiguration(String idTokenStringForCustomCognitoUserPool) {
// Amazon Cognito User Pools - Custom CognitoUserPool
AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
awsConfiguration.setConfiguration("MultiAuthAndroidIntegTestApp_AMAZON_COGNITO_USER_POOLS_2");
JSONObject appSyncConfig = awsConfiguration.optJsonObject("AppSync");
String clientDatabasePrefix = JsonExtract.stringValue(appSyncConfig, "ClientDatabasePrefix");
String clientName = JsonExtract.stringValue(appSyncConfig, "AuthMode");
AWSAppSyncClient.Builder awsAppSyncClientBuilder4 = AWSAppSyncClient.builder().context(getTargetContext()).cognitoUserPoolsAuthProvider(() -> idTokenStringForCustomCognitoUserPool).awsConfiguration(awsConfiguration).useClientDatabasePrefix(true);
AWSAppSyncClient awsAppSyncClient4 = awsAppSyncClientBuilder4.build();
validateAppSyncClient(awsAppSyncClient4, clientDatabasePrefix, clientName);
return awsAppSyncClient4;
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class AddPostActivity method save.
private void save() {
final AddPostActivity display = this;
String title = ((EditText) findViewById(R.id.updateTitle)).getText().toString();
String author = ((EditText) findViewById(R.id.updateAuthor)).getText().toString();
String url = ((EditText) findViewById(R.id.updateUrl)).getText().toString();
String content = ((EditText) findViewById(R.id.updateContent)).getText().toString();
final AWSAppSyncClient client = ClientFactory.getInstance(this.getApplicationContext());
CreatePostMutation.Data expected = new CreatePostMutation.Data(null);
CreatePostInput createPostInput = CreatePostInput.builder().title(title).author(author).content(content).url(url).ups(0).downs(0).build();
CreatePostMutation addPostMutation = CreatePostMutation.builder().input(createPostInput).build();
client.mutate(addPostMutation, expected).enqueue(new GraphQLCall.Callback<CreatePostMutation.Data>() {
@Override
public void onResponse(@Nonnull final Response<CreatePostMutation.Data> response) {
Log.d(TAG, "Post added");
// Add To Delta Table
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(display, "Added!", Toast.LENGTH_SHORT).show();
display.finish();
}
});
}
@Override
public void onFailure(@Nonnull ApolloException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(display, "Failed to add post!", Toast.LENGTH_SHORT).show();
display.finish();
}
});
Log.d(TAG, "Post add failed with [" + e.getLocalizedMessage() + "]");
e.printStackTrace();
}
});
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class Posts method query.
/**
* Queries for a post with a given ID.
* @param client AppSync client through which to make the query
* @param responseFetcher A fetcher, e.g. {@link CacheAndNetworkFetcher}
* @param postId ID of a post for which to query
* @return cached/network responses, labeled in a Map as "NETWORK" and/or "CACHE"
*/
public static Map<String, Response<GetPostQuery.Data>> query(AWSAppSyncClient client, ResponseFetcher responseFetcher, String postId) {
Log.d(TAG, "Calling Query queryPost with responseFetcher: " + responseFetcher.toString());
int expectedResponseCount = responseFetcher.equals(AppSyncResponseFetchers.CACHE_AND_NETWORK) ? 2 : 1;
CountDownLatch queryCountDownLatch = new CountDownLatch(expectedResponseCount);
Map<String, Response<GetPostQuery.Data>> getPostQueryResponses = new HashMap<>();
client.query(GetPostQuery.builder().id(postId).build()).responseFetcher(responseFetcher).enqueue(DelegatingGraphQLCallback.to(response -> {
if (response.fromCache()) {
getPostQueryResponses.put("CACHE", response);
} else {
getPostQueryResponses.put("NETWORK", response);
}
queryCountDownLatch.countDown();
}, failure -> {
failure.printStackTrace();
queryCountDownLatch.countDown();
}));
Await.latch(queryCountDownLatch);
Log.d(TAG, "responseFetcher: " + responseFetcher.toString() + "; Cache response: " + getPostQueryResponses.get("CACHE"));
Log.d(TAG, "responseFetcher: " + responseFetcher.toString() + "; Network response: " + getPostQueryResponses.get("NETWORK"));
return getPostQueryResponses;
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class MultiClientInstrumentationTest method testClearMutationsCacheOnly.
@Test
public void testClearMutationsCacheOnly() throws ClearCacheException {
AWSAppSyncClient client = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
Response<AllPostsQuery.Data> networkResponse = Posts.list(client, AppSyncResponseFetchers.NETWORK_FIRST).get("NETWORK");
assertNotNull(networkResponse);
assertNotNull(networkResponse.data());
goOffline();
// Add a post
AddPostMutation.Data addPostMutationData = new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0));
AddPostMutation addPostMutation = AddPostMutation.builder().input(CreatePostInput.builder().title("Learning to Live ").author("Dream Theater @ ").url("Dream Theater Station").content("No energy for anger @" + System.currentTimeMillis()).ups(1).downs(0).build()).build();
client.mutate(addPostMutation, addPostMutationData).enqueue(NoOpGraphQLCallback.instance());
// Act: clear the caches.
client.clearCaches(ClearCacheOptions.builder().clearMutations().build());
// Check Mutation Queue
assertTrue(client.isMutationQueueEmpty());
// Query from cache and check data is available since only mutations queue is cleared.
Response<AllPostsQuery.Data> cacheResponse = Posts.list(client, AppSyncResponseFetchers.CACHE_ONLY).get("CACHE");
assertNotNull(cacheResponse);
assertNotNull(cacheResponse.data());
}
use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.
the class MultiClientInstrumentationTest method testClearCache.
@Test
public void testClearCache() throws ClearCacheException {
AWSAppSyncClient client = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
goOffline();
// Add a post
AddPostMutation.Data expected = new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0));
AddPostMutation addPostMutation = AddPostMutation.builder().input(CreatePostInput.builder().title("Learning to Live ").author("Dream Theater @ ").url("Dream Theater Station").content("No energy for anger @" + System.currentTimeMillis()).ups(1).downs(0).build()).build();
client.mutate(addPostMutation, expected).enqueue(NoOpGraphQLCallback.instance());
assertFalse(client.isMutationQueueEmpty());
client.clearCaches();
// Check Mutation Queue
assertTrue(client.isMutationQueueEmpty());
// Query from cache and check no data is available
Response<AllPostsQuery.Data> listPostsResponse = Posts.list(client, AppSyncResponseFetchers.CACHE_ONLY).get("CACHE");
assertNotNull(listPostsResponse);
assertNull(listPostsResponse.data());
}
Aggregations