Search code examples
javacallbackreturnbinance

How can i make my method return a callback?


Im trying to code for an trading bot using the java binance api. what i`d like to do is use to async client from the library to create a method to fetch data/candlesticks and then return it.

My problem is that the async client returns the response to a callback and i have now idea how to handle it, make my method return the data like the example below:

    public List<Candlestick> asyncGetCandles(){
    //get the data
        return response /**List<Candlestick>response*/
    }

This is what i got so far:

    public void asyncGetCandles() throws ParseException, IOException {

    BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance();
    BinanceApiAsyncRestClient asyncClient = factory.newAsyncRestClient();

    long start = dateTime.startOfListing();
    long now = System.currentTimeMillis();

    BinanceApiCallback<List<Candlestick>> callback = response -> {
        System.out.println(response);//prints 1000 candles
    };


    asyncClient.getCandlestickBars("BTCUSDT", CandlestickInterval.HOURLY, 1000, start, now, callback);

    /**How to return the response here?*/
}

Any help on how to do this would be really appreciated!


Solution

  • It should look something like this:

    package stackoverflow;
    
    import java.io.IOException;
    import java.text.ParseException;
    import java.util.List;
    
    public class ReturnAsyncCallback {
        interface BinanceApiCallback<T> { // either this usecase with interface
            BinanceApiCallback<T> run(String response);
        }
        abstract class BinanceApiCallback2<T> { // or this usecase with abstract base class
            abstract BinanceApiCallback<T> run(String response);
        }
        static class Candlestick {
    
        }
    
        public BinanceApiCallback<List<Candlestick>> asyncGetCandles() throws ParseException, IOException {
    
            final BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance();
            final BinanceApiAsyncRestClient asyncClient = factory.newAsyncRestClient();
    
            final long start = dateTime.startOfListing();
            final long now = System.currentTimeMillis();
    
            final BinanceApiCallback<List<Candlestick>> callback = (response) -> {
                System.out.println(response);//prints 1000 candles
            };
    
    
            asyncClient.getCandlestickBars("BTCUSDT", CandlestickInterval.HOURLY, 1000, start, now, callback);
    
            /**How to return the response here?*/
            return callback;
        }
    
    }
    

    Note, that there are TWO ways for lambda expressions. Both need a class with exactly ONE abstract (unimplemented) method:

    1. Use an interface with only 1 (abstract) method, can have as many default methods as it wants.
    2. Use an abstract base class with exactly ONE abstract method.

    Additional notes for my example:

    1. The inner classes BinanceApiCallback and BinanceApiCallback2 and Candlestick are only there for the http://sscce.org/

    Hints for further questions:

    1. Your code was NOT an http://sscce.org/, so it makes it a lot harder for us to answer.