Search in sources :

Example 11 with LbryioRequestException

use of com.odysee.app.exceptions.LbryioRequestException 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 LbryioRequestException

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

the class Lbryio method call.

public static Response call(String resource, String action, Map<String, String> options, String method, Context context) throws LbryioRequestException, LbryioResponseException {
    String authToken = AUTH_TOKEN;
    if (context != null) {
        AccountManager am = AccountManager.get(context);
        Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
        if (odyseeAccount != null) {
            authToken = am.peekAuthToken(odyseeAccount, "auth_token_type");
        }
    }
    if (options != null && options.containsKey("auth_token"))
        authToken = options.get("auth_token");
    if (Helper.isNullOrEmpty(authToken) && !generatingAuthToken) {
        // Only call getAuthToken if not calling /user/new
        authToken = getAuthToken(context);
    }
    /*if (BuildConfig.DEBUG) {
            Log.d(TAG, String.format("Using authToken for request: %s", authToken));
        }*/
    String url = String.format("%s/%s/%s", CONNECTION_STRING, resource, action);
    if (Helper.METHOD_GET.equalsIgnoreCase(method)) {
        Uri.Builder uriBuilder = Uri.parse(url).buildUpon();
        if (!Helper.isNullOrEmpty(authToken)) {
            uriBuilder.appendQueryParameter(AUTH_TOKEN_PARAM, authToken);
        }
        if (options != null) {
            for (Map.Entry<String, String> option : options.entrySet()) {
                uriBuilder.appendQueryParameter(option.getKey(), option.getValue());
            }
        }
        url = uriBuilder.build().toString();
    }
    /*if (BuildConfig.DEBUG) {
            Log.d(TAG, String.format("Request Method: %s, Sending request to URL: %s", method, url));
        }*/
    Request.Builder builder = new Request.Builder().url(url);
    if (Helper.METHOD_POST.equalsIgnoreCase(method)) {
        RequestBody body = RequestBody.create(buildQueryString(authToken, options), Helper.FORM_MEDIA_TYPE);
        builder.post(body);
    }
    Request request = builder.build();
    OkHttpClient client = new OkHttpClient.Builder().writeTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS).build();
    try {
        return client.newCall(request).execute();
    } catch (IOException ex) {
        throw new LbryioRequestException(String.format("%s request to %s/%s failed", method, resource, action), ex);
    }
}
Also used : Account(android.accounts.Account) OkHttpClient(okhttp3.OkHttpClient) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) Request(okhttp3.Request) IOException(java.io.IOException) Uri(android.net.Uri) AccountManager(android.accounts.AccountManager) HashMap(java.util.HashMap) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 13 with LbryioRequestException

use of com.odysee.app.exceptions.LbryioRequestException 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 LbryioRequestException

use of com.odysee.app.exceptions.LbryioRequestException 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 LbryioRequestException

use of com.odysee.app.exceptions.LbryioRequestException 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

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