Search in sources :

Example 11 with Response

use of org.apache.hc.client5.http.fluent.Response in project feign by OpenFeign.

the class AsyncApacheHttp5Client method execute.

@Override
public CompletableFuture<Response> execute(Request request, Options options, Optional<HttpClientContext> requestContext) {
    final SimpleHttpRequest httpUriRequest = toClassicHttpRequest(request, options);
    final CompletableFuture<Response> result = new CompletableFuture<>();
    final FutureCallback<SimpleHttpResponse> callback = new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(SimpleHttpResponse httpResponse) {
            result.complete(toFeignResponse(httpResponse, request));
        }

        @Override
        public void failed(Exception ex) {
            result.completeExceptionally(ex);
        }

        @Override
        public void cancelled() {
            result.cancel(false);
        }
    };
    client.execute(httpUriRequest, configureTimeouts(options, requestContext.orElseGet(HttpClientContext::new)), callback);
    return result;
}
Also used : SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 12 with Response

use of org.apache.hc.client5.http.fluent.Response in project pact-jvm by DiUS.

the class ConsumerClient method getAsMap.

public Map getAsMap(String path, String queryString) throws IOException {
    URIBuilder uriBuilder;
    try {
        uriBuilder = new URIBuilder(url).setPath(path);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    if (StringUtils.isNotEmpty(queryString)) {
        uriBuilder.setParameters(parseQueryString(queryString));
    }
    Content response = Request.get(uriBuilder.toString()).addHeader(TESTREQHEADER, TESTREQHEADERVALUE).execute().returnContent();
    return jsonToMap(response.getType() != null ? response.asString() : response.asString(Charset.defaultCharset()));
}
Also used : Content(org.apache.hc.client5.http.fluent.Content) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.hc.core5.net.URIBuilder)

Example 13 with Response

use of org.apache.hc.client5.http.fluent.Response in project pact-jvm by DiUS.

the class DefaultValuesTest method testWithDefaultValues.

@Test
@PactVerification
public void testWithDefaultValues() throws IOException {
    Response response = Request.get(provider.getUrl() + "/path").addHeader("Accept", APPLICATION_JSON).addHeader("Content-Type", APPLICATION_JSON).execute();
    assertThat(response.returnResponse().getCode(), is(200));
    response = Request.get(provider.getUrl() + "/path2?source_filename=test%20file").addHeader("Accept", APPLICATION_JSON).addHeader("Content-Type", APPLICATION_JSON).execute();
    assertThat(response.returnResponse().getCode(), is(200));
}
Also used : Response(org.apache.hc.client5.http.fluent.Response) Test(org.junit.Test)

Example 14 with Response

use of org.apache.hc.client5.http.fluent.Response in project bender by Nextdoor.

the class Http2Transport method sendBatch.

public void sendBatch(byte[] raw) throws TransportException {
    /*
     * Wrap the call with retry logic to avoid intermittent ES issues.
     */
    Callable<SimpleHttpResponse> callable = () -> {
        SimpleHttpResponse resp;
        String responseString = null;
        SimpleRequestBuilder rb = SimpleRequestBuilder.post().setHttpHost(this.target).setPath(this.url.getPath());
        /*
       * Do the call, read response, release connection so it is available for use again, and
       * finally check the response.
       */
        if (this.useGzip) {
            rb = rb.addHeader("Accept-Encoding", "gzip");
            resp = execute(rb, raw, ContentType.DEFAULT_BINARY);
        } else {
            resp = execute(rb, raw, getUncompressedContentType());
        }
        responseString = resp.getBodyText();
        if (responseString == null || responseString == "") {
            responseString = "Empty Reponse";
        }
        checkResponse(resp, responseString);
        return resp;
    };
    RetryConfig config = new RetryConfigBuilder().retryOnSpecificExceptions(TransportException.class).withMaxNumberOfTries(this.retries + 1).withDelayBetweenTries(this.retryDelayMs, ChronoUnit.MILLIS).withExponentialBackoff().build();
    try {
        new CallExecutor(config).execute(callable);
    } catch (RetriesExhaustedException ree) {
        logger.warn("transport failed after " + ree.getCallResults().getTotalTries() + " tries.");
        throw new TransportException(ree.getCallResults().getLastExceptionThatCausedRetry());
    } catch (UnexpectedException ue) {
        throw new TransportException(ue);
    }
}
Also used : RetryConfigBuilder(com.evanlennick.retry4j.RetryConfigBuilder) CallExecutor(com.evanlennick.retry4j.CallExecutor) UnexpectedException(com.evanlennick.retry4j.exception.UnexpectedException) RetryConfig(com.evanlennick.retry4j.RetryConfig) RetriesExhaustedException(com.evanlennick.retry4j.exception.RetriesExhaustedException) SimpleRequestBuilder(org.apache.hc.client5.http.async.methods.SimpleRequestBuilder) TransportException(com.nextdoor.bender.ipc.TransportException) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse)

Aggregations

IOException (java.io.IOException)8 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)5 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 Gson (com.google.gson.Gson)3 TypeToken (com.google.gson.reflect.TypeToken)3 feign (feign)3 ResponseMappingDecoder (feign.Feign.ResponseMappingDecoder)3 HttpMethod (feign.Request.HttpMethod)3 HardCodedTarget (feign.Target.HardCodedTarget)3 MockWebServerAssertions.assertThat (feign.assertj.MockWebServerAssertions.assertThat)3 feign.codec (feign.codec)3 BeanQueryMapEncoder (feign.querymap.BeanQueryMapEncoder)3 FieldQueryMapEncoder (feign.querymap.FieldQueryMapEncoder)3 Type (java.lang.reflect.Type)3 URI (java.net.URI)3 java.util (java.util)3 java.util.concurrent (java.util.concurrent)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3