Flux

Fluent http/rest-client

License

License

GroupId

GroupId

de.galan
ArtifactId

ArtifactId

flux
Last Version

Last Version

0.7.5
Release Date

Release Date

Type

Type

jar
Description

Description

Flux
Fluent http/rest-client
Project URL

Project URL

https://github.com/galan/flux
Source Code Management

Source Code Management

https://github.com/galan/flux

Download flux

How to add to project

<!-- https://jarcasting.com/artifacts/de.galan/flux/ -->
<dependency>
    <groupId>de.galan</groupId>
    <artifactId>flux</artifactId>
    <version>0.7.5</version>
</dependency>
// https://jarcasting.com/artifacts/de.galan/flux/
implementation 'de.galan:flux:0.7.5'
// https://jarcasting.com/artifacts/de.galan/flux/
implementation ("de.galan:flux:0.7.5")
'de.galan:flux:jar:0.7.5'
<dependency org="de.galan" name="flux" rev="0.7.5">
  <artifact name="flux" type="jar" />
</dependency>
@Grapes(
@Grab(group='de.galan', module='flux', version='0.7.5')
)
libraryDependencies += "de.galan" % "flux" % "0.7.5"
[de.galan/flux "0.7.5"]

Dependencies

compile (4)

Group / Artifact Type Version
de.galan : commons jar 0.11.1
commons-codec : commons-codec jar 1.10
org.apache.logging.log4j : log4j-core Optional jar 2.5
org.apache.logging.log4j : log4j-api jar 2.5

test (3)

Group / Artifact Type Version
org.simpleframework : simple-http jar 6.0.1
junit : junit jar 4.12
org.assertj : assertj-core jar 3.2.0

Project Modules

There are no modules declared in this project.

Efficient lightweight client-library for easy fluent http/rest requests in Java.

💡 This project has been integrated into de.galan:commons. For now the project-page acts as landing-page, but will refer to the new location.

To migrate simply change the dependency to de.galan:commons and update the package from de.galan.flux to de.galan.commons.net.flux.

Features

  • HTTP methods GET, PUT, POST, DELETE, HEAD, TRACE and OPTIONS
  • Basic-Authentication support
  • Proxy support (with authentication)
  • Configurable retries
  • Configurable default timeouts and headers

Integration with Maven

Just add the following dependency (see releases for latest version):

<dependency>
	<groupId>de.galan</groupId>
	<artifactId>commons</artifactId>
	<version>x.y.z</version>
</dependency>

Example usage

Creating Requests

Simplest case

Response response = Flux.request("http://host/resource").get();
System.out.println(response.getStreamAsString()); // Work with response, eg. print body
response.close();

Using AutoCloseable

try (Response response = Flux.request("http://host/resource").get()) {
    System.out.println(response.getStreamAsString());
}

Methods

Calling the HTTP methods executes the http requests and returns a Response.

Flux.request("http://host/resource").get();

The following methods are available:

  • get()
  • put()
  • post()
  • delete()
  • head()
  • trace()
  • options()
  • method(Method method)

Additional each method is available as asynchronous call when postfixed with Async. Those methods will return a Future<Response>:

  • getAsync()
  • putAsync()
  • postAsync()
  • deleteAsync()
  • headAsync()
  • traceAsync()
  • optionsAsync()
  • methodAsync(Method method)

Query parameters

You can add parameters by calling the fluent parametermethods, adding multiple parameter with the same name is possible.

Flux.request("http://host/resource").parameter("key", "value").get();
Flux.request("http://host/resource").parameter("q", "search").paramter("page", "2").get();
Flux.request("http://host/resource").parameter("cities", "hamburg", "halle").get();

Additional methods for rarely used arguments are also available:

  • paramter(Map<String, String>)
  • parameterList(Map<String, List<String>>).

Body

You can pass a body by calling the body(..) method.

Flux.request("http://host/resource").body("Hello World").get();
Flux.request("http://host/resource").body(new byte[]{72, 101, 108, ...}).get();

HTTP Header

Beside the default header you can set the HTTP header for each request individualy. Methods available in the fluent builder:

  • header(String key, String value)
  • headers(Map<String, String> headers)

For example:

Flux.request("http://host/resource").header("X-Myapp", "Demo").header("User-Agent", "Flux").get();
// code above does the same as below
Map<String, String> header = new HashMap<>();
header.put("X-Myapp", "Demo");
header.put("User-Agent", "Flux");
Flux.request("http://host/resource").headers(header).get();

Proxy

Enable a proxy by passing the proxy to a proxy(..) method.

Flux.request("http://host/resource").proxy("1.2.3.4:8080").get();

If the proxy requires authentication use proxyAuthentication(String username, String password).

Flux.request("http://host/resource").proxy("1.2.3.4:8080").proxyAuthentication("user", "pass").get();

Timeout

Setting the default connection and read timeout at once (can be overriden using the builder)

Flux.setDefaultTimeout(2500L); // using 2,5 seconds default
Flux.setDefaultTimeout("2s500ms"); // using 2,5 seconds default
Flux.setDefaultTimeout("1m 5s"); // using 1 minutes and 5 seconds default

Setting the timeout using the builder

Flux.request("http://host/resource").timeout(2500L).get(); // using 2,5 seconds
Flux.request("http://host/resource").timeout("2s500ms").get(); // using 2,5 seconds
Flux.request("http://host/resource").timeout("1m 5s").get(); // using 1 minutes and 5 seconds

Setting the connection and read timeout independently

Flux.setDefaultTimeoutConnection("5s");
Flux.setDefaultTimeoutRead("1m");
Flux.request("http://host/resource").timeoutConnection("5s").get();
Flux.request("http://host/resource").timeoutRead("1m").get();

Retries

Failed requests can be retried automatically, enable by setting the amount of retries and the time between the retries. If the retries also fail, the last failed response will be returned.

// repeat 2 times with 10 seconds in between
Flux.request("http://host/resource").retries(2L, "10s").get(); 

Redirects

You can follow redirects by calling followRedirects(), this is enabled by default. To disable this feature call unfollowRedirects().

Response

The Response returned from the HttpClient contains status information as well as an open connection to retrieve the stream (therefor the required close()or AutoClosable - benefit: you don't have to retrieve the whole content if you do not require it).

Actions that can be performed on the Response object:

  • getStream() - Returns the raw body InputStream
  • getStreamAsBytearray()- Returns the raw body as Byte array (byte[])
  • getStreamAsString() - Returns the body as String (UTF-8 is assumed)
  • getStreamAsString(String encoding) - Returns the body as String (encoding is used to decode the String)
  • storeStream(File file) - Stores the raw body in the given file
  • storeStream(File file, boolean gunzip) - Stores the raw body in the given file with the option to gzip it
  • getContentEncoding() - Returns the "Content-Encoding" header
  • getContentType() - Returns the "Content-Type" header
  • getStatusCode() - Returns the http status code
  • isFailed() - Returns true if the status code is not succeded
  • isSucceded() - Returns true if the status code is between 200 and 299
  • isInformational() - Returns true if the status code is between 100 and 199
  • isRedirection() - Returns true if the status code is between 300 and 399
  • isClientError() - Returns true if the status code is between 400 and 499
  • isServerError() - Returns true if the status code is between 500 and 599
  • getHeaderField(String name) - Returns the value for a response header
  • getHeaderFields() - Returns all response headers as Map<String, String>
  • close() - Closes the response (stream); note that Response is Autoclosable

Notes & Hints

  • You don't have to use the static Flux factory class, if you want to Inject the builder using a DI framework such as Guice, you can simply bind an instance of FluentHttpClient.
  • Flux is thread-safe
  • For testing it is useful to know that you can utilize the MockCommonHttpClient. You can pass the MockCommonHttpClient with canned Responses. Check the MockTest class for an example of the usage.

Versions

Version
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0