Search in sources :

Example 1 with RequestDecorator

use of com.amplifyframework.api.aws.auth.RequestDecorator 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)

Example 2 with RequestDecorator

use of com.amplifyframework.api.aws.auth.RequestDecorator in project amplify-android by aws-amplify.

the class MultiAuthAppSyncGraphQLOperation method dispatchRequest.

private void dispatchRequest() {
    if (authTypes.hasNext()) {
        AuthorizationType authType = authTypes.next();
        Request okHttpRequest = new Request.Builder().url(endpoint).addHeader("accept", CONTENT_TYPE).addHeader("content-type", CONTENT_TYPE).post(RequestBody.create(getRequest().getContent(), MediaType.parse(CONTENT_TYPE))).build();
        Request decoratedOkHttpRequest;
        try {
            RequestDecorator requestDecorator = apiRequestDecoratorFactory.forAuthType(authType);
            decoratedOkHttpRequest = requestDecorator.decorate(okHttpRequest);
        } catch (ApiException apiException) {
            LOG.warn("Failed to make a successful request with " + authType, apiException);
            // Only queue up a retry if it's an auth-related exception.
            if (apiException instanceof ApiAuthException && authTypes.hasNext()) {
                executorService.submit(this::dispatchRequest);
            } else {
                onFailure.accept(apiException);
            }
            return;
        }
        LOG.debug("Request: " + getRequest().getContent());
        ongoingCall = client.newCall(decoratedOkHttpRequest);
        ongoingCall.enqueue(new OkHttpCallback());
    } else {
        onFailure.accept(new ApiAuthException("Unable to successfully complete request with any of the compatible auth types.", "Check your application logs for detail."));
    }
}
Also used : ApiAuthException(com.amplifyframework.api.ApiException.ApiAuthException) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Request(okhttp3.Request) RequestDecorator(com.amplifyframework.api.aws.auth.RequestDecorator) ApiException(com.amplifyframework.api.ApiException)

Example 3 with RequestDecorator

use of com.amplifyframework.api.aws.auth.RequestDecorator in project amplify-android by aws-amplify.

the class AppSyncGraphQLOperation method dispatchRequest.

private void dispatchRequest() {
    try {
        LOG.debug("Request: " + getRequest().getContent());
        RequestDecorator requestDecorator = apiRequestDecoratorFactory.fromGraphQLRequest(getRequest());
        Request okHttpRequest = new Request.Builder().url(endpoint).addHeader("accept", CONTENT_TYPE).addHeader("content-type", CONTENT_TYPE).post(RequestBody.create(getRequest().getContent(), MediaType.parse(CONTENT_TYPE))).build();
        ongoingCall = client.newCall(requestDecorator.decorate(okHttpRequest));
        ongoingCall.enqueue(new OkHttpCallback());
    } catch (Exception error) {
        // Cancel if possible
        if (ongoingCall != null) {
            ongoingCall.cancel();
        }
        onFailure.accept(new ApiException("OkHttp client failed to make a successful request.", error, AmplifyException.TODO_RECOVERY_SUGGESTION));
    }
}
Also used : Request(okhttp3.Request) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) RequestDecorator(com.amplifyframework.api.aws.auth.RequestDecorator) AmplifyException(com.amplifyframework.AmplifyException) IOException(java.io.IOException) ApiException(com.amplifyframework.api.ApiException) ApiException(com.amplifyframework.api.ApiException)

Aggregations

ApiException (com.amplifyframework.api.ApiException)3 RequestDecorator (com.amplifyframework.api.aws.auth.RequestDecorator)3 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)2 IOException (java.io.IOException)2 Request (okhttp3.Request)2 AmplifyException (com.amplifyframework.AmplifyException)1 ApiAuthException (com.amplifyframework.api.ApiException.ApiAuthException)1 ApiRequestDecoratorFactory (com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory)1 AuthRuleRequestDecorator (com.amplifyframework.api.aws.auth.AuthRuleRequestDecorator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OkHttpClient (okhttp3.OkHttpClient)1