Search in sources :

Example 1 with EndpointDefinition

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();
}
Also used : HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ClassName(com.squareup.javapoet.ClassName) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Options(com.palantir.conjure.java.Options) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) ParameterSpec(com.squareup.javapoet.ParameterSpec) ImmutableMap(com.google.common.collect.ImmutableMap) AuthType(com.palantir.conjure.spec.AuthType) Deserializer(com.palantir.dialogue.Deserializer) BodyParameterType(com.palantir.conjure.spec.BodyParameterType) ParameterType(com.palantir.conjure.spec.ParameterType) Objects(java.util.Objects) List(java.util.List) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) TypeMarker(com.palantir.dialogue.TypeMarker) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) ListType(com.palantir.conjure.spec.ListType) Auth(com.palantir.conjure.java.services.Auth) ExternalReference(com.palantir.conjure.spec.ExternalReference) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) PathParameterType(com.palantir.conjure.spec.PathParameterType) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) Serializer(com.palantir.dialogue.Serializer) PlainSerDe(com.palantir.dialogue.PlainSerDe) SafeArg(com.palantir.logsafe.SafeArg) ConjureRuntime(com.palantir.dialogue.ConjureRuntime) SetType(com.palantir.conjure.spec.SetType) Request(com.palantir.dialogue.Request) CodeBlock(com.squareup.javapoet.CodeBlock) EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) EndpointName(com.palantir.conjure.spec.EndpointName) EndpointChannel(com.palantir.dialogue.EndpointChannel) QueryParameterType(com.palantir.conjure.spec.QueryParameterType) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) HeaderParameterType(com.palantir.conjure.spec.HeaderParameterType) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) Request(com.palantir.dialogue.Request)

Example 2 with EndpointDefinition

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());
    }
}
Also used : PathTemplate(com.palantir.dialogue.PathTemplate) ListMultimap(com.google.common.collect.ListMultimap) Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) Collections2(com.google.common.collect.Collections2) HttpPath(com.palantir.conjure.spec.HttpPath) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Options(com.palantir.conjure.java.Options) Endpoint(com.palantir.dialogue.Endpoint) Splitter(com.google.common.base.Splitter) CodeBlock(com.squareup.javapoet.CodeBlock) ImmutableSet(com.google.common.collect.ImmutableSet) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) HttpMethod(com.palantir.dialogue.HttpMethod) TypeSpec(com.squareup.javapoet.TypeSpec) JavaFile(com.squareup.javapoet.JavaFile) UrlBuilder(com.palantir.dialogue.UrlBuilder) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) UriTemplateParser(org.glassfish.jersey.uri.internal.UriTemplateParser) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) ImmutableSet(com.google.common.collect.ImmutableSet) CodeBlock(com.squareup.javapoet.CodeBlock)

Example 3 with EndpointDefinition

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();
}
Also used : ClassName(com.squareup.javapoet.ClassName) UndertowRuntime(com.palantir.conjure.java.undertow.lib.UndertowRuntime) CodeBlock(com.squareup.javapoet.CodeBlock) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) HttpString(io.undertow.util.HttpString) TypeSpec(com.squareup.javapoet.TypeSpec) UndertowService(com.palantir.conjure.java.undertow.lib.UndertowService)

Example 4 with EndpointDefinition

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();
}
Also used : TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) Type(com.palantir.conjure.spec.Type) ClassName(com.squareup.javapoet.ClassName) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) MustBeClosed(com.google.errorprone.annotations.MustBeClosed) SafeArg(com.palantir.logsafe.SafeArg) ConjureRuntime(com.palantir.dialogue.ConjureRuntime) Endpoint(com.palantir.dialogue.Endpoint) DialogueService(com.palantir.dialogue.DialogueService) CodeBlock(com.squareup.javapoet.CodeBlock) EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Channel(com.palantir.dialogue.Channel) TypeSpec(com.squareup.javapoet.TypeSpec) IsUndertowAsyncMarkerVisitor(com.palantir.conjure.java.services.IsUndertowAsyncMarkerVisitor) DialogueServiceFactory(com.palantir.dialogue.DialogueServiceFactory) JavaFile(com.squareup.javapoet.JavaFile) Collectors.toList(java.util.stream.Collectors.toList) EndpointChannel(com.palantir.dialogue.EndpointChannel) ServiceGenerators(com.palantir.conjure.java.services.ServiceGenerators) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Preconditions(com.palantir.logsafe.Preconditions) InputStream(java.io.InputStream) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) InputStream(java.io.InputStream)

Example 5 with EndpointDefinition

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;
}
Also used : ArrayList(java.util.ArrayList) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition)

Aggregations

EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)12 ServiceDefinition (com.palantir.conjure.spec.ServiceDefinition)9 ClassName (com.squareup.javapoet.ClassName)8 TypeSpec (com.squareup.javapoet.TypeSpec)8 Optional (java.util.Optional)8 Options (com.palantir.conjure.java.Options)7 MethodSpec (com.squareup.javapoet.MethodSpec)7 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)7 TypeName (com.squareup.javapoet.TypeName)7 Modifier (javax.lang.model.element.Modifier)7 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)6 Packages (com.palantir.conjure.java.util.Packages)6 ArgumentDefinition (com.palantir.conjure.spec.ArgumentDefinition)6 AuthType (com.palantir.conjure.spec.AuthType)6 ParameterType (com.palantir.conjure.spec.ParameterType)6 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)6 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)6 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 TypeMapper (com.palantir.conjure.java.types.TypeMapper)4