Skip to content

Commit

Permalink
Configure API Plugin with Amplify Outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcreaser committed Apr 17, 2024
1 parent 0d70044 commit 37fe854
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import androidx.annotation.VisibleForTesting;
import androidx.core.util.ObjectsCompat;

import com.amplifyframework.AmplifyException;
import com.amplifyframework.annotations.InternalAmplifyApi;
import com.amplifyframework.api.ApiException;
import com.amplifyframework.api.ApiPlugin;
import com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory;
Expand All @@ -40,6 +42,7 @@
import com.amplifyframework.core.Action;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.Consumer;
import com.amplifyframework.core.configuration.AmplifyOutputsData;
import com.amplifyframework.hub.HubChannel;
import com.amplifyframework.util.Immutable;
import com.amplifyframework.util.UserAgent;
Expand Down Expand Up @@ -131,9 +134,18 @@ public void configure(
@NonNull Context context
) throws ApiException {
// Null-check for configuration is done inside readFrom method
AWSApiPluginConfiguration pluginConfig =
AWSApiPluginConfigurationReader.readFrom(pluginConfiguration);
AWSApiPluginConfiguration pluginConfig = AWSApiPluginConfigurationReader.readFrom(pluginConfiguration);
configure(pluginConfig);
}

@Override
@InternalAmplifyApi
public void configure(@NonNull AmplifyOutputsData configuration, @NonNull Context context) throws AmplifyException {
AWSApiPluginConfiguration pluginConfig = AWSApiPluginConfigurationReader.from(configuration);
configure(pluginConfig);
}

private void configure(AWSApiPluginConfiguration pluginConfig) {
for (Map.Entry<String, ApiConfiguration> entry : pluginConfig.getApis().entrySet()) {
final String apiName = entry.getKey();
final ApiConfiguration apiConfiguration = entry.getValue();
Expand All @@ -148,10 +160,10 @@ public void configure(
}

final ApiRequestDecoratorFactory requestDecoratorFactory = new ApiRequestDecoratorFactory(authProvider,
apiConfiguration.getAuthorizationType(),
apiConfiguration.getRegion(),
apiConfiguration.getEndpointType(),
apiConfiguration.getApiKey());
apiConfiguration.getAuthorizationType(),
apiConfiguration.getRegion(),
apiConfiguration.getEndpointType(),
apiConfiguration.getApiKey());

ClientDetails clientDetails = null;
if (EndpointType.REST.equals(endpointType)) {
Expand All @@ -169,22 +181,22 @@ public void configure(
});
}
clientDetails = new ClientDetails(apiConfiguration,
okHttpClientBuilder.build(),
null,
requestDecoratorFactory);
okHttpClientBuilder.build(),
null,
requestDecoratorFactory);
restApis.add(apiName);
} else if (EndpointType.GRAPHQL.equals(endpointType)) {
final SubscriptionAuthorizer subscriptionAuthorizer =
new SubscriptionAuthorizer(apiConfiguration, authProvider);
final OkHttpConfigurator websocketUpgradeConfigurator =
apiWebsocketUpgradeClientConfigurators.get(apiName);
apiWebsocketUpgradeClientConfigurators.get(apiName);
final SubscriptionEndpoint subscriptionEndpoint =
new SubscriptionEndpoint(apiConfiguration, websocketUpgradeConfigurator, gqlResponseFactory,
subscriptionAuthorizer, apiName);
subscriptionAuthorizer, apiName);
clientDetails = new ClientDetails(apiConfiguration,
okHttpClientBuilder.build(),
subscriptionEndpoint,
requestDecoratorFactory);
okHttpClientBuilder.build(),
subscriptionEndpoint,
requestDecoratorFactory);
gqlApis.add(apiName);
}
if (clientDetails != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.amplifyframework.AmplifyException;
import com.amplifyframework.api.ApiException;
import com.amplifyframework.core.configuration.AmplifyOutputsData;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -34,6 +35,8 @@
* associated key.
*/
final class AWSApiPluginConfigurationReader {
private static final String GEN2_API_NAME = "default";

private AWSApiPluginConfigurationReader() { /* no instances */ }

/**
Expand All @@ -56,6 +59,51 @@ static AWSApiPluginConfiguration readFrom(JSONObject configurationJson)
return parseConfigurationJson(configurationJson);
}

static AWSApiPluginConfiguration from(AmplifyOutputsData outputs) throws ApiException {
final AmplifyOutputsData.Data data = outputs.getData();

if (data == null) {
throw new ApiException(
"Missing data configuration in Amplify Outputs",
"Check that your amplify_outputs.json file contains a \"data\" section"
);
}

// Gen2 config supports multiple authorization type, but the library only supports one, so we use
// the default authorization type
final AuthorizationType authorizationType = getAuthorizationType(data.getDefaultAuthorizationType());

// The Gen2 data config is quite different from Gen1. It only supports defining a single GraphQL API. REST
// APIs are not supported.
final ApiConfiguration apiConfigBuilder = ApiConfiguration.builder()
.endpointType(EndpointType.GRAPHQL)
.endpoint(data.getUrl())
.region(data.getAwsRegion())
.authorizationType(authorizationType)
.apiKey(data.getApiKey())
.build();

return AWSApiPluginConfiguration.builder()
.addApi(GEN2_API_NAME, apiConfigBuilder)
.build();
}

private static AuthorizationType getAuthorizationType(AmplifyOutputsData.AwsAppsyncAuthorizationType type) {
switch (type) {
case AMAZON_COGNITO_USER_POOLS:
return AuthorizationType.AMAZON_COGNITO_USER_POOLS;
case API_KEY:
return AuthorizationType.API_KEY;
case AWS_IAM:
return AuthorizationType.AWS_IAM;
case AWS_LAMBDA:
return AuthorizationType.AWS_LAMBDA;
case OPENID_CONNECT:
return AuthorizationType.OPENID_CONNECT;
}
return AuthorizationType.NONE;
}

private static AWSApiPluginConfiguration parseConfigurationJson(JSONObject configurationJson)
throws ApiException {

Expand Down

0 comments on commit 37fe854

Please sign in to comment.