use of com.linecorp.armeria.client.WebClient in project scribejava by scribejava.
the class ArmeriaHttpClient method doExecuteAsync.
private <T> CompletableFuture<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, Supplier<HttpData> contentSupplier, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
// Get the URI and Path
final URI uri = URI.create(completeUrl);
final String path = getServicePath(uri);
// Fetch/Create WebClient instance for a given Endpoint
final WebClient client = getClient(uri);
// Build HTTP request
final RequestHeadersBuilder headersBuilder = RequestHeaders.of(getHttpMethod(httpVerb), path).toBuilder();
headersBuilder.add(headers.entrySet());
if (userAgent != null) {
headersBuilder.add(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
}
// Build the request body and execute HTTP request
final HttpResponse response;
if (httpVerb.isPermitBody()) {
// POST, PUT, PATCH and DELETE methods
final HttpData contents = contentSupplier.get();
if (httpVerb.isRequiresBody() && contents == null) {
// POST or PUT methods
throw new IllegalArgumentException("Contents missing for request method " + httpVerb.name());
}
if (headersBuilder.contentType() == null) {
headersBuilder.contentType(MediaType.FORM_DATA);
}
if (contents != null) {
response = client.execute(headersBuilder.build(), contents);
} else {
response = client.execute(headersBuilder.build());
}
} else {
response = client.execute(headersBuilder.build());
}
// Aggregate HTTP response (asynchronously) and return the result Future
return response.aggregate().thenApply(aggregatedResponse -> whenResponseComplete(callback, converter, aggregatedResponse)).exceptionally(throwable -> completeExceptionally(callback, throwable));
}
use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.
the class HTTPSourceTest method testHTTPJsonResponse415.
@Test
public void testHTTPJsonResponse415() {
// Prepare
final int testMaxPendingRequests = 1;
final int testThreadCount = 1;
final int serverTimeoutInMillis = 500;
when(sourceConfig.getRequestTimeoutInMillis()).thenReturn(serverTimeoutInMillis);
when(sourceConfig.getMaxPendingRequests()).thenReturn(testMaxPendingRequests);
when(sourceConfig.getThreadCount()).thenReturn(testThreadCount);
HTTPSourceUnderTest = new HTTPSource(sourceConfig, pluginMetrics, pluginFactory);
// Start the source
HTTPSourceUnderTest.start(testBuffer);
refreshMeasurements();
final RequestHeaders testRequestHeaders = RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build();
final HttpData testHttpData = HttpData.ofUtf8("[{\"log\": \"somelog\"}]");
// Fill in the buffer
WebClient.of().execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
// Disable client timeout
WebClient testWebClient = WebClient.builder().responseTimeoutMillis(0).build();
// When/Then
testWebClient.execute(testRequestHeaders, testHttpData).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.REQUEST_TIMEOUT)).join();
// verify metrics
final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
Assertions.assertEquals(2.0, requestReceivedCount.getValue());
final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
Assertions.assertEquals(1.0, successRequestsCount.getValue());
final Measurement requestTimeoutsCount = MetricsTestUtil.getMeasurementFromList(requestTimeoutsMeasurements, Statistic.COUNT);
Assertions.assertEquals(1.0, requestTimeoutsCount.getValue());
final Measurement requestProcessDurationMax = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.MAX);
final double maxDurationInMillis = 1000 * requestProcessDurationMax.getValue();
Assertions.assertTrue(maxDurationInMillis > serverTimeoutInMillis);
}
use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.
the class UnauthenticatedArmeriaHttpAuthenticationProviderTest method httpRequest_with_BasicAuthentication_responds_OK.
@Test
void httpRequest_with_BasicAuthentication_responds_OK() {
final WebClient client = WebClient.builder(server.httpUri()).auth(BasicToken.of(UUID.randomUUID().toString(), UUID.randomUUID().toString())).build();
final AggregatedHttpResponse httpResponse = client.get("/test").aggregate().join();
assertThat(httpResponse.status(), equalTo(HttpStatus.OK));
}
use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.
the class UnauthenticatedArmeriaHttpAuthenticationProviderTest method httpRequest_without_authentication_responds_OK.
@Test
void httpRequest_without_authentication_responds_OK() {
final WebClient client = WebClient.of(server.httpUri());
final AggregatedHttpResponse httpResponse = client.get("/test").aggregate().join();
assertThat(httpResponse.status(), equalTo(HttpStatus.OK));
}
use of com.linecorp.armeria.client.WebClient in project data-prepper by opensearch-project.
the class UnauthenticatedGrpcAuthenticationProviderTest method httpRequest_with_BasicAuthentication_responds_OK.
@Test
void httpRequest_with_BasicAuthentication_responds_OK() {
final WebClient client = WebClient.builder(server.httpUri()).auth(BasicToken.of(UUID.randomUUID().toString(), UUID.randomUUID().toString())).build();
HttpRequest request = HttpRequest.of(RequestHeaders.builder().method(HttpMethod.POST).path("/grpc.health.v1.Health/Check").contentType(MediaType.JSON_UTF_8).build());
final AggregatedHttpResponse httpResponse = client.execute(request).aggregate().join();
// TODO: Figure out how to get SampleHealthGrpcService to return a status of 200
assertThat(httpResponse.status(), equalTo(HttpStatus.SERVICE_UNAVAILABLE));
}
Aggregations