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