Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public class JsonPriceFeed

  1. Object
  2. JsonPriceFeed

ImplementsPriceFeed

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.

Constructors

public JsonPriceFeed(RpcTransport transport, String urlTemplate, String pricePath, String name)A feed.

Methods

public Quote getQuote(String base, String quote) throws IOExceptionThe price of base in quote, by ticker (BTC, ETH, USD, EUR); case does not matter.

Inherited methods

Constructor details

JsonPriceFeed

public JsonPriceFeed(RpcTransport transport, String urlTemplate, String pricePath, String name)
A feed.

Parameters

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

Method details

getQuote

public Quote getQuote(String base, String quote) throws IOException
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