Class JsonPriceFeed

java.lang.Object
com.codename1.crypto.market.JsonPriceFeed
All Implemented Interfaces:
PriceFeed

public class JsonPriceFeed extends Object implements PriceFeed

A feed over any HTTP API that answers one price in JSON. The URL is a template in which {base}/{quote} become the lower-case tickers and {BASE}/{QUOTE} the upper-case ones; the path names the price in the reply, member names and array indices separated by dots.

// Coinbase: {"data":{"amount":"67187.33","base":"BTC","currency":"USD"}}
RpcTransport transport = new NetworkTransport();
PriceFeed coinbase = new JsonPriceFeed(transport,
        "https://api.coinbase.com/v2/prices/{BASE}-{QUOTE}/spot", "data.amount", "coinbase");
// Kraken: {"result":{"XXBTZUSD":{"c":["67187.30000","0.01"]}}} -- the pair key varies, so
// pass a path per pair or use a feed that knows the exchange.

The price may be a JSON number or a numeric string.

  • Constructor Details

    • JsonPriceFeed

      public JsonPriceFeed(RpcTransport transport, String urlTemplate, String pricePath, String name)

      A feed.

      Parameters
      • transport: how to send the request
      • urlTemplate: the URL with {base}, {quote}, {BASE}, {QUOTE} placeholders
      • pricePath: where the price is in the reply, such as data.amount
      • name: the source name the quotes carry
  • Method Details

    • getQuote

      public Quote getQuote(String base, String quote) throws IOException
      Description copied from interface: PriceFeed

      The price of base in quote, by ticker (BTC, ETH, USD, EUR); case does not matter.

      Throws
      • IOException: when the source cannot be reached or does not know the pair
      Specified by:
      getQuote in interface PriceFeed
      Throws:
      IOException