Search in sources :

Example 11 with CategoryConfiguration

use of com.amplifyframework.core.category.CategoryConfiguration in project amplify-android by aws-amplify.

the class TestCategory method forPlugin.

/**
 * Creates a test category to be used for testing. A category containing given
 * plugin will be used to register and configure the plugin. The category
 * instance will have the same Amplify category-type as registered plugin,
 * and it should be cast appropriately before usage.
 *
 * <pre>
 *     AuthCategory auth = (AuthCategory) TestCategory.forPlugin(plugin);
 * </pre>
 *
 * Resulting category object should not be used in production.
 *
 * @param plugin Plugin to register in the category. The resulting category
 *               will share the category type with this plugin.
 * @param <P> Type of plugin being registered.
 * @return An Amplify category object with the given plugin registered and
 *         configured. The category will be of same type as registered plugin
 *         and it should be cast to correct type before usage.
 * @throws AmplifyException if category fails to configure given plugin.
 */
public static <P extends Plugin<?>> Category<?> forPlugin(P plugin) throws AmplifyException {
    CategoryType categoryType = plugin.getCategoryType();
    Category<Plugin<?>> category = (Category<Plugin<?>>) fromCategoryType(categoryType);
    category.addPlugin(plugin);
    CategoryConfiguration config = AmplifyConfiguration.fromConfigFile(context()).forCategoryType(categoryType);
    category.configure(config, context());
    category.initialize(context());
    return category;
}
Also used : Category(com.amplifyframework.core.category.Category) AnalyticsCategory(com.amplifyframework.analytics.AnalyticsCategory) GeoCategory(com.amplifyframework.geo.GeoCategory) StorageCategory(com.amplifyframework.storage.StorageCategory) AuthCategory(com.amplifyframework.auth.AuthCategory) LoggingCategory(com.amplifyframework.logging.LoggingCategory) ApiCategory(com.amplifyframework.api.ApiCategory) HubCategory(com.amplifyframework.hub.HubCategory) PredictionsCategory(com.amplifyframework.predictions.PredictionsCategory) DataStoreCategory(com.amplifyframework.datastore.DataStoreCategory) CategoryConfiguration(com.amplifyframework.core.category.CategoryConfiguration) CategoryType(com.amplifyframework.core.category.CategoryType) Plugin(com.amplifyframework.core.plugin.Plugin)

Example 12 with CategoryConfiguration

use of com.amplifyframework.core.category.CategoryConfiguration in project amplify-android by aws-amplify.

the class TestApiCategory method fromConfiguration.

/**
 * Creates an instance of {@link ApiCategory}, using the provided configuration
 * file, referred to by its android resource ID.
 * @return A configured and initialized ApiCategory instance
 */
@NonNull
static ApiCategory fromConfiguration(@RawRes int resourceId) throws AmplifyException {
    CognitoUserPoolsAuthProvider cognitoUserPoolsAuthProvider = new DefaultCognitoUserPoolsAuthProvider(AWSMobileClient.getInstance());
    ApiAuthProviders providers = ApiAuthProviders.builder().awsCredentialsProvider(AWSMobileClient.getInstance()).cognitoUserPoolsAuthProvider(cognitoUserPoolsAuthProvider).build();
    AWSApiPlugin plugin = AWSApiPlugin.builder().apiAuthProviders(providers).build();
    ApiCategory apiCategory = new ApiCategory();
    apiCategory.addPlugin(plugin);
    CategoryConfiguration apiConfiguration = AmplifyConfiguration.fromConfigFile(getApplicationContext(), resourceId).forCategoryType(CategoryType.API);
    apiCategory.configure(apiConfiguration, getApplicationContext());
    // apiCategory.initialize(...); Doesn't currently contain any logic, so, skip it.
    return apiCategory;
}
Also used : DefaultCognitoUserPoolsAuthProvider(com.amplifyframework.api.aws.sigv4.DefaultCognitoUserPoolsAuthProvider) CategoryConfiguration(com.amplifyframework.core.category.CategoryConfiguration) ApiCategory(com.amplifyframework.api.ApiCategory) DefaultCognitoUserPoolsAuthProvider(com.amplifyframework.api.aws.sigv4.DefaultCognitoUserPoolsAuthProvider) CognitoUserPoolsAuthProvider(com.amplifyframework.api.aws.sigv4.CognitoUserPoolsAuthProvider) NonNull(androidx.annotation.NonNull)

Aggregations

CategoryConfiguration (com.amplifyframework.core.category.CategoryConfiguration)12 ApiCategory (com.amplifyframework.api.ApiCategory)6 Context (android.content.Context)5 RawRes (androidx.annotation.RawRes)4 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)4 AmplifyException (com.amplifyframework.AmplifyException)4 AWSApiPlugin (com.amplifyframework.api.aws.AWSApiPlugin)4 AndroidLoggingPlugin (com.amplifyframework.logging.AndroidLoggingPlugin)4 CategoryType (com.amplifyframework.core.category.CategoryType)3 HashMap (java.util.HashMap)3 DefaultCognitoUserPoolsAuthProvider (com.amplifyframework.api.aws.sigv4.DefaultCognitoUserPoolsAuthProvider)2 AuthCategory (com.amplifyframework.auth.AuthCategory)2 AmplifyConfiguration (com.amplifyframework.core.AmplifyConfiguration)2 PredictionsCategory (com.amplifyframework.predictions.PredictionsCategory)2 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)2 RandomString (com.amplifyframework.testutils.random.RandomString)2 JSONObject (org.json.JSONObject)2 Before (org.junit.Before)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2