java-stellar-sdk icon indicating copy to clipboard operation
java-stellar-sdk copied to clipboard

Unify and empower streaming api

Open msfeldstein opened this issue 6 years ago • 3 comments

Currently the streaming api has pretty bad ergonomics. I was trying to stream payment operations and it looks like server.payments().stream returns an OperationResponse which doesn't have any information available to it. I can use server.transactions().stream which gives me a TransactionResponse and that lets me use the envelope xdr to get all the information about the actual operations in one go. OperationResponse makes me do a secondary request to get any info.

Also, TransactionResponse (and OperationResponse when it gets the ability) should have a way to get the Transaction object without parsing the XDR manually.

msfeldstein avatar Aug 22 '19 01:08 msfeldstein

Maybe I misunderstood what you mean, but we can get useful information from server.payments().stream.

import org.stellar.sdk.Server;
import org.stellar.sdk.requests.EventListener;
import org.stellar.sdk.requests.PaymentsRequestBuilder;
import org.stellar.sdk.responses.operations.CreateAccountOperationResponse;
import org.stellar.sdk.responses.operations.OperationResponse;
import org.stellar.sdk.responses.operations.PaymentOperationResponse;
import shadow.com.google.common.base.Optional;

public class StellarDemo {
    public static void main(String[] args) {
        Server server = new Server("https://horizon.stellar.org");
        PaymentsRequestBuilder paymentsRequest = server.payments().cursor("now");
        paymentsRequest.stream(new EventListener<>() {
            @Override
            public void onEvent(OperationResponse operationResponse) {
                if (operationResponse instanceof PaymentOperationResponse) {
                    PaymentOperationResponse paymentOperationResponse = (PaymentOperationResponse) operationResponse;
                    System.out.println("Send from: " + paymentOperationResponse.getFrom());
                    // Get other information
                }

                if (operationResponse instanceof CreateAccountOperationResponse) {
                    CreateAccountOperationResponse createAccountOperationResponse = (CreateAccountOperationResponse) operationResponse;
                    System.out.println("Funder: " + createAccountOperationResponse.getFunder());
                    // Get other information
                }
                // Path Payment and Account Merge
            }

            @Override
            public void onFailure(Optional<Throwable> optional, Optional<Integer> optional1) {

            }
        });
    }
}

overcat avatar Sep 25 '19 06:09 overcat

How do i get the memo?

msfeldstein avatar Sep 25 '19 14:09 msfeldstein

Memo does not appear in Horizon's response, and we may need another HTTP request if we need it.

Michael Feldstein [email protected] 于2019年9月25日周三 下午10:21写道:

How do i get the memo?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/stellar/java-stellar-sdk/issues/237?email_source=notifications&email_token=AASCHEZMHCAMBMW4BD2RZQ3QLNXYNA5CNFSM4IOP4BNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7SCXQY#issuecomment-535047107, or mute the thread https://github.com/notifications/unsubscribe-auth/AASCHEYJJY7J3DHS5RFLBW3QLNXYNANCNFSM4IOP4BNA .

overcat avatar Sep 25 '19 14:09 overcat