use of com.palantir.dialogue.Request in project conjure-java-runtime by palantir.
the class JaxRsClientDialogueEndpointTest method testPostWithBody.
@Test
public void testPostWithBody() {
Channel channel = stubNoContentResponseChannel();
StubService service = JaxRsClient.create(StubService.class, channel, runtime);
service.post("Hello, World!");
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("post");
assertThat(endpoint.httpMethod()).isEqualTo(HttpMethod.POST);
Request request = requestCaptor.getValue();
assertThat(request.body()).isPresent();
assertThat(request.body().get().contentType()).isEqualTo("text/plain");
assertThat(request.headerParams().asMap()).containsExactly(new AbstractMap.SimpleImmutableEntry<>("Accept", ImmutableList.of("application/json")), new AbstractMap.SimpleImmutableEntry<>("Content-Length", ImmutableList.of("13")));
}
use of com.palantir.dialogue.Request in project dialogue by palantir.
the class ServiceImplementationGenerator method clientImpl.
private MethodSpec clientImpl(EndpointDefinition def) {
List<ParameterSpec> params = def.arguments().stream().map(arg -> ParameterSpec.builder(ArgumentTypes.caseOf(arg.argType()).primitive((javaTypeName, _parameterSerializerMethodName, _isList) -> javaTypeName).rawRequestBody(typeName -> typeName).optional((optionalJavaType, _unused) -> optionalJavaType).mapType(typeName -> typeName).customType(typeName -> typeName), arg.argName().get()).build()).collect(Collectors.toList());
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(def.endpointName().get()).addModifiers(Modifier.PUBLIC).addParameters(params).addAnnotation(Override.class);
methodBuilder.addCode("$T $L = $T.builder();", Request.Builder.class, REQUEST, Request.class);
def.arguments().forEach(arg -> methodBuilder.addCode(generateParam(arg)));
methodBuilder.returns(def.returns().returnType());
boolean isAsync = def.returns().asyncInnerType().isPresent();
String executeCode = isAsync ? "$L.clients().call($L, $L.build(), $L);" : "$L.clients().callBlocking($L, $L.build(), $L);";
CodeBlock execute = CodeBlock.of(executeCode, serviceDefinition.conjureRuntimeArgName(), def.channelFieldName(), REQUEST, def.returns().deserializerFieldName());
methodBuilder.addCode(!def.returns().isVoid() || isAsync ? "return $L" : "$L", execute);
return methodBuilder.build();
}
use of com.palantir.dialogue.Request in project dialogue by palantir.
the class NoResponseTest method testConnectionClosedAfterDelay.
@Test
public void testConnectionClosedAfterDelay() {
// This test closes the connection after taking some time to "process"
// the request. We expect the client _not_ to retry in this case.
AtomicInteger requests = new AtomicInteger();
Undertow server = createStarted(new BlockingHandler(exchange -> {
requests.incrementAndGet();
Uninterruptibles.sleepUninterruptibly(Duration.ofSeconds(5));
exchange.getConnection().close();
}));
try {
Channel channel = create(defaultClientConfig(getPort(server)));
assertThatThrownBy(channel.execute(TestEndpoint.POST, request)::get).hasCauseInstanceOf(SocketTimeoutException.class);
assertThat(requests).as("Request mustn't be retried").hasValue(1);
} finally {
server.stop();
}
}
use of com.palantir.dialogue.Request in project dialogue by palantir.
the class ApacheHttpClientChannelsTest method testContentLength.
@SuppressWarnings("FutureReturnValueIgnored")
private void testContentLength(Optional<String> headerValue, Optional<RequestBody> body, long value) {
try {
endpoint.method = HttpMethod.POST;
Request.Builder builder = Request.builder().from(request).body(body);
headerValue.ifPresent(header -> builder.putHeaderParams(HttpHeaders.CONTENT_LENGTH, header));
request = builder.build();
channel.execute(endpoint, request);
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("POST");
assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_LENGTH)).isEqualTo(Long.toString(value));
assertThat(recordedRequest.getHeader(HttpHeaders.TRANSFER_ENCODING)).isNull();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of com.palantir.dialogue.Request in project dialogue by palantir.
the class SimulationServer method execute.
@Override
public ListenableFuture<Response> execute(Endpoint endpoint, Request request) {
Meter perEndpointRequests = MetricNames.requestMeter(simulation.taggedMetrics(), serverName, endpoint);
activeRequests.inc();
perEndpointRequests.mark();
simulation.metricsReporter().report();
for (ServerHandler handler : handlers) {
long beforeNanos = simulation.clock().read();
Optional<ListenableFuture<Response>> maybeResp = handler.maybeExecute(this, endpoint, request);
if (!maybeResp.isPresent()) {
continue;
}
ListenableFuture<Response> resp = maybeResp.get();
DialogueFutures.addDirectCallback(resp, DialogueFutures.onSuccess(_ignored -> globalResponses.inc()));
resp.addListener(() -> {
activeRequests.dec();
globalServerTimeNanos.inc(simulation.clock().read() - beforeNanos);
}, DialogueFutures.safeDirectExecutor());
if (log.isDebugEnabled()) {
DialogueFutures.addDirectCallback(resp, DialogueFutures.onSuccess(result -> {
log.debug("time={} server={} status={} id={}", Duration.ofNanos(simulation.clock().read()), serverName, result.code(), request != null ? request.headerParams().get(Benchmark.REQUEST_ID_HEADER) : null);
}));
}
return resp;
}
log.error("No handler available for request {}", request);
activeRequests.dec();
return Futures.immediateFailedFuture(new SafeRuntimeException("No handler"));
}
Aggregations