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;
}
}
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);
}
}
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);
}
}
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;
}
}
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;
}
}
Aggregations