Search in sources :

Example 16 with LbryioRequestException

use of com.odysee.app.exceptions.LbryioRequestException in project odysee-android by OdyseeTeam.

the class SyncGetTask method doInBackground.

protected WalletSync doInBackground(Void... params) {
    try {
        password = Helper.isNullOrEmpty(password) ? "" : password;
        JSONObject result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_WALLET_STATUS, Lbryio.AUTH_TOKEN);
        boolean isLocked = Helper.getJSONBoolean("is_locked", false, result);
        boolean unlockSuccessful = !isLocked || (boolean) Lbry.authenticatedGenericApiCall(Lbry.METHOD_WALLET_UNLOCK, Lbry.buildSingleParam("password", password), Lbryio.AUTH_TOKEN);
        if (!unlockSuccessful) {
            throw new WalletException("The wallet could not be unlocked with the provided password.");
        }
        String hash = (String) Lbry.authenticatedGenericApiCall(Lbry.METHOD_SYNC_HASH, null, Lbryio.AUTH_TOKEN);
        try {
            JSONObject response = (JSONObject) Lbryio.parseResponse(Lbryio.call("sync", "get", Lbryio.buildSingleParam("hash", hash), Helper.METHOD_POST, null));
            WalletSync walletSync = new WalletSync(Helper.getJSONString("hash", null, response), Helper.getJSONString("data", null, response), Helper.getJSONBoolean("changed", false, response));
            if (applySyncChanges && (!hash.equalsIgnoreCase(walletSync.getHash()) || walletSync.isChanged())) {
                // Lbry.sync_apply({ password, data: response.data, blocking: true });
                try {
                    Map<String, Object> options = new HashMap<>();
                    options.put("password", Helper.isNullOrEmpty(password) ? "" : password);
                    options.put("data", walletSync.getData());
                    options.put("blocking", true);
                    JSONObject syncApplyResponse = (JSONObject) Lbry.authenticatedGenericApiCall(Lbry.METHOD_SYNC_APPLY, options, Lbryio.AUTH_TOKEN);
                    syncHash = Helper.getJSONString("hash", null, syncApplyResponse);
                    syncData = Helper.getJSONString("data", null, syncApplyResponse);
                    applySyncSuccessful = true;
                } catch (ApiCallException | ClassCastException ex) {
                    // sync_apply failed
                    syncApplyError = ex;
                }
            }
            if (Lbryio.isSignedIn() && !Lbryio.userHasSyncedWallet) {
                // indicate that the user owns a synced wallet (only if the user is signed in)
                Lbryio.userHasSyncedWallet = true;
            }
            return walletSync;
        } catch (LbryioResponseException ex) {
            // wallet sync data doesn't exist
            return null;
        }
    } catch (ApiCallException | WalletException | ClassCastException | LbryioRequestException ex) {
        error = ex;
        return null;
    }
}
Also used : WalletException(com.odysee.app.exceptions.WalletException) HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) WalletSync(com.odysee.app.model.WalletSync) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 17 with LbryioRequestException

use of com.odysee.app.exceptions.LbryioRequestException in project odysee-android by OdyseeTeam.

the class ClaimRewardTask method doInBackground.

public String doInBackground(Void... params) {
    String message = null;
    try {
        String txid = null;
        if (Reward.TYPE_FIRST_CHANNEL.equalsIgnoreCase(type)) {
            // fetch a channel
            txid = fetchSingleClaimTxid(Claim.TYPE_CHANNEL);
        } else if (Reward.TYPE_FIRST_PUBLISH.equalsIgnoreCase(type)) {
            // fetch a publish
            txid = fetchSingleClaimTxid(Claim.TYPE_STREAM);
        }
        // Get a new wallet address for the reward
        String address = (String) Lbry.genericApiCall(Lbry.METHOD_ADDRESS_UNUSED, authToken);
        Map<String, String> options = new HashMap<>();
        options.put("reward_type", type);
        options.put("wallet_address", address);
        if (!Helper.isNullOrEmpty(rewardCode)) {
            options.put("code", rewardCode);
        }
        if (!Helper.isNullOrEmpty(txid)) {
            options.put("transaction_id", txid);
        }
        if (authToken != null) {
            options.put("auth_token", authToken);
        }
        JSONObject reward = (JSONObject) Lbryio.parseResponse(Lbryio.call("reward", "claim", options, Helper.METHOD_POST, null));
        if (reward != null) {
            amountClaimed = Helper.getJSONDouble("reward_amount", 0, reward);
            message = Helper.getJSONString("reward_notification", "", reward);
        }
    } catch (ApiCallException | JSONException | LbryioRequestException | LbryioResponseException ex) {
        error = ex;
    }
    return message;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) JSONException(org.json.JSONException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Aggregations

LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)17 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)16 JSONObject (org.json.JSONObject)13 HashMap (java.util.HashMap)11 JSONException (org.json.JSONException)7 JSONArray (org.json.JSONArray)5 AccountManager (android.accounts.AccountManager)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Supplier (java.util.function.Supplier)4 Response (okhttp3.Response)4 Account (android.accounts.Account)3 Activity (android.app.Activity)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 MainActivity (com.odysee.app.MainActivity)3 LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)3 Reward (com.odysee.app.model.lbryinc.Reward)3 FetchRewardsSupplier (com.odysee.app.supplier.FetchRewardsSupplier)3 Context (android.content.Context)2 Color (android.graphics.Color)2