10000 GitHub - esuomi/http: HTTP client wrapper as for some reason all clients do a billion bells and whistles but fail to provide a single clean easily testable abstraction
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Mar 13, 2020. It is now read-only.
/ http Public archive

HTTP client wrapper as for some reason all clients do a billion bells and whistles but fail to provide a single clean easily testable abstraction

License

Notifications You must be signed in to change notification settings

esuomi/http

Repository files navigation

HTTP client wrapper

Whether it's Jersey, Apache HttpClient 3.x or 4, Ning or any of the n+1 HTTP clients already available for the JVM, they all share the following two traits:

  1. They are all very powerful and provide all the bells and whistles one could ever need for HTTP client connectivity
  2. Almost none of them provide a good baseline abstraction for both easy usage and mocking of said library

While an argument exists that HTTP client should be hidden behind a Gateway and used as is I have through experience found that being able to hotswap HTTP implementation itself has allowed me to reuse integration tests as system tests while still using the actual Gateway object. I have found this immensely valuable as this proves the Gateway object behaves correctly with the rest of the code base while keeping especially the tests snappy.

Basic use

The meat of this wrapper is the HttpClient interface which provides the commonly used HTTP 1.1 methods as standard Java methods. Going line by line below is the most idiomatic use for this wrapper:

// Instantiate any implementation. The instance is reusable.
HttpClient httpClient = ...;
// provide params and headers as multimaps
Multimap<String, String> params = HashMultimap.create();
Multimap<String, String> headers = HashMultimap.create();
// provide request body as stream
InputStream requestBody = new ByteInputStream();
// use try-with-resources to perform HTTP call
try (Response response = httpClient.get("http://www.example.com", params, headers, requestBody)) {
    // status code for the response
    int statusCode = response.getStatusCode();
    // headers returned by the server
    Multimap<String, String> responseHeaders = response.getResponseHeaders();
    // optional response body as stream
    Optional<InputStream> responseBody = response.getResponseBody();
}

As you can see, no magic is involved. Headers aren't automatically set, parameters are not automatically converted from various object types to Strings (you don't need to encode them though - implementations take care of that) and in general there isn't a ton of builders nor other classes involved in the request itself.

Modules

As not all projects can include all transitive dependencies of various libraries this client is divided into highly granular modules to allow you, the user, to pick and choose what you want to and can use. The modules are

  • http-core The minimal dependency core library for the use of HttpClient
  • http-ning Ning implementation of the HttpClient
  • http-builders Builders and fluent utilities to ease the use of HttpClient and its facilities
  • http-integration-tests contains test suite for all implementations.

About

HTTP client wrapper as for some reason all clients do a billion bells and whistles but fail to provide a single clean easily testable abstraction

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0