Search in sources :

Example 11 with LbryioResponseException

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

the class UserSignin method call.

@Override
public Boolean call() throws Exception {
    try {
        options.put("domain", "odysee.com");
        Response responseSignIn = Lbryio.call("user", "signin", options, Helper.METHOD_POST, ctx);
        if (responseSignIn.isSuccessful()) {
            ResponseBody responseBody = responseSignIn.body();
            if (responseBody != null) {
                String responseString = responseBody.string();
                responseSignIn.close();
                JSONObject responseJson = new JSONObject(responseString);
                if (responseJson.getBoolean("success")) {
                    JSONObject jsondata = responseJson.getJSONObject("data");
                    return jsondata.has("primary_email") && jsondata.getString("primary_email").equals(options.get("email"));
                }
            }
        }
        return false;
    } catch (LbryioRequestException | LbryioResponseException e) {
        Log.e(TAG, e.toString());
        return false;
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ResponseBody(okhttp3.ResponseBody)

Example 12 with LbryioResponseException

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

the class Lbryio method parseResponse.

public static Object parseResponse(Response response) throws LbryioResponseException {
    String responseString = null;
    try (ResponseBody responseBody = response.body()) {
        if (responseBody != null) {
            responseString = responseBody.string();
            if (!isValidJSON(responseString)) {
                return responseString;
            }
            JSONObject json = new JSONObject(responseString);
            if (response.code() >= 200 && response.code() < 300) {
                if (json.isNull("data")) {
                    return null;
                }
                return json.get("data");
            }
            if (json.has("error")) {
                if (json.isNull("error")) {
                    throw new LbryioResponseException("No error message", response.code());
                }
                throw new LbryioResponseException(json.getString("error"), response.code());
            } else {
                throw new LbryioResponseException("Unknown API error signature.", response.code());
            }
        } else {
            return null;
        }
    } catch (JSONException | IOException ex) {
        throw new LbryioResponseException(String.format("Could not parse response: %s", responseString), ex);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) IOException(java.io.IOException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ResponseBody(okhttp3.ResponseBody)

Example 13 with LbryioResponseException

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

the class Lbryio method newInstall.

public static void newInstall(Context context) {
    String appVersion = "";
    if (context != null) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
            appVersion = info.versionName;
        } catch (PackageManager.NameNotFoundException ex) {
        }
    }
    Map<String, String> options = new HashMap<>();
    if (context instanceof MainActivity) {
        String firebaseToken = ((MainActivity) context).getFirebaseMessagingToken();
        if (!Helper.isNullOrEmpty(firebaseToken)) {
            options.put("firebase_token", firebaseToken);
        }
    }
    options.put("app_version", appVersion);
    options.put("app_id", Lbry.INSTALLATION_ID);
    options.put("node_id", "");
    options.put("operating_system", "android");
    options.put("platform", String.format("Android %s (API %d)", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
    options.put("domain", "odysee.com");
    try {
        JSONObject response = (JSONObject) parseResponse(call("install", "new", options, Helper.METHOD_POST, context));
    } catch (LbryioRequestException | LbryioResponseException | ClassCastException ex) {
        // pass
        Log.e(TAG, String.format("install/new failed: %s", ex.getMessage()), ex);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) PackageInfo(android.content.pm.PackageInfo) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) MainActivity(com.odysee.app.MainActivity) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 14 with LbryioResponseException

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

the class Lbryio method fetchCurrentUser.

public static User fetchCurrentUser(Context context) throws AuthTokenInvalidatedException {
    try {
        Response response = Lbryio.call("user", "me", context);
        JSONObject object = (JSONObject) parseResponse(response);
        Type type = new TypeToken<User>() {
        }.getType();
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
        User user = gson.fromJson(object.toString(), type);
        return user;
    } catch (LbryioRequestException | LbryioResponseException | ClassCastException | IllegalStateException ex) {
        LbryAnalytics.logError(String.format("/user/me failed: %s", ex.getMessage()), ex.getClass().getName());
        if (ex instanceof LbryioResponseException) {
            LbryioResponseException error = (LbryioResponseException) ex;
            if (error.getStatusCode() == 403) {
                // auth token invalidated
                AUTH_TOKEN = null;
                throw new AuthTokenInvalidatedException();
            }
        }
        Log.e(TAG, "Could not retrieve the current user", ex);
        return null;
    }
}
Also used : User(com.odysee.app.model.lbryinc.User) GsonBuilder(com.google.gson.GsonBuilder) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) Gson(com.google.gson.Gson) Response(okhttp3.Response) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 15 with LbryioResponseException

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

the class SyncSetTask method doInBackground.

protected String doInBackground(Void... params) {
    try {
        Map<String, String> options = new HashMap<>();
        options.put("old_hash", oldHash);
        options.put("new_hash", newHash);
        options.put("data", data);
        JSONObject response = (JSONObject) Lbryio.parseResponse(Lbryio.call("sync", "set", options, Helper.METHOD_POST, null));
        String hash = Helper.getJSONString("hash", null, response);
        return hash;
    } catch (LbryioRequestException | LbryioResponseException | ClassCastException ex) {
        error = ex;
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Aggregations

LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)19 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)16 JSONObject (org.json.JSONObject)16 HashMap (java.util.HashMap)12 JSONException (org.json.JSONException)8 Map (java.util.Map)5 JSONArray (org.json.JSONArray)5 Bundle (android.os.Bundle)4 ArrayList (java.util.ArrayList)4 Supplier (java.util.function.Supplier)4 Response (okhttp3.Response)4 AccountManager (android.accounts.AccountManager)3 Activity (android.app.Activity)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 MainActivity (com.odysee.app.MainActivity)3 LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)3 Reward (com.odysee.app.model.lbryinc.Reward)3 User (com.odysee.app.model.lbryinc.User)3