/** * Call this before one of the {@code executeAsyncHttpRequest(...)} methods in order to get a request builder you * can populate with query params, headers, body, etc. If you want to specify a custom circuit breaker (or disable * circuit breaking entirely) for this call then use {@link #getRequestBuilder(String, HttpMethod, Optional, * boolean)} instead. This method tells the HTTP client to use a default circuit breaker based on the host being * called. */ public RequestBuilderWrapper getRequestBuilder(String url, HttpMethod method) { return getRequestBuilder(url, method, Optional.empty(), false); }
/** * @param asyncHttpClientHelper The async HTTP client you want this method to use to make the AWS metadata call. * * @return A {@link CompletableFuture} that will contain the AWS instance ID this app is running on (assuming it * completes successfully). If an error occurs retrieving the instance ID from AWS then the error will be logged and * {@link AppInfo#UNKNOWN_VALUE} returned as the value. */ public static CompletableFuture<String> getAwsInstanceId(AsyncHttpClientHelper asyncHttpClientHelper) { return asyncHttpClientHelper.executeAsyncHttpRequest( asyncHttpClientHelper.getRequestBuilder(AMAZON_METADATA_INSTANCE_ID_URL, HttpMethod.GET), Response::getResponseBody ).handle((instanceId, error) -> { if (error != null) { logger.error("Unable to get instance ID info from AWS metadata service.", error); return AppInfo.UNKNOWN_VALUE; } if (instanceId == null) { logger.error("AWS metadata service returned null for instance ID. Using 'unknown' as fallback."); return AppInfo.UNKNOWN_VALUE; } return instanceId; }); }
asyncHttpClientHelper.getRequestBuilder(AMAZON_METADATA_DOCUMENT_URL, HttpMethod.GET), response -> { String region = null;
@Test public void getRequestBuilder_delegates_to_helper_with_default_circuit_breaker_args() { // given String url = UUID.randomUUID().toString(); HttpMethod method = HttpMethod.valueOf(UUID.randomUUID().toString()); RequestBuilderWrapper rbwMock = mock(RequestBuilderWrapper.class); doReturn(rbwMock).when(helperSpy) .getRequestBuilder(anyString(), any(HttpMethod.class), any(Optional.class), anyBoolean()); // when RequestBuilderWrapper rbw = helperSpy.getRequestBuilder(url, method); // then verify(helperSpy).getRequestBuilder(url, method, Optional.empty(), false); assertThat(rbw).isSameAs(rbwMock); }
private Pair<CompletableFuture<String>, AsyncResponseHandler<String>> executeGetAwsRegionAndExtractHandler() { CompletableFuture<String> cf = AwsUtil.getAwsRegion(asyncClientMock); verify(asyncClientMock).getRequestBuilder(AMAZON_METADATA_DOCUMENT_URL, HttpMethod.GET); ArgumentCaptor<AsyncResponseHandler> handlerArgCaptor = ArgumentCaptor.forClass(AsyncResponseHandler.class); verify(asyncClientMock).executeAsyncHttpRequest(eq(regionRequestBuilderWrapperMock), handlerArgCaptor.capture()); AsyncResponseHandler<String> handler = handlerArgCaptor.getValue(); return Pair.of(cf, handler); }
private Pair<CompletableFuture<String>, AsyncResponseHandler<String>> executeGetAwsInstanceIdAndExtractHandler() { CompletableFuture<String> cf = AwsUtil.getAwsInstanceId(asyncClientMock); verify(asyncClientMock).getRequestBuilder(AMAZON_METADATA_INSTANCE_ID_URL, HttpMethod.GET); ArgumentCaptor<AsyncResponseHandler> handlerArgCaptor = ArgumentCaptor.forClass(AsyncResponseHandler.class); verify(asyncClientMock).executeAsyncHttpRequest(eq(awsInstanceIdRequestBuilderWrapperMock), handlerArgCaptor.capture()); AsyncResponseHandler<String> handler = handlerArgCaptor.getValue(); return Pair.of(cf, handler); }
.getRequestBuilder(AMAZON_METADATA_DOCUMENT_URL, HttpMethod.GET); doAnswer(invocation -> { AsyncResponseHandler<String> handler = (AsyncResponseHandler<String>) invocation.getArguments()[1]; .getRequestBuilder(AMAZON_METADATA_INSTANCE_ID_URL, HttpMethod.GET); doAnswer(invocation -> { AsyncResponseHandler<String> handler = (AsyncResponseHandler<String>) invocation.getArguments()[1];
RequestBuilderWrapper rbw = helperSpy.getRequestBuilder(url, method, cbOpt, disableCb);
RequestBuilderWrapper rbw = asyncClient.getRequestBuilder(fullUrl, HttpMethod.GET); rbw.requestBuilder.setHeader(TestEndpoint.EXPECTED_HEADER_KEY, TestEndpoint.EXPECTED_HEADER_VAL); rbw.requestBuilder.setBody(TestEndpoint.EXPECTED_REQUEST_PAYLOAD);
RequestBuilderWrapper rbw = asyncClient.getRequestBuilder(fullUrl, HttpMethod.GET); rbw.requestBuilder.setHeader(TestEndpoint.EXPECTED_HEADER_KEY, TestEndpoint.EXPECTED_HEADER_VAL); rbw.requestBuilder.setBody(TestEndpoint.EXPECTED_REQUEST_PAYLOAD);