Search in sources :

Example 1 with Request

use of com.palantir.dialogue.Request 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 Request

use of com.palantir.dialogue.Request 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 Request

use of com.palantir.dialogue.Request 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 Request

use of com.palantir.dialogue.Request 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 Request

use of com.palantir.dialogue.Request 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

Request (com.palantir.dialogue.Request)40 Test (org.junit.jupiter.api.Test)21 Channel (com.palantir.dialogue.Channel)14 Endpoint (com.palantir.dialogue.Endpoint)14 Response (com.palantir.dialogue.Response)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 TestResponse (com.palantir.dialogue.TestResponse)8 Test (org.junit.Test)8 TestEndpoint (com.palantir.dialogue.TestEndpoint)6 Optional (java.util.Optional)6 Futures (com.google.common.util.concurrent.Futures)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 RequestBody (com.palantir.dialogue.RequestBody)5 UrlBuilder (com.palantir.dialogue.UrlBuilder)5 OutputStream (java.io.OutputStream)5 Duration (java.time.Duration)5 ExecutionException (java.util.concurrent.ExecutionException)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 ImmutableList (com.google.common.collect.ImmutableList)4