use of email.schaal.ocreader.http.HttpManager in project ocreader by schaal.
the class API method login.
public static void login(final Context context, final HttpUrl baseUrl, final String username, final String password, final APICallback<Status, LoginError> loginCallback) {
final HttpManager httpManager = new HttpManager(username, password, baseUrl);
final HttpUrl resolvedBaseUrl = baseUrl.resolve("");
if (resolvedBaseUrl == null) {
loginCallback.onFailure(new LoginError("Couldn't parse URL"));
return;
}
final Moshi moshi = new Moshi.Builder().build();
final JsonAdapter<NewsError> errorJsonAdapter = moshi.adapter(NewsError.class);
final Retrofit retrofit = new Retrofit.Builder().baseUrl(resolvedBaseUrl).client(httpManager.getClient()).addConverterFactory(MoshiConverterFactory.create(moshi)).build();
final CommonAPI commonAPI = retrofit.create(CommonAPI.class);
commonAPI.apiLevels().enqueue(new Callback<APILevels>() {
@Override
public void onResponse(@NonNull Call<APILevels> call, @NonNull Response<APILevels> response) {
if (response.isSuccessful()) {
loginInstance = null;
final APILevels apiLevels = response.body();
final Level apiLevel = apiLevels != null ? apiLevels.highestSupportedApi() : null;
loginInstance = Level.getAPI(context, apiLevel);
if (apiLevel == null) {
loginCallback.onFailure(new LoginError(context.getString(R.string.error_not_compatible)));
} else {
loginInstance.setupApi(httpManager);
loginInstance.metaData(new Callback<Status>() {
@Override
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) {
final Status status = response.body();
final Version version = status != null ? status.getVersion() : null;
if (version != null && MIN_VERSION.lessThanOrEqualTo(version)) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(Preferences.USERNAME.getKey(), username).putString(Preferences.PASSWORD.getKey(), password).putString(Preferences.URL.getKey(), resolvedBaseUrl.toString()).putString(Preferences.SYS_DETECTED_API_LEVEL.getKey(), apiLevel.getLevel()).apply();
instance = loginInstance;
loginInstance = null;
loginCallback.onSuccess(status);
} else {
if (version != null) {
Log.d(TAG, String.format("Nextcloud News version is less than minimally supported version: %s < %s", version.toString(), MIN_VERSION.toString()));
loginCallback.onFailure(new LoginError(context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
} else {
Log.d(TAG, "Couldn't parse Nextcloud News version");
loginCallback.onFailure(new LoginError(context.getString(R.string.failed_detect_nc_version)));
}
}
} else {
String message = getErrorMessage(errorJsonAdapter, response);
Log.d(TAG, "Metadata call failed with error: " + message);
loginCallback.onFailure(LoginError.getError(context, response.code(), message));
}
}
@Override
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.e(TAG, "Failed to log in", t);
loginCallback.onFailure(LoginError.getError(context, t));
}
});
}
} else {
Log.d(TAG, "API level call failed with error: " + response.code() + " " + getErrorMessage(errorJsonAdapter, response));
// Either nextcloud news is not installed or version prior 8.8.0
loginCallback.onFailure(LoginError.getError(context, response.code(), context.getString(R.string.ncnews_too_old, MIN_VERSION.toString())));
}
}
@Override
public void onFailure(@NonNull Call<APILevels> call, @NonNull Throwable t) {
Log.e(TAG, "Failed to log in", t);
loginCallback.onFailure(LoginError.getError(context, t));
}
});
}
Aggregations