Making currency rate requests in Java with CompletableFuture here is my code to get quotes for forex pairs, using the CurrencyLayer API, with CompletableFuture. We do an HTTP GET, parse the JSON response and map it into my data structures (see sample output at the bottom) 

Does this look OK? 
I'm worried about thread-safety !! :x

Currency.java:

public enum Currency {
    GBP("GBP", "Pound Sterling"),   
    EUR("EUR", "Euros"),
    USD("USD", "US Dollars");

    public final String symbol;
    public final String name;
    Currency(String symbol, String name) {
        this.symbol = symbol;
        this.name = name;
    }
}


CurrencyPair.java:

public class CurrencyPair {
    final public Currency base;
    final public Currency counter;

    public CurrencyPair(Currency base, Currency counter) {
        this.base = base;
        this.counter = counter;
    }
}


CurrencyLayer.java:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import models.Currency;

public class CurrencyLayer implements ForexQueryService {
    private final Client client = ClientBuilder.newClient();

    private CompletableFuture&lt;Response&gt; makeRequest(List&lt;CurrencyPair&gt; pairs) {
        final String url = "http://www.apilayer.net/api/live?access_key=[my key ^^]&amp;format=1";
        return CompletableFuture.supplyAsync(() -&gt; client.target(url).request().get());
    }

    private static double getUSDto(JsonObject quotes, Currency c) {
        if (c == Currency.USD) {
            return 1.0;
        } else {
            return quotes.get("USD" + c.symbol.replace("XBT", "BTC")).getAsDouble();
        }
    }

    private static List&lt;ForexRate&gt; convertResponse(Response r, List&lt;CurrencyPair&gt; pairs) {
        final String body = r.readEntity(String.class);

        // log this data
        System.out.println(body);

        JsonElement jelement = new JsonParser().parse(body);
        JsonObject jo = jelement.getAsJsonObject();
        JsonObject quotes = jo.get("quotes").getAsJsonObject();

        List&lt;ForexRate&gt; ret = new ArrayList&lt;&gt;();
        for (CurrencyPair p : pairs) {
            ret.add(new ForexRate(p.base, p.counter, getUSDto(quotes,p.counter)/getUSDto(quotes, p.base)));
        }

        return ret;
    }

    public CompletableFuture&lt;List&lt;ForexRate&gt;&gt; lookUpRates(List&lt;CurrencyPair&gt; pairs) {
        return makeRequest(pairs).thenApply((t) -&gt; convertResponse(t, pairs));
    }
}


ForexRate.java

public class ForexRate {
    public final Currency from;
    public final Currency to;
    public final Double rate;

    public ForexRate(Currency from, Currency to, Double rate) {
        this.from = from;
        this.to = to;
        this.rate = rate;
    }
}


Main.java:

public class Main {
    static public void main(String[] args) {
        CurrencyLayer c = new CurrencyLayer();
        try {
            List&lt;CurrencyPair&gt; pairs = new ArrayList&lt;&gt;();
            pairs.add(new CurrencyPair(Currency.GBP,Currency.USD));
            pairs.add(new CurrencyPair(Currency.JPY,Currency.GBP));

            List&lt;ForexRate&gt; rates = c.lookUpRates(pairs).get();
            for (ForexRate rate : rates) {
                System.out.println(rate.from + " " + rate.to + " -&gt; " + rate.rate);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Sample output:

{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "timestamp":1505384954,
  "source":"USD",
  "quotes":{
    "USDAUD":1.249706,
    "USDEUR":0.840197,
    "USDGBP":0.75723
  }
}
GBP USD -&gt; 1.320602723082815
EUR GBP -&gt; 0.9012529204460382
Thread-safety


  I'm worried about thread-safety !! :x


I don't see any shared data anywhere,
and as such there are no visible thread-safety issues.

If you called CurrencyLayer.lookUpRates from multiple threads,
then CurrencyLayer.client would be shared and so it would need to be thread-safe.

The current program as it is,
fetches and processes exchange rates practically sequentially,
so there's no visible need for multi-threading,
and doesn't really benefit at all from CompletableFuture.
There is no visible reason for CurrencyLayer.lookUpRates to return CompletableFuture&lt;List&lt;ForexRate&gt;&gt;,
it could as well return a simple List&lt;ForexRate&gt;.

Unused method parameter


private CompletableFuture&lt;Response&gt; makeRequest(List&lt;CurrencyPair&gt; pairs) {
    final String url = "http://www.apilayer.net/api/live?access_key=[my key ^^]&amp;format=1";
    return CompletableFuture.supplyAsync(() -&gt; client.target(url).request().get());
}



The pairs parameter is not used in this method.

Exception handling in main

In this code,
it would be better to remove the try-catch.
The outcome will look the same in case of failure,
but the exit code will be correctly non-zero,
which is currently not the case.


static public void main(String[] args) {
    CurrencyLayer c = new CurrencyLayer();
    try {
        // ...
    } catch (Exception e) {
        e.printStackTrace();
    }
}