@Test public void getPairInfo_success() throws Exception { when(mockGuestApi.call(anyString())) .thenReturn(JSON_PARSER.parse("{\"btc_usd\":{\"high\":2381.507,\"low\":2300," + "\"avg\":2340.7535,\"vol\":10359347.87922,\"vol_cur\":4423.75448," + "\"last\":2374,\"buy\":2374,\"sell\":2373.551,\"updated\":1496436981}," + "\"ltc_usd\":{\"high\":27.612,\"low\":25.60265,\"avg\":26.607325," + "\"vol\":4404490.34434,\"vol_cur\":164911.27789,\"last\":26.768," + "\"buy\":26.769,\"sell\":26.65201,\"updated\":1496436981}}") .getAsJsonObject()); CallResult<List<Ticker>> callResult = testable.getPairInfo(new HashSet<>(Arrays.asList("BTC/USD", "LTC/USD"))); assertTrue(callResult.isSuccess()); assertNull(callResult.getError()); List<Ticker> tickers = callResult.getPayload(); assertNotNull(tickers); assertEquals(2, tickers.size()); Ticker ticker = findByKey(tickers, new Predicate<Ticker>() { @Override public boolean check(Ticker element) { return "LTC/USD".equals(element.getPair()); } }); assertNotNull(ticker); assertEquals(new BigDecimal("27.612"), ticker.getHigh()); assertEquals(new BigDecimal("25.60265"), ticker.getLow()); assertEquals(new BigDecimal("26.607325"), ticker.getAvg()); assertEquals(new BigDecimal("4404490.34434"), ticker.getVol()); assertEquals(new BigDecimal("164911.27789"), ticker.getVolCur()); assertEquals(new BigDecimal("26.768"), ticker.getLast()); assertEquals(new BigDecimal("26.769"), ticker.getBuy()); assertEquals(new BigDecimal("26.65201"), ticker.getSell()); assertEquals(1496436981L, ticker.getUpdated()); }
@WorkerThread @NonNull public CallResult<List<Ticker>> getPairInfo(@NonNull Set<String> pairs) { String url = hostUrl + "/api/3/ticker/"; for (String pair : pairs) { url += PairUtils.localToServer(pair) + "-"; } JsonObject response = guestApi.call(url.substring(0, url.length() - 1)); CallResult<List<Ticker>> result = new CallResult<>(); if (response == null || response.has(SUCCESS_KEY)) { result.isSuccess = false; result.error = response == null ? generalErrorText : response.get(ERROR_KEY).getAsString(); return result; } result.isSuccess = true; List<Ticker> tickers = new ArrayList<>(); for (String pair : response.keySet()) { tickers.add(Ticker.createFromServer(pair, response.getAsJsonObject(pair))); } result.payload = tickers; return result; }
Api { @WorkerThread @NonNull public CallResult<List<Ticker>> getPairInfo(@NonNull Set<String> pairs) { String url = hostUrl + "/api/3/ticker/"; for (String pair : pairs) { url += PairUtils.localToServer(pair) + "-"; } JsonObject response = guestApi.call(url.substring(0, url.length() - 1)); CallResult<List<Ticker>> result = new CallResult<>(); if (response == null || response.has(SUCCESS_KEY)) { result.isSuccess = false; result.error = response == null ? generalErrorText : response.get(ERROR_KEY).getAsString(); return result; } result.isSuccess = true; List<Ticker> tickers = new ArrayList<>(); for (String pair : response.keySet()) { tickers.add(Ticker.createFromServer(pair, response.getAsJsonObject(pair))); } result.payload = tickers; return result; } }
Api { @WorkerThread @NonNull public CallResult<List<Ticker>> getPairInfo(@NonNull Set<String> pairs) { String url = hostUrl + "/api/3/ticker/"; for (String pair : pairs) { url += PairUtils.localToServer(pair) + "-"; } JsonObject response = guestApi.call(url.substring(0, url.length() - 1)); CallResult<List<Ticker>> result = new CallResult<>(); if (response == null || response.has(SUCCESS_KEY)) { result.isSuccess = false; result.error = response == null ? generalErrorText : response.get(ERROR_KEY).getAsString(); return result; } result.isSuccess = true; List<Ticker> tickers = new ArrayList<>(); for (String pair : response.keySet()) { tickers.add(Ticker.createFromServer(pair, response.getAsJsonObject(pair))); } result.payload = tickers; return result; } Api(@NonNull Context appContext, @NonNull String hostUrl,
               @Nullable String apiKey, @Nullable String apiSecret); @VisibleForTesting  Api(@NonNull Context appContext, @NonNull String hostUrl,
        @NonNull GuestApi guestApi, @NonNull AuthApi authApi); }
Api { @WorkerThread @NonNull public CallResult<List<Ticker>> getPairInfo(@NonNull Set<String> pairs) { String url = hostUrl + "/api/3/ticker/"; for (String pair : pairs) { url += PairUtils.localToServer(pair) + "-"; } JsonObject response = guestApi.call(url.substring(0, url.length() - 1)); CallResult<List<Ticker>> result = new CallResult<>(); if (response == null || response.has(SUCCESS_KEY)) { result.isSuccess = false; result.error = response == null ? generalErrorText : response.get(ERROR_KEY).getAsString(); return result; } result.isSuccess = true; List<Ticker> tickers = new ArrayList<>(); for (String pair : response.keySet()) { tickers.add(Ticker.createFromServer(pair, response.getAsJsonObject(pair))); } result.payload = tickers; return result; } Api(@NonNull Context appContext, @NonNull String hostUrl,
               @Nullable String apiKey, @Nullable String apiSecret); @VisibleForTesting  Api(@NonNull Context appContext, @NonNull String hostUrl,
        @NonNull GuestApi guestApi, @NonNull AuthApi authApi); void setCredentials(@NonNull String apiKey, @NonNull String apiSecret); void setHostUrl(@NonNull String hostUrl); @WorkerThread @NonNull CallResult<ExchangeInfo> getExchangeInfo(); @WorkerThread @NonNull CallResult<List<Ticker>> getPairInfo(@NonNull Set<String> pairs); @NonNull @WorkerThread CallResult<Depth> depth(@NonNull String pair); @NonNull @WorkerThread CallResult<AccountInfo> getAccountInfo(); @NonNull @WorkerThread CallResult<List<Transaction>> getTransactionsHistory(
            @NonNull Map<String, String> parameters); @NonNull @WorkerThread CallResult<List<TradeHistoryEntry>> getTradeHistory(
            @NonNull Map<String, String> parameters); @NonNull @WorkerThread CallResult<List<ActiveOrder>> getActiveOrders(); @NonNull @WorkerThread CallResult<TradeResponse> trade(@NonNull String pair, @TradeType @NonNull String type,
                                           @NonNull String rate, @NonNull String amount); @NonNull @WorkerThread CallResult<CancelOrderResponse> cancelOrder(long orderId); @NonNull @WorkerThread CallResult<WithdrawResponse> withdraw(@NonNull String coinName,
                                                 double amount, @NonNull String address); }
Api { @WorkerThread @NonNull public CallResult<List<Ticker>> getPairInfo(@NonNull Set<String> pairs) { String url = hostUrl + "/api/3/ticker/"; for (String pair : pairs) { url += PairUtils.localToServer(pair) + "-"; } JsonObject response = guestApi.call(url.substring(0, url.length() - 1)); CallResult<List<Ticker>> result = new CallResult<>(); if (response == null || response.has(SUCCESS_KEY)) { result.isSuccess = false; result.error = response == null ? generalErrorText : response.get(ERROR_KEY).getAsString(); return result; } result.isSuccess = true; List<Ticker> tickers = new ArrayList<>(); for (String pair : response.keySet()) { tickers.add(Ticker.createFromServer(pair, response.getAsJsonObject(pair))); } result.payload = tickers; return result; } Api(@NonNull Context appContext, @NonNull String hostUrl,
               @Nullable String apiKey, @Nullable String apiSecret); @VisibleForTesting  Api(@NonNull Context appContext, @NonNull String hostUrl,
        @NonNull GuestApi guestApi, @NonNull AuthApi authApi); void setCredentials(@NonNull String apiKey, @NonNull String apiSecret); void setHostUrl(@NonNull String hostUrl); @WorkerThread @NonNull CallResult<ExchangeInfo> getExchangeInfo(); @WorkerThread @NonNull CallResult<List<Ticker>> getPairInfo(@NonNull Set<String> pairs); @NonNull @WorkerThread CallResult<Depth> depth(@NonNull String pair); @NonNull @WorkerThread CallResult<AccountInfo> getAccountInfo(); @NonNull @WorkerThread CallResult<List<Transaction>> getTransactionsHistory(
            @NonNull Map<String, String> parameters); @NonNull @WorkerThread CallResult<List<TradeHistoryEntry>> getTradeHistory(
            @NonNull Map<String, String> parameters); @NonNull @WorkerThread CallResult<List<ActiveOrder>> getActiveOrders(); @NonNull @WorkerThread CallResult<TradeResponse> trade(@NonNull String pair, @TradeType @NonNull String type,
                                           @NonNull String rate, @NonNull String amount); @NonNull @WorkerThread CallResult<CancelOrderResponse> cancelOrder(long orderId); @NonNull @WorkerThread CallResult<WithdrawResponse> withdraw(@NonNull String coinName,
                                                 double amount, @NonNull String address);  }