Skip to content

A fork of the spring ClientHttpRequestFactory api and implementations without dependency on the remainder of the spring framework

License

Notifications You must be signed in to change notification settings

zalando-nakadi/fahrschein-http

Repository files navigation

Fahrschein HTTP API and Implementation

Build Status Release Maven Central

A fork of the spring ClientHttpRequestFactory api and implementations without dependency on the remainder of the spring framework. The main usecase is for the fahrschein nakadi client.

Changes compared to the spring implementation

  • The ClientHttpResponse#close methods do not try to consume remaining data from the stream, instead the connection is aborted (see SPR-14040 and SPR-14882).

Usage

Sending a request body serialized with Jackson and deserializing the response

    final URI uri = URI.create(...);
    final ObjectMapper objectMapper = new ObjectMapper();

    // RequestData and ResponseData can be arbitrary java objects which can be serialized and deserialized by jackson
    final RequestData requestData = ...

    final SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    requestFactoryDelegate.setConnectTimeout(500);
    requestFactoryDelegate.setReadTimeout(60 * 1000);

    final ClientHttpRequest request = clientHttpRequestFactory.createRequest(uri, HttpMethod.POST);

    request.getHeaders().setContentType(MediaType.APPLICATION_JSON);

    try (final OutputStream os = request.getBody()) {
        objectMapper.writeValue(os, requestData);
    }

    try (final ClientHttpResponse response = request.execute()) {
        try (final InputStream is = response.getBody()) {
            final ResponseData responseData = objectMapper.readValue(is, ResponseData.class);
            ...
        }
    }

Using the Apache HttpComponents implementation

final RequestConfig config = RequestConfig.custom().setSocketTimeout(readTimeout)
                                                   .setConnectTimeout(connectTimeout)
                                                   .setConnectionRequestTimeout(requestTimeout)
                                                   .build();

final CloseableHttpClient httpClient = HttpClients.custom()
                                                  .setConnectionTimeToLive(readTimeout, TimeUnit.MILLISECONDS)
                                                  .disableAutomaticRetries()
                                                  .setDefaultRequestConfig(config)
                                                  .disableRedirectHandling()
                                                  .setMaxConnTotal(8)
                                                  .setMaxConnPerRoute(2)
                                                  .build();

final ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

Getting help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's issue tracker.

Getting involved

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change.

About

A fork of the spring ClientHttpRequestFactory api and implementations without dependency on the remainder of the spring framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published