use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AnalyticsPinpointInstrumentedTest method setUp.
/**
* Configure the Amplify framework.
*
* @throws AmplifyException From Amplify configuration.
*/
@BeforeClass
public static void setUp() throws AmplifyException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Exception> asyncException = new AtomicReference<>();
Context context = getApplicationContext();
AWSMobileClient.getInstance().initialize(context, new AWSConfiguration(context), new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails result) {
latch.countDown();
}
@Override
public void onError(Exception exception) {
asyncException.set(exception);
latch.countDown();
}
});
try {
if (latch.await(AUTH_TIMEOUT, TimeUnit.SECONDS)) {
if (asyncException.get() != null) {
fail("Failed to instantiate AWSMobileClient");
}
} else {
fail("Failed to instantiate AWSMobileClient within " + AUTH_TIMEOUT + " seconds");
}
} catch (InterruptedException error) {
fail("Failed to instantiate AWSMobileClient");
}
AWSPinpointAnalyticsPlugin plugin = new AWSPinpointAnalyticsPlugin((Application) context, AWSMobileClient.getInstance());
Amplify.addPlugin(plugin);
Amplify.configure(context);
analyticsClient = plugin.getAnalyticsClient();
targetingClient = plugin.getTargetingClient();
}
use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AppSyncGraphQLRequestFactory method getMapOfFieldNameAndValues.
private static Map<String, Object> getMapOfFieldNameAndValues(@NonNull ModelSchema schema, @NonNull Model instance) throws AmplifyException {
if (!instance.getClass().getSimpleName().equals(schema.getName())) {
throw new AmplifyException("The object provided is not an instance of " + schema.getName() + ".", "Please provide an instance of " + schema.getName() + " that matches the schema type.");
}
final Map<String, Object> result = new HashMap<>();
for (ModelField modelField : schema.getFields().values()) {
if (modelField.isReadOnly()) {
// Skip read only fields, since they should not be included on the input object.
continue;
}
String fieldName = modelField.getName();
Object fieldValue = extractFieldValue(fieldName, instance, schema);
final ModelAssociation association = schema.getAssociations().get(fieldName);
if (association == null) {
result.put(fieldName, fieldValue);
} else if (association.isOwner()) {
Model target = (Model) Objects.requireNonNull(fieldValue);
result.put(association.getTargetName(), target.getId());
}
// Ignore if field is associated, but is not a "belongsTo" relationship
}
/*
* If the owner field exists on the model, and the value is null, it should be omitted when performing a
* mutation because the AppSync server will automatically populate it using the authentication token provided
* in the request header. The logic below filters out the owner field if null for this scenario.
*/
for (AuthRule authRule : schema.getAuthRules()) {
if (AuthStrategy.OWNER.equals(authRule.getAuthStrategy())) {
String ownerField = authRule.getOwnerFieldOrDefault();
if (result.containsKey(ownerField) && result.get(ownerField) == null) {
result.remove(ownerField);
}
}
}
return result;
}
use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AppSyncGraphQLRequestFactory method extractFieldValue.
private static Object extractFieldValue(String fieldName, Model instance, ModelSchema schema) throws AmplifyException {
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 AppSyncRequestFactory method getMapOfFieldNameAndValues.
private static Map<String, Object> getMapOfFieldNameAndValues(@NonNull ModelSchema schema, @NonNull Model instance) throws AmplifyException {
boolean isSerializedModel = instance instanceof SerializedModel;
boolean hasMatchingModelName = instance.getClass().getSimpleName().equals(schema.getName());
if (!(hasMatchingModelName || isSerializedModel)) {
throw new AmplifyException("The object provided is not an instance of " + schema.getName() + ".", "Please provide an instance of " + schema.getName() + " that matches the schema type.");
}
Map<String, Object> result = new HashMap<>(extractFieldLevelData(schema, instance));
/*
* If the owner field exists on the model, and the value is null, it should be omitted when performing a
* mutation because the AppSync server will automatically populate it using the authentication token provided
* in the request header. The logic below filters out the owner field if null for this scenario.
*/
for (AuthRule authRule : schema.getAuthRules()) {
if (AuthStrategy.OWNER.equals(authRule.getAuthStrategy())) {
String ownerField = authRule.getOwnerFieldOrDefault();
if (result.containsKey(ownerField) && result.get(ownerField) == null) {
result.remove(ownerField);
}
}
}
return result;
}
use of com.amplifyframework.AmplifyException in project amplify-android by aws-amplify.
the class AppSyncRequestFactory method buildUpdateRequest.
static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> buildUpdateRequest(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(getMapOfFieldNameAndValues(schema, model));
return buildMutation(schema, inputMap, predicate, MutationType.UPDATE, strategyType);
} catch (AmplifyException amplifyException) {
throw new DataStoreException("Failed to get fields for model.", amplifyException, "Validate your model file.");
}
}
Aggregations