in aws-android-sdk-appsync/src/main/java/com/amazonaws/mobileconnectors/appsync/AWSAppSyncClient.java [526:676]
public AWSAppSyncClient build() {
// Validate context
if (mContext == null) {
throw new RuntimeException("A valid Android Context is required.");
}
// Validate AuthMode
Map<Object, AuthMode> authModeObjects = new HashMap<>();
authModeObjects.put(mApiKey, AuthMode.API_KEY);
authModeObjects.put(mCredentialsProvider, AuthMode.AWS_IAM);
authModeObjects.put(mCognitoUserPoolsAuthProvider, AuthMode.AMAZON_COGNITO_USER_POOLS);
authModeObjects.put(mOidcAuthProvider, AuthMode.OPENID_CONNECT);
authModeObjects.put(mAWSLambdaAuthProvider, AuthMode.AWS_LAMBDA);
authModeObjects.remove(null);
// Validate if only one Auth object is passed in to the builder
if (authModeObjects.size() > 1) {
throw new RuntimeException("More than one AuthMode has been passed in to the builder. " +
authModeObjects.values().toString() +
". Please pass in exactly one AuthMode into the builder.");
}
// Store references to the authMode object passed in to the builder and the
// corresponding AuthMode
Object selectedAuthModeObject = null;
AuthMode selectedAuthMode = null;
Iterator<Object> iterator = authModeObjects.keySet().iterator();
if (iterator.hasNext()) {
selectedAuthModeObject = iterator.next();
selectedAuthMode = authModeObjects.get(selectedAuthModeObject);
}
// Read serverUrl, region and AuthMode from awsconfiguration.json if present
if (mAwsConfiguration != null) {
try {
// Populate the serverUrl and region from awsconfiguration.json
AuthMode authModeFromConfigJson = null;
JSONObject appSyncJsonObject = mAwsConfiguration.optJsonObject("AppSync");
if (appSyncJsonObject == null) {
throw new RuntimeException("AppSync configuration is missing from awsconfiguration.json");
}
mServerUrl = mServerUrl != null ? mServerUrl : appSyncJsonObject.getString("ApiUrl");
mRegion = mRegion != null ? mRegion : Regions.fromName(appSyncJsonObject.getString("Region"));
if (mUseClientDatabasePrefix && mClientDatabasePrefix == null) {
// Populate the ClientDatabasePrefix from awsconfiguration.json
String clientDatabasePrefixFromConfigJson = null;
try {
clientDatabasePrefixFromConfigJson = appSyncJsonObject.getString("ClientDatabasePrefix");
} catch (Exception ex) {
Log.e(TAG, "Error is reading the ClientDatabasePrefix from AppSync configuration in awsconfiguration.json.");
throw new RuntimeException("ClientDatabasePrefix is not present in AppSync configuration in awsconfiguration.json " +
"however .useClientDatabasePrefix(true) is passed in.");
}
mClientDatabasePrefix = clientDatabasePrefixFromConfigJson;
}
authModeFromConfigJson = AuthMode.fromName(appSyncJsonObject.getString("AuthMode"));
// This validation is only for input passed via the awsconfiguration.json.
// Read the AuthMode and validate if the corresponding Auth object is passed in
// to the builder
final AuthMode authMode = authModeFromConfigJson;
if (selectedAuthModeObject == null && authMode.equals(AuthMode.API_KEY)) {
mApiKey = new BasicAPIKeyAuthProvider(appSyncJsonObject.getString("ApiKey"));
authModeObjects.put(mApiKey, AuthMode.API_KEY);
selectedAuthMode = authMode;
}
// Validate if the AuthMode match
if (!authMode.equals(selectedAuthMode)) {
throw new RuntimeException("Found conflicting AuthMode. Should be " +
authMode.toString() + " but you selected " + selectedAuthMode.toString());
}
} catch (Exception exception) {
throw new RuntimeException("Please check the AppSync configuration in " +
"awsconfiguration.json.", exception);
}
}
// Validate for the presence of one AuthMode object
if (authModeObjects.size() == 0) {
throw new RuntimeException("No valid AuthMode object is passed in to the builder.");
}
if (mUseClientDatabasePrefix && (mClientDatabasePrefix == null || StringUtils.isBlank(mClientDatabasePrefix))) {
throw new RuntimeException("Please pass in a valid ClientDatabasePrefix when useClientDatabasePrefix is true.");
}
if (!mUseClientDatabasePrefix && mClientDatabasePrefix != null && !StringUtils.isBlank(mClientDatabasePrefix)) {
Log.w(TAG, "A ClientDatabasePrefix is passed in however useClientDatabasePrefix is false.");
// Don't use the client database prefix when mUseClientDatabasePrefix flag is not set or set to false.
mClientDatabasePrefix = null;
}
// Validate ClientDatabasePrefix only when mUseClientDatabasePrefix is true
if (mUseClientDatabasePrefix) {
// Validate if the ClientDatabasePrefix passed in follows the pattern
if (mClientDatabasePrefix != null) {
final Pattern pattern = Pattern.compile(CLIENT_DATABAE_PREFIX_PATTERN);
final Matcher matcher = pattern.matcher(mClientDatabasePrefix);
if (!matcher.matches()) {
throw new RuntimeException("ClientDatabasePrefix validation failed. " +
"Please pass in characters that matches the pattern: " + CLIENT_DATABAE_PREFIX_PATTERN);
}
}
// Validate if the ClientDatabasePrefix is not used by the same GraphQL endpoint
// but different AuthMode.
String endpointAndAuthMode = prefixMap.get(mClientDatabasePrefix);
if (endpointAndAuthMode != null) {
if (!endpointAndAuthMode.equals(mServerUrl + "_" + selectedAuthMode)) {
throw new RuntimeException("ClientDatabasePrefix validation failed. " +
"The ClientDatabasePrefix " + mClientDatabasePrefix + " is already used by " +
"an other AWSAppSyncClient object with API Server Url: " + mServerUrl +
" with authMode: " + selectedAuthMode);
}
} else {
prefixMap.put(mClientDatabasePrefix, mServerUrl + "_" + selectedAuthMode);
}
}
if (mResolver == null) {
mResolver = new CacheKeyResolver() {
@Nonnull
@Override
public CacheKey fromFieldRecordSet(@Nonnull ResponseField field, @Nonnull Map<String, Object> recordSet) {
return formatCacheKey((String) recordSet.get("id"));
}
@Nonnull
@Override
public CacheKey fromFieldArguments(@Nonnull ResponseField field, @Nonnull Operation.Variables variables) {
return formatCacheKey((String) field.resolveArgument("id", variables));
}
private CacheKey formatCacheKey(String id) {
if (id == null || id.isEmpty()) {
return CacheKey.NO_KEY;
} else {
return CacheKey.from(id);
}
}
};
}
return new AWSAppSyncClient(this);
}