use of com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory in project amplify-android by aws-amplify.
the class AWSApiPlugin method configure.
@Override
public void configure(JSONObject pluginConfiguration, @NonNull Context context) throws ApiException {
// Null-check for configuration is done inside readFrom method
AWSApiPluginConfiguration pluginConfig = AWSApiPluginConfigurationReader.readFrom(pluginConfiguration);
for (Map.Entry<String, ApiConfiguration> entry : pluginConfig.getApis().entrySet()) {
final String apiName = entry.getKey();
final ApiConfiguration apiConfiguration = entry.getValue();
final EndpointType endpointType = apiConfiguration.getEndpointType();
final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder.addNetworkInterceptor(UserAgentInterceptor.using(UserAgent::string));
okHttpClientBuilder.eventListener(new ApiConnectionEventListener());
OkHttpConfigurator configurator = apiConfigurators.get(apiName);
if (configurator != null) {
configurator.applyConfiguration(okHttpClientBuilder);
}
final ApiRequestDecoratorFactory requestDecoratorFactory = new ApiRequestDecoratorFactory(authProvider, apiConfiguration.getAuthorizationType(), apiConfiguration.getRegion(), apiConfiguration.getEndpointType(), apiConfiguration.getApiKey());
ClientDetails clientDetails = null;
if (EndpointType.REST.equals(endpointType)) {
if (apiConfiguration.getAuthorizationType() != AuthorizationType.NONE) {
AuthorizationType authorizationType = apiConfiguration.getAuthorizationType();
okHttpClientBuilder.addInterceptor(chain -> {
try {
RequestDecorator decorator = requestDecoratorFactory.forAuthType(authorizationType);
return chain.proceed(decorator.decorate(chain.request()));
} catch (ApiException.ApiAuthException apiAuthException) {
throw new IOException("Failed to decorate request for authorization.", apiAuthException);
}
});
}
clientDetails = new ClientDetails(apiConfiguration, okHttpClientBuilder.build(), null, requestDecoratorFactory);
restApis.add(apiName);
} else if (EndpointType.GRAPHQL.equals(endpointType)) {
final SubscriptionAuthorizer subscriptionAuthorizer = new SubscriptionAuthorizer(apiConfiguration, authProvider);
final SubscriptionEndpoint subscriptionEndpoint = new SubscriptionEndpoint(apiConfiguration, gqlResponseFactory, subscriptionAuthorizer);
clientDetails = new ClientDetails(apiConfiguration, okHttpClientBuilder.build(), subscriptionEndpoint, requestDecoratorFactory);
gqlApis.add(apiName);
}
if (clientDetails != null) {
apiDetails.put(apiName, clientDetails);
}
}
}
Aggregations