Skip to content

orbit-legacy/orbit-rest-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Orbit Async REST Client

Release Maven Central Javadocs Build Status Gitter

Allows calling JAX-RS REST interfaces with methods returning CompletableFuture or Orbit Task. This enables writing REST client code in a fluent asynchronous way.

The project orbit-rest-client depends only on the JAX-RS standard and it is in principle compatible with any client library that provides javax.ws.rs.client.WebTarget. It has been tested with jersey-client.

Developer & License

This project was developed by Electronic Arts and is licensed under the BSD 3-Clause License.

Examples

Using Orbit Tasks

public interface Hello
{
    @GET
    @Path("/")
    Task<String> getHome();
}

public static void main(String args[])
{
    WebTarget webTarget = getWebTarget("http://example.com");

    Hello hello = new RestClient(webTarget).get(Hello.class);

    Task<String> response = hello.getHome();

    response.thenAccept(x -> System.out.println(x)); 
    response.join();
}

// use any jax-rs client library to get javax.ws.rs.client.WebTarget
private static WebTarget getWebTarget(String host)
{
    ClientConfig clientConfig = new ClientConfig();
    Client client = ClientBuilder.newClient(clientConfig);
    return client.target(host);
}

Using CompletableFuture

public interface Hello
{
    @GET
    @Path("/")
    CompletableFuture<String> getHome();
}

public static void main(String args[])
{
    WebTarget webTarget = getWebTarget("http://example.com");

    Hello hello = new RestClient(webTarget).get(Hello.class);

    CompletableFuture<String> response = hello.getHome();
    
    response.thenAccept(x -> System.out.println(x)); 
    response.join();
}

// use any jax-rs client library to get javax.ws.rs.client.WebTarget
private static WebTarget getWebTarget(String host)
{
    ClientConfig clientConfig = new ClientConfig();
    Client client = ClientBuilder.newClient(clientConfig);
    return client.target(host);
}