Search in sources :

Example 1 with HttpManager

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));
        }
    });
}
Also used : NewsError(email.schaal.ocreader.api.json.NewsError) Status(email.schaal.ocreader.api.json.Status) Call(retrofit2.Call) Moshi(com.squareup.moshi.Moshi) HttpManager(email.schaal.ocreader.http.HttpManager) LoginError(email.schaal.ocreader.util.LoginError) HttpUrl(okhttp3.HttpUrl) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) APILevels(email.schaal.ocreader.api.json.APILevels) Callback(retrofit2.Callback) Version(com.github.zafarkhaja.semver.Version) NonNull(android.support.annotation.NonNull)

Aggregations

NonNull (android.support.annotation.NonNull)1 Version (com.github.zafarkhaja.semver.Version)1 Moshi (com.squareup.moshi.Moshi)1 APILevels (email.schaal.ocreader.api.json.APILevels)1 NewsError (email.schaal.ocreader.api.json.NewsError)1 Status (email.schaal.ocreader.api.json.Status)1 HttpManager (email.schaal.ocreader.http.HttpManager)1 LoginError (email.schaal.ocreader.util.LoginError)1 HttpUrl (okhttp3.HttpUrl)1 Call (retrofit2.Call)1 Callback (retrofit2.Callback)1 Response (retrofit2.Response)1 Retrofit (retrofit2.Retrofit)1