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);
}
}
}
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."));
}
}
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));
}
}
Aggregations