Skip to content

Syncing a user between instances

alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Aug 15, 2018 · 3 revisions

Syncing a user's linked brokers across app (SDK) instances

On app startup

A user's linked brokers consistsof the userId/userToken pairs and the associated broker accounts. After initializing the SDK, you can call the TradeItLinkedBrokerManager.syncLocalLinkedBrokers() method to sync the externally saved linked brokers/accounts for a given user.

public class MainActivity extends AppCompatActivity {
    ...
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        TradeItConfigurationBuilder configurationBuilder = new TradeItConfigurationBuilder(
            this.getApplicationContext(),
            "tradeit-test-api-key",
            TradeItEnvironment.LOCAL
        );

        TradeItSDK.configure(configurationBuilder);
        
        // Get the linked brokers and accounts for this user and put them in a list to sync with the TradeItLinkedBrokerManager

        List<TradeItLinkedBrokerData> linkedBrokers = new ArrayList();
        
        // For each linked broker create a TradeItLinkedBrokerData object:
        TradeItLinkedBrokerData linkedBrokerData = new TradeItLinkedBrokerData(
            "fidelity", // the broker
            "this-is-the-userId",
            "this-is-the-userToken"
        );
        
        // Each linked broker has a list of accounts
        List<TradeItLinkedBrokerAccountData> linkedBrokerAccounts = new ArrayList();
        
        // For each account for that linked broker create a TradeItLinkedBrokerAccountData object
        TradeItLinkedBrokerAccountData accountData = new TradeItLinkedBrokerAccountData(
            "this-is-the-accountName",
            "this-is-the-accountNumber",
            "JPY" // the base currency of the account
        );
        
        // Add the account to the linked broker
        linkedBrokerData.injectAccount(accountData);
        
        // Sync the list of linked brokers to the linked broker manager
        TradeItSDK.getLinkedBrokerManager().syncLocalLinkedBrokers(linkedBrokers);
        ...
    }
    ...
}

When a new broker has been linked

A successfully completed OAuth flow should result in a deep link back into your app with a valid oAuthVerifier token. This token is submitted to the TradeIt API to complete linking, at which point you will have an instance of a newly created TradeItLinkedBrokerParcelable object. Use this object to persist the user's linked broker externally.

TradeItSDK.getLinkedBrokerManager().linkBrokerWithOauthVerifier(
    "Some Linked Login Label",
    oAuthVerifier,
    new TradeItCallback<TradeItLinkedBrokerParcelable>() {
        @Override
        public void onSuccess(TradeItLinkedBrokerParcelable linkedBroker) {
            // Persist the necessary data externally and associate it with your user
        
            // The broker's name, userId, and userToken
            TradeItLinkedLogin linkedLogin = linkedBroker.getLinkedLogin()
            linkedLogin.broker
            linkedLogin.userId
            linkedLogin.userToken
            
            // Each of the associated accounts' account name, account number, and base currency
            for (TradeItLinkedBrokerAccountParcelable account : linkedBroker.getAccounts()) {
                account.getAccountName()
                account.getAccountNumber()
                account.getAccountBaseCurrency()
            }
        }
    }
);

When an existing broker has been relinked

Similar to handling the deep link from linking. The userId will stay the same, but the userToken will change. Updated the userToken of the externally persisted linked broker associated with the userId.

When a broker has been unlinked

Remove the externally persisted data for the userId associated with the unlinked broker.