use of com.palantir.humanreadabletypes.HumanReadableDuration in project conjure-java by palantir.
the class UndertowServiceHandlerGenerator method endpointInvocation.
private CodeBlock endpointInvocation(EndpointDefinition endpointDefinition, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typeDefinitions, TypeMapper typeMapper, TypeMapper returnTypeMapper) {
CodeBlock.Builder code = CodeBlock.builder();
// auth code
Optional<String> authVarName = addAuthCode(code, endpointDefinition);
// body parameter
getBodyParamTypeArgument(endpointDefinition.getArgs()).ifPresent(bodyParam -> {
String paramName = sanitizeVarName(bodyParam.getArgName().get(), endpointDefinition);
Type dealiased = TypeFunctions.toConjureTypeWithoutAliases(bodyParam.getType(), typeDefinitions);
if (TypeFunctions.isBinaryOrOptionalBinary(dealiased)) {
code.addStatement("$1T $2N = $3N.bodySerDe().deserializeInputStream($4N)", InputStream.class, paramName, RUNTIME_VAR_NAME, EXCHANGE_VAR_NAME);
} else {
code.addStatement("$1T $2N = $3N.deserialize($4N)", typeMapper.getClassName(bodyParam.getType()).box(), paramName, DESERIALIZER_VAR_NAME, EXCHANGE_VAR_NAME);
}
code.add(generateParamMetadata(bodyParam, bodyParam.getArgName().get(), paramName, typeMapper));
});
// path parameters
addPathParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
// header parameters
addHeaderParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
// query parameters
addQueryParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
List<CodeBlock> methodArgs = new ArrayList<>();
authVarName.ifPresent(name -> methodArgs.add(CodeBlock.of("$N", name)));
ParameterOrder.sorted(endpointDefinition.getArgs()).stream().map(arg -> arg.getArgName().get()).map(arg -> sanitizeVarName(arg, endpointDefinition)).map(arg -> CodeBlock.of("$N", arg)).forEach(methodArgs::add);
if (Tags.hasServerRequestContext(endpointDefinition)) {
methodArgs.add(CodeBlock.of("$N.contexts().createContext($N, this)", RUNTIME_VAR_NAME, EXCHANGE_VAR_NAME));
}
Optional<AsyncRequestProcessingMetadata> async = UndertowTypeFunctions.async(endpointDefinition, options);
if (async.isPresent() || endpointDefinition.getReturns().isPresent()) {
code.addStatement("$1T $2N = $3N.$4L($5L)", async.isPresent() ? UndertowTypeFunctions.getAsyncReturnType(endpointDefinition, returnTypeMapper, options) : returnTypeMapper.getClassName(endpointDefinition.getReturns().get()), RESULT_VAR_NAME, DELEGATE_VAR_NAME, JavaNameSanitizer.sanitize(endpointDefinition.getEndpointName().get()), methodArgs.stream().collect(CodeBlock.joining(",")));
} else {
code.addStatement("$1N.$2L($3L)", DELEGATE_VAR_NAME, endpointDefinition.getEndpointName(), methodArgs.stream().collect(CodeBlock.joining(",")));
}
if (async.isPresent()) {
AsyncRequestProcessingMetadata metadata = async.get();
if (metadata.timeout().isPresent()) {
HumanReadableDuration timeout = metadata.timeout().get();
code.add(CodeBlocks.statement("$N.async().register($N, this, $T.ofMillis(/* $L */ $L), $N)", RUNTIME_VAR_NAME, RESULT_VAR_NAME, Duration.class, timeout.toString(), timeout.toMilliseconds(), EXCHANGE_VAR_NAME));
} else {
code.add(CodeBlocks.statement("$1N.async().register($2N, this, $3N)", RUNTIME_VAR_NAME, RESULT_VAR_NAME, EXCHANGE_VAR_NAME));
}
} else {
code.add(generateReturnValueCodeBlock(endpointDefinition, typeDefinitions));
}
return code.build();
}
Aggregations