use of com.palantir.conjure.spec.EndpointDefinition in project conjure-java by palantir.
the class DefaultStaticFactoryMethodGenerator method clientImpl.
private MethodSpec clientImpl(EndpointDefinition def) {
List<ParameterSpec> params = parameterTypes.implementationMethodParams(def);
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(def.getEndpointName().get()).addModifiers(Modifier.PUBLIC).addParameters(params).addAnnotation(Override.class);
if (def.getDeprecated().isPresent()) {
methodBuilder.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build());
}
TypeName returnType = methodType.switchBy(returnTypes.baseType(def.getReturns()), returnTypes.async(def.getReturns()));
methodBuilder.returns(returnType);
CodeBlock.Builder requestParams = CodeBlock.builder();
def.getAuth().map(DefaultStaticFactoryMethodGenerator::generateAuthHeader).ifPresent(requestParams::add);
def.getArgs().stream().map(param -> generateParam(def.getEndpointName().get(), param)).forEach(requestParams::add);
CodeBlock request = CodeBlock.builder().add("$T $L = $T.builder();", Request.Builder.class, REQUEST, Request.class).add(requestParams.build()).build();
String codeBlock = methodType.switchBy("$L.clients().callBlocking($L, $L.build(), $L);", "$L.clients().call($L, $L.build" + "(), $L);");
CodeBlock execute = CodeBlock.of(codeBlock, StaticFactoryMethodGenerator.RUNTIME, Names.endpointChannel(def), REQUEST, def.getReturns().filter(type -> isBinaryOrOptionalBinary(returnTypes.baseType(type), returnTypes)).map(type -> StaticFactoryMethodGenerator.RUNTIME + (isOptionalBinary(returnTypes.baseType(type), returnTypes) ? ".bodySerDe().optionalInputStreamDeserializer()" : ".bodySerDe().inputStreamDeserializer()")).orElseGet(() -> def.getEndpointName().get() + "Deserializer"));
methodBuilder.addCode(request);
methodBuilder.addCode(methodType.switchBy(def.getReturns().isPresent() ? "return " : "", "return "));
methodBuilder.addCode(execute);
return methodBuilder.build();
}
use of com.palantir.conjure.spec.EndpointDefinition in project conjure-java by palantir.
the class DialogueEndpointsGenerator method addTags.
private static void addTags(EndpointDefinition def, TypeSpec.Builder destination) {
if (!def.getTags().isEmpty()) {
CodeBlock arrayValues = CodeBlock.join(Collections2.transform(def.getTags(), value -> CodeBlock.of("$S", value)), ", ");
destination.addField(FieldSpec.builder(ParameterizedTypeName.get(ImmutableSet.class, String.class), "tags", Modifier.PRIVATE, Modifier.FINAL).initializer(CodeBlock.of("$T.of($L)", ImmutableSet.class, arrayValues)).build());
destination.addMethod(MethodSpec.methodBuilder("tags").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(ParameterizedTypeName.get(Set.class, String.class)).addStatement("return tags").build());
}
}
use of com.palantir.conjure.spec.EndpointDefinition in project conjure-java by palantir.
the class UndertowServiceHandlerGenerator method generateServiceHandler.
public JavaFile generateServiceHandler(ServiceDefinition serviceDefinition, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typeDefinitions, TypeMapper typeMapper, TypeMapper returnTypeMapper) {
String serviceName = serviceDefinition.getServiceName().getName();
// class name
ClassName serviceClass = ClassName.get(Packages.getPrefixedPackage(serviceDefinition.getServiceName().getPackage(), options.packagePrefix()), (options.undertowServicePrefix() ? "Undertow" : "") + serviceDefinition.getServiceName().getName());
TypeSpec.Builder factory = TypeSpec.classBuilder(serviceName + "Factory").addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
// addFields
factory.addField(serviceClass, DELEGATE_VAR_NAME, Modifier.PRIVATE, Modifier.FINAL);
factory.addField(ClassName.get(UndertowRuntime.class), RUNTIME_VAR_NAME, Modifier.PRIVATE, Modifier.FINAL);
// addConstructor
factory.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(UndertowRuntime.class, RUNTIME_VAR_NAME).addParameter(serviceClass, DELEGATE_VAR_NAME).addStatement("this.$1N = $1N", RUNTIME_VAR_NAME).addStatement("this.$1N = $1N", DELEGATE_VAR_NAME).build());
ClassName serviceType = ClassName.get(Packages.getPrefixedPackage(serviceDefinition.getServiceName().getPackage(), options.packagePrefix()), serviceDefinition.getServiceName().getName() + "Endpoints");
CodeBlock endpointBlock = CodeBlock.builder().build();
for (EndpointDefinition e : serviceDefinition.getEndpoints()) {
CodeBlock nextBlock = CodeBlock.of("new $1T($2N, $3N)", endpointToHandlerType(serviceDefinition.getServiceName(), e.getEndpointName()), RUNTIME_VAR_NAME, DELEGATE_VAR_NAME);
endpointBlock = endpointBlock.isEmpty() ? nextBlock : CodeBlock.of("$1L, $2L", endpointBlock, nextBlock);
}
TypeSpec endpoints = TypeSpec.classBuilder(serviceType.simpleName()).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(UndertowServiceHandlerGenerator.class)).addSuperinterface(UndertowService.class).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addField(FieldSpec.builder(serviceClass, DELEGATE_VAR_NAME, Modifier.PRIVATE, Modifier.FINAL).build()).addMethod(MethodSpec.methodBuilder("of").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(serviceClass, DELEGATE_VAR_NAME).addStatement("return new $T($N)", serviceType, DELEGATE_VAR_NAME).returns(UndertowService.class).build()).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(serviceClass, DELEGATE_VAR_NAME).addStatement("this.$1N = $1N", DELEGATE_VAR_NAME).build()).addMethod(MethodSpec.methodBuilder("endpoints").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(UndertowRuntime.class, RUNTIME_VAR_NAME).returns(ParameterizedTypeName.get(List.class, Endpoint.class)).addStatement("return $1T.of($2L)", ImmutableList.class, endpointBlock).build()).addTypes(Lists.transform(serviceDefinition.getEndpoints(), e -> generateEndpointHandler(e, serviceDefinition, serviceClass, typeDefinitions, typeMapper, returnTypeMapper))).build();
return JavaFile.builder(Packages.getPrefixedPackage(serviceDefinition.getServiceName().getPackage(), options.packagePrefix()), endpoints).skipJavaLangImports(true).indent(" ").build();
}
use of com.palantir.conjure.spec.EndpointDefinition in project conjure-java by palantir.
the class DialogueInterfaceGenerator method apiMethod.
private MethodSpec apiMethod(EndpointDefinition endpointDef, Function<Optional<Type>, TypeName> returnTypeMapper) {
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(endpointDef.getEndpointName().get()).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).addParameters(parameterTypes.interfaceMethodParams(endpointDef)).addAnnotations(ConjureAnnotations.getClientEndpointAnnotations(endpointDef));
endpointDef.getMarkers().stream().filter(marker -> !marker.accept(IsUndertowAsyncMarkerVisitor.INSTANCE)).map(marker -> {
Preconditions.checkState(marker.accept(TypeVisitor.IS_REFERENCE), "Endpoint marker must be a reference type", SafeArg.of("marker", marker));
return marker.accept(TypeVisitor.REFERENCE);
}).forEach(referenceType -> methodBuilder.addAnnotation(ClassName.get(referenceType.getPackage(), referenceType.getName())));
endpointDef.getDeprecated().ifPresent(deprecatedDocsValue -> methodBuilder.addAnnotation(Deprecated.class));
methodBuilder.addJavadoc("$L", ServiceGenerators.getJavaDocWithRequestLine(endpointDef));
TypeName returnType = returnTypeMapper.apply(endpointDef.getReturns());
methodBuilder.returns(returnType);
if (TypeName.get(InputStream.class).equals(returnType)) {
methodBuilder.addAnnotation(MustBeClosed.class);
}
return methodBuilder.build();
}
use of com.palantir.conjure.spec.EndpointDefinition in project conjure by palantir.
the class ConjureParserUtils method parseService.
static ServiceDefinition parseService(com.palantir.conjure.parser.services.ServiceDefinition parsed, TypeName serviceName, ReferenceTypeResolver typeResolver, DealiasingTypeVisitor dealiasingVisitor) {
List<EndpointDefinition> endpoints = new ArrayList<>();
parsed.endpoints().forEach((name, def) -> endpoints.add(ConjureParserUtils.parseEndpoint(name, def, parsed.basePath(), parseAuthType(parsed.defaultAuth()), typeResolver, dealiasingVisitor)));
ServiceDefinition service = ServiceDefinition.builder().serviceName(serviceName).docs(parsed.docs().map(Documentation::of)).addAllEndpoints(endpoints).build();
ServiceDefinitionValidator.validateAll(service);
return service;
}
Aggregations