use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AppSyncRequestFactory method buildSyncRequest.
/**
* Builds the query document for base and delta sync.
* If you provide lastSyncTime, it builds a delta sync, where the delta is computed
* against the provided time. Otherwise, if you provide a null lastSyncTime, a
* request doc is generated for a base sync.
* @param modelSchema Schema Class for which we want to sync.
* @param lastSync The last time synced. If not provided, do a base query.
* If provided, do a delta query.
* @param <T> The type of objects we are syncing
* @return A string which contains a GraphQL query doc for an base/delta sync
* @throws DataStoreException On Failure to inspect
*/
@NonNull
static <T> AppSyncGraphQLRequest<T> buildSyncRequest(@NonNull final ModelSchema modelSchema, @Nullable final Long lastSync, @Nullable final Integer limit, @NonNull final QueryPredicate predicate, @NonNull final AuthModeStrategyType strategyType) throws DataStoreException {
try {
AppSyncGraphQLRequest.Builder builder = AppSyncGraphQLRequest.builder().modelClass(modelSchema.getModelClass()).modelSchema(modelSchema).operation(QueryType.SYNC).requestAuthorizationStrategyType(strategyType).requestOptions(new DataStoreGraphQLRequestOptions()).responseType(TypeMaker.getParameterizedType(PaginatedResult.class, ModelWithMetadata.class, modelSchema.getModelClass()));
if (lastSync != null) {
builder.variable("lastSync", "AWSTimestamp", lastSync);
}
if (limit != null) {
builder.variable("limit", "Int", limit);
}
if (!QueryPredicates.all().equals(predicate)) {
String filterType = "Model" + Casing.capitalizeFirst(modelSchema.getName()) + "FilterInput";
QueryPredicate syncPredicate = predicate;
if (!(syncPredicate instanceof QueryPredicateGroup)) {
// When a filter is provided, wrap it with a predicate group of type AND. By doing this, it enables
// AppSync to optimize the request by performing a DynamoDB query instead of a scan. If the
// provided syncPredicate is already a QueryPredicateGroup, this is not needed. If the provided
// group is of type AND, the optimization will occur. If the top level group is OR or NOT, the
// optimization is not possible anyway.
syncPredicate = QueryPredicateGroup.andOf(syncPredicate);
}
builder.variable("filter", filterType, parsePredicate(syncPredicate));
}
return builder.build();
} catch (AmplifyException amplifyException) {
throw new DataStoreException("Failed to get fields for model.", amplifyException, "Validate your model file.");
}
}
use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AppSyncRequestFactory method buildDeletionRequest.
static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> buildDeletionRequest(ModelSchema schema, M model, Integer version, QueryPredicate predicate, AuthModeStrategyType strategyType) throws DataStoreException {
try {
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("_version", version);
inputMap.putAll(getDeleteMutationInputMap(schema, model));
return buildMutation(schema, inputMap, predicate, MutationType.DELETE, strategyType);
} catch (AmplifyException amplifyException) {
throw new DataStoreException("Failed to get fields for model.", amplifyException, "Validate your model file.");
}
}
use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AppSyncRequestFactory method extractFieldValue.
private static Object extractFieldValue(String fieldName, Model instance, ModelSchema schema) throws AmplifyException {
if (instance instanceof SerializedModel) {
SerializedModel serializedModel = (SerializedModel) instance;
Map<String, Object> serializedData = serializedModel.getSerializedData();
ModelField field = schema.getFields().get(fieldName);
Object fieldValue = serializedData.get(fieldName);
if (fieldValue != null && field != null && field.isCustomType()) {
return extractCustomTypeFieldValue(fieldName, serializedData.get(fieldName));
}
return fieldValue;
}
try {
Field privateField = instance.getClass().getDeclaredField(fieldName);
privateField.setAccessible(true);
return privateField.get(instance);
} catch (Exception exception) {
throw new AmplifyException("An invalid field was provided. " + fieldName + " is not present in " + schema.getName(), exception, "Check if this model schema is a correct representation of the fields in the provided Object");
}
}
use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class TestStorageCategory method create.
/**
* Creates an instance of {@link StorageCategory} using the provided configuration resource.
* @param context Android Context
* @param resourceId Android resource ID for a configuration file
* @return A StorageCategory instance using the provided configuration
*/
static StorageCategory create(@NonNull Context context, @RawRes int resourceId) {
Objects.requireNonNull(context);
final StorageCategory storageCategory = new StorageCategory();
try {
storageCategory.addPlugin(new AWSS3StoragePlugin(new TestCognitoAuthProvider()));
CategoryConfiguration storageConfiguration = AmplifyConfiguration.fromConfigFile(context, resourceId).forCategoryType(CategoryType.STORAGE);
storageCategory.configure(storageConfiguration, context);
// storageCategory.initialize(context); // Doesn't do anything right now.
} catch (AmplifyException initializationFailure) {
throw new RuntimeException(initializationFailure);
}
return storageCategory;
}
use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AmplifyConfiguration method configsFromJson.
private static Map<String, CategoryConfiguration> configsFromJson(JSONObject json) throws AmplifyException {
final List<CategoryConfiguration> possibleConfigs = Arrays.asList(new AnalyticsCategoryConfiguration(), new ApiCategoryConfiguration(), new AuthCategoryConfiguration(), new DataStoreCategoryConfiguration(), new GeoCategoryConfiguration(), new HubCategoryConfiguration(), new LoggingCategoryConfiguration(), new PredictionsCategoryConfiguration(), new StorageCategoryConfiguration());
final Map<String, CategoryConfiguration> actualConfigs = new HashMap<>();
try {
for (CategoryConfiguration possibleConfig : possibleConfigs) {
String categoryJsonKey = possibleConfig.getCategoryType().getConfigurationKey();
if (json.has(categoryJsonKey)) {
possibleConfig.populateFromJSON(json.getJSONObject(categoryJsonKey));
actualConfigs.put(categoryJsonKey, possibleConfig);
}
}
} catch (JSONException error) {
throw new AmplifyException("Could not parse amplifyconfiguration.json ", error, "Check any modifications made to the file.");
}
return Immutable.of(actualConfigs);
}
Aggregations