in codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/SyncClientClass.java [252:349]
private MethodSpec traditionalMethod(OperationModel opModel) {
MethodSpec.Builder method = SyncClientInterface.operationMethodSignature(model, opModel)
.addAnnotation(Override.class);
addRequestModifierCode(opModel, model).ifPresent(method::addCode);
if (!useSraAuth) {
method.addCode(ClientClassUtils.callApplySignerOverrideMethod(opModel));
}
method.addCode(protocolSpec.responseHandler(model, opModel));
protocolSpec.errorResponseHandler(opModel).ifPresent(method::addCode);
if (opModel.getEndpointDiscovery() != null) {
method.addStatement("boolean endpointDiscoveryEnabled = "
+ "clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED)");
method.addStatement("boolean endpointOverridden = "
+ "clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER)"
+ ".isEndpointOverridden()");
if (opModel.getEndpointDiscovery().isRequired()) {
if (!model.getCustomizationConfig().allowEndpointOverrideForEndpointDiscoveryRequiredOperations()) {
method.beginControlFlow("if (endpointOverridden)");
method.addStatement("throw new $T($S)", IllegalStateException.class,
"This operation requires endpoint discovery, but an endpoint override was specified "
+ "when the client was created. This is not supported.");
method.endControlFlow();
method.beginControlFlow("if (!endpointDiscoveryEnabled)");
method.addStatement("throw new $T($S)", IllegalStateException.class,
"This operation requires endpoint discovery, but endpoint discovery was disabled on the "
+ "client.");
method.endControlFlow();
} else {
method.beginControlFlow("if (endpointOverridden)");
method.addStatement("endpointDiscoveryEnabled = false");
method.nextControlFlow("else if (!endpointDiscoveryEnabled)");
method.addStatement("throw new $T($S)", IllegalStateException.class,
"This operation requires endpoint discovery to be enabled, or for you to specify an "
+ "endpoint override when the client is created.");
method.endControlFlow();
}
}
method.addStatement("$T cachedEndpoint = null", URI.class);
method.beginControlFlow("if (endpointDiscoveryEnabled)");
ParameterizedTypeName identityFutureTypeName =
ParameterizedTypeName.get(ClassName.get(CompletableFuture.class),
WildcardTypeName.subtypeOf(AwsCredentialsIdentity.class));
method.addCode("$T identityFuture = $N.overrideConfiguration()",
identityFutureTypeName,
opModel.getInput().getVariableName())
.addCode(" .flatMap($T::credentialsIdentityProvider)", AwsRequestOverrideConfiguration.class)
.addCode(" .orElseGet(() -> clientConfiguration.option($T.CREDENTIALS_IDENTITY_PROVIDER))",
AwsClientOption.class)
.addCode(" .resolveIdentity();");
method.addCode("$T key = $T.joinLikeSync(identityFuture).accessKeyId();", String.class, CompletableFutureUtils.class);
method.addCode("$1T endpointDiscoveryRequest = $1T.builder()", EndpointDiscoveryRequest.class)
.addCode(" .required($L)", opModel.getInputShape().getEndpointDiscovery().isRequired())
.addCode(" .defaultEndpoint(clientConfiguration.option($T.CLIENT_ENDPOINT_PROVIDER).clientEndpoint())",
SdkClientOption.class)
.addCode(" .overrideConfiguration($N.overrideConfiguration().orElse(null))",
opModel.getInput().getVariableName())
.addCode(" .build();");
method.addStatement("cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest)");
method.endControlFlow();
}
method.addStatement("$T clientConfiguration = updateSdkClientConfiguration($L, this.clientConfiguration)",
SdkClientConfiguration.class, opModel.getInput().getVariableName());
method.addStatement("$T<$T> metricPublishers = "
+ "resolveMetricPublishers(clientConfiguration, $N.overrideConfiguration().orElse(null))",
List.class,
MetricPublisher.class,
opModel.getInput().getVariableName())
.addStatement("$1T apiCallMetricCollector = metricPublishers.isEmpty() ? $2T.create() : $1T.create($3S)",
MetricCollector.class, NoOpMetricCollector.class, "ApiCall");
method.beginControlFlow("try")
.addStatement("apiCallMetricCollector.reportMetric($T.$L, $S)",
CoreMetric.class, "SERVICE_ID", model.getMetadata().getServiceId())
.addStatement("apiCallMetricCollector.reportMetric($T.$L, $S)",
CoreMetric.class, "OPERATION_NAME", opModel.getOperationName());
addS3ArnableFieldCode(opModel, model).ifPresent(method::addCode);
method.addCode(ClientClassUtils.addEndpointTraitCode(opModel));
method.addCode(protocolSpec.executionHandler(opModel))
.endControlFlow()
.beginControlFlow("finally")
.addStatement("metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()))")
.endControlFlow();
return method.build();
}