Search in sources :

Example 1 with Endpoint

use of com.palantir.dialogue.Endpoint 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 2 with Endpoint

use of com.palantir.dialogue.Endpoint in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testSlashPathParameter.

@Test
public void testSlashPathParameter() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.innerPath("/");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    UrlBuilder urlBuilder = mock(UrlBuilder.class);
    endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
    // context path
    verify(urlBuilder).pathSegment("foo");
    // encoded into %2F by DefaultUrlBuilder
    verify(urlBuilder).pathSegment("/");
}
Also used : Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) UrlBuilder(com.palantir.dialogue.UrlBuilder) Test(org.junit.Test)

Example 3 with Endpoint

use of com.palantir.dialogue.Endpoint in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testEndpoint.

@Test
public void testEndpoint() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.ping();
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    Endpoint endpoint = endpointCaptor.getValue();
    assertThat(endpoint.serviceName()).isEqualTo("StubService");
    assertThat(endpoint.endpointName()).isEqualTo("ping");
    assertThat(endpoint.httpMethod()).isEqualTo(HttpMethod.GET);
    Request request = requestCaptor.getValue();
    assertThat(request.body()).isEmpty();
    assertThat(request.headerParams().asMap()).containsExactly(new AbstractMap.SimpleImmutableEntry<>("Accept", ImmutableList.of("application/json")));
}
Also used : AbstractMap(java.util.AbstractMap) Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) Test(org.junit.Test)

Example 4 with Endpoint

use of com.palantir.dialogue.Endpoint in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testTrailingWildcardParameter_slashes.

@Test
public void testTrailingWildcardParameter_slashes() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.complexPath("dynamic0", "dynamic1/dynamic2");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    UrlBuilder urlBuilder = mock(UrlBuilder.class);
    endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
    // context path
    verify(urlBuilder).pathSegment("foo");
    verify(urlBuilder).pathSegment("static0");
    verify(urlBuilder).pathSegment("dynamic0");
    verify(urlBuilder).pathSegment("static1");
    // Value should not be split into multiple segments
    verify(urlBuilder).pathSegment("dynamic1/dynamic2");
}
Also used : Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) UrlBuilder(com.palantir.dialogue.UrlBuilder) Test(org.junit.Test)

Example 5 with Endpoint

use of com.palantir.dialogue.Endpoint in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testQueryParameterCollection.

@Test
public void testQueryParameterCollection() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.collectionOfQueryParams(ImmutableList.of("a", "/", "", "a b", "a+b"));
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    UrlBuilder urlBuilder = mock(UrlBuilder.class);
    endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
    verify(urlBuilder).queryParam("query", "a");
    verify(urlBuilder).queryParam("query", "/");
    verify(urlBuilder).queryParam("query", "");
    verify(urlBuilder).queryParam("query", "a b");
    verify(urlBuilder).queryParam("query", "a+b");
}
Also used : Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) UrlBuilder(com.palantir.dialogue.UrlBuilder) Test(org.junit.Test)

Aggregations

Endpoint (com.palantir.dialogue.Endpoint)30 Channel (com.palantir.dialogue.Channel)17 Request (com.palantir.dialogue.Request)15 Response (com.palantir.dialogue.Response)14 Test (org.junit.jupiter.api.Test)11 TestEndpoint (com.palantir.dialogue.TestEndpoint)10 TestResponse (com.palantir.dialogue.TestResponse)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 Test (org.junit.Test)8 EndpointChannel (com.palantir.dialogue.EndpointChannel)6 UrlBuilder (com.palantir.dialogue.UrlBuilder)6 Optional (java.util.Optional)6 Random (java.util.Random)6 Futures (com.google.common.util.concurrent.Futures)5 HttpMethod (com.palantir.dialogue.HttpMethod)5 IOException (java.io.IOException)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Meter (com.codahale.metrics.Meter)4 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)4