Search in sources :

Example 1 with ApiRequestDecoratorFactory

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);
        }
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) ApiRequestDecoratorFactory(com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory) AuthRuleRequestDecorator(com.amplifyframework.api.aws.auth.AuthRuleRequestDecorator) RequestDecorator(com.amplifyframework.api.aws.auth.RequestDecorator) HashMap(java.util.HashMap) Map(java.util.Map) ApiException(com.amplifyframework.api.ApiException)

Aggregations

ApiException (com.amplifyframework.api.ApiException)1 ApiRequestDecoratorFactory (com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory)1 AuthRuleRequestDecorator (com.amplifyframework.api.aws.auth.AuthRuleRequestDecorator)1 RequestDecorator (com.amplifyframework.api.aws.auth.RequestDecorator)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OkHttpClient (okhttp3.OkHttpClient)1