Search in sources :

Example 1 with ApiCallException

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

the class Comments method checkCommentsEndpointStatus.

public static void checkCommentsEndpointStatus() throws IOException, JSONException, ApiCallException {
    Request request = new Request.Builder().url(STATUS_ENDPOINT).build();
    OkHttpClient client = new OkHttpClient.Builder().writeTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).build();
    Response response = client.newCall(request).execute();
    JSONObject status = new JSONObject(Objects.requireNonNull(response.body()).string());
    String statusText = Helper.getJSONString("text", null, status);
    boolean isRunning = Helper.getJSONBoolean("is_running", false, status);
    if (!"ok".equalsIgnoreCase(statusText) || !isRunning) {
        throw new ApiCallException("The comment server is not available at this time. Please try again later.");
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) ApiCallException(com.odysee.app.exceptions.ApiCallException) Request(okhttp3.Request)

Example 2 with ApiCallException

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

the class Lbry method directApiCall.

/**
 * @deprecated Use authenticatedGenericApiCall(String, Map, String) instead
 * @param method
 * @param p
 * @param authToken
 * @return
 * @throws ApiCallException
 */
@Deprecated
public static Object directApiCall(String method, Map<String, Object> p, String authToken) throws ApiCallException {
    p.put("auth_token", authToken);
    Object response = null;
    try {
        response = parseResponse(apiCall(method, p, API_CONNECTION_STRING));
    } catch (LbryRequestException | LbryResponseException ex) {
        throw new ApiCallException(String.format("Could not execute %s call: %s", method, ex.getMessage()), ex);
    }
    return response;
}
Also used : ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONObject(org.json.JSONObject) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException)

Example 3 with ApiCallException

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

the class Lbry method fileList.

public static List<LbryFile> fileList(String claimId, boolean downloads, int page, int pageSize) throws ApiCallException {
    List<LbryFile> files = new ArrayList<>();
    Map<String, Object> params = new HashMap<>();
    if (!Helper.isNullOrEmpty(claimId)) {
        params.put("claim_id", claimId);
    }
    if (downloads) {
        params.put("download_path", null);
        params.put("comparison", "ne");
    }
    if (page > 0) {
        params.put("page", page);
    }
    if (pageSize > 0) {
        params.put("page_size", pageSize);
    }
    try {
        JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_FILE_LIST, params));
        JSONArray items = result.getJSONArray("items");
        for (int i = 0; i < items.length(); i++) {
            JSONObject fileObject = items.getJSONObject(i);
            LbryFile file = LbryFile.fromJSONObject(fileObject);
            files.add(file);
            String fileClaimId = file.getClaimId();
            if (!Helper.isNullOrEmpty(fileClaimId)) {
                ClaimCacheKey key = new ClaimCacheKey();
                key.setClaimId(fileClaimId);
                if (claimCache.containsKey(key)) {
                    claimCache.get(key).setFile(file);
                }
            }
        }
    } catch (LbryRequestException | LbryResponseException | JSONException ex) {
        throw new ApiCallException("Could not execute resolve call", ex);
    }
    return files;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) ClaimCacheKey(com.odysee.app.model.ClaimCacheKey) JSONObject(org.json.JSONObject) LbryFile(com.odysee.app.model.LbryFile) JSONObject(org.json.JSONObject)

Example 4 with ApiCallException

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

the class MainActivity method unlockTips.

public void unlockTips() {
    if (unlockingTips) {
        return;
    }
    Map<String, Object> options = new HashMap<>();
    options.put("type", "support");
    options.put("is_not_my_input", true);
    options.put("blocking", true);
    AccountManager am = AccountManager.get(getApplicationContext());
    String authToken = am.peekAuthToken(am.getAccounts()[0], "auth_token_type");
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        unlockingTips = true;
        Supplier<Boolean> task = new UnlockingTipsSupplier(options, authToken);
        CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(task);
        completableFuture.thenAccept(result -> {
            unlockingTips = false;
        });
    } else {
        Thread unlockingThread = new Thread(new Runnable() {

            @Override
            public void run() {
                Callable<Boolean> callable = () -> {
                    try {
                        Lbry.directApiCall(Lbry.METHOD_TXO_SPEND, options, authToken);
                        return true;
                    } catch (ApiCallException | ClassCastException ex) {
                        ex.printStackTrace();
                        return false;
                    }
                };
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                Future<Boolean> future = executorService.submit(callable);
                try {
                    unlockingTips = true;
                    // This doesn't block main thread as it is called from a different thread
                    future.get();
                    unlockingTips = false;
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
        unlockingThread.start();
    }
}
Also used : HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) SpannableString(android.text.SpannableString) UnlockingTipsSupplier(com.odysee.app.supplier.UnlockingTipsSupplier) Callable(java.util.concurrent.Callable) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture) JSONObject(org.json.JSONObject) AccountManager(android.accounts.AccountManager)

Example 5 with ApiCallException

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

the class CommentCreateTask method doInBackground.

public Comment doInBackground(Void... params) {
    Comment createdComment = null;
    ResponseBody responseBody = null;
    try {
        // check comments status endpoint
        Comments.checkCommentsEndpointStatus();
        JSONObject comment_body = new JSONObject();
        comment_body.put("comment", comment.getText());
        comment_body.put("claim_id", comment.getClaimId());
        if (!Helper.isNullOrEmpty(comment.getParentId())) {
            comment_body.put("parent_id", comment.getParentId());
        }
        comment_body.put("channel_id", comment.getChannelId());
        comment_body.put("channel_name", comment.getChannelName());
        if (authToken != null) {
            comment_body.put("auth_token", authToken);
        }
        JSONObject jsonChannelSign = Comments.channelSignWithCommentData(comment_body, comment, comment.getText());
        if (jsonChannelSign.has("signature") && jsonChannelSign.has("signing_ts")) {
            comment_body.put("signature", jsonChannelSign.getString("signature"));
            comment_body.put("signing_ts", jsonChannelSign.getString("signing_ts"));
        }
        Response resp = Comments.performRequest(comment_body, "comment.Create");
        responseBody = resp.body();
        if (responseBody != null) {
            String responseString = responseBody.string();
            resp.close();
            JSONObject jsonResponse = new JSONObject(responseString);
            if (jsonResponse.has("result"))
                createdComment = Comment.fromJSONObject(jsonResponse.getJSONObject("result"));
        }
    } catch (ApiCallException | ClassCastException | IOException | JSONException ex) {
        error = ex;
    } finally {
        if (responseBody != null) {
            responseBody.close();
        }
    }
    return createdComment;
}
Also used : Response(okhttp3.Response) Comment(com.odysee.app.model.Comment) JSONObject(org.json.JSONObject) ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONException(org.json.JSONException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Aggregations

ApiCallException (com.odysee.app.exceptions.ApiCallException)31 JSONObject (org.json.JSONObject)28 HashMap (java.util.HashMap)22 JSONException (org.json.JSONException)17 Claim (com.odysee.app.model.Claim)10 JSONArray (org.json.JSONArray)10 ArrayList (java.util.ArrayList)8 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)7 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)7 MainActivity (com.odysee.app.MainActivity)6 DecimalFormat (java.text.DecimalFormat)5 DecimalFormatSymbols (java.text.DecimalFormatSymbols)5 LinkedHashMap (java.util.LinkedHashMap)5 ExecutorService (java.util.concurrent.ExecutorService)5 AccountManager (android.accounts.AccountManager)4 Response (okhttp3.Response)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 View (android.view.View)3 TextView (android.widget.TextView)3 Subscription (com.odysee.app.model.lbryinc.Subscription)3