Search in sources :

Example 1 with MwException

use of org.wikipedia.dataclient.mwapi.MwException in project apps-android-wikipedia by wikimedia.

the class ReadingListPageInfoClient method request.

@VisibleForTesting
public Call<MwQueryResponse> request(@NonNull Service service, @NonNull List<PageTitle> titles, @NonNull final Callback cb) {
    Call<MwQueryResponse> call = service.request(TextUtils.join("|", titles));
    call.enqueue(new retrofit2.Callback<MwQueryResponse>() {

        @Override
        public void onResponse(@NonNull Call<MwQueryResponse> call, @NonNull Response<MwQueryResponse> response) {
            if (response.isSuccessful()) {
                if (response.body().success()) {
                    // noinspection ConstantConditions
                    cb.success(call, response.body().query().pages());
                } else if (response.body().hasError()) {
                    // noinspection ConstantConditions
                    cb.failure(call, new MwException(response.body().getError()));
                } else {
                    cb.failure(call, new IOException("An unknown error occurred."));
                }
            } else {
                cb.failure(call, RetrofitException.httpError(response));
            }
        }

        @Override
        public void onFailure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable t) {
            cb.failure(call, t);
        }
    });
    return call;
}
Also used : MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 2 with MwException

use of org.wikipedia.dataclient.mwapi.MwException in project apps-android-wikipedia by wikimedia.

the class LoginClient method request.

public void request(@NonNull final WikiSite wiki, @NonNull final String userName, @NonNull final String password, @NonNull final LoginCallback cb) {
    cancel();
    tokenCall = cachedService.service(wiki).requestLoginToken();
    tokenCall.enqueue(new Callback<MwQueryResponse>() {

        @Override
        public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
            if (response.body().success()) {
                // noinspection ConstantConditions
                login(wiki, userName, password, null, response.body().query().loginToken(), cb);
            } else if (response.body().getError() != null) {
                // noinspection ConstantConditions
                cb.error(new MwException(response.body().getError()));
            } else {
                cb.error(new IOException("Unexpected error trying to retrieve login token. " + response.body().toString()));
            }
        }

        @Override
        public void onFailure(Call<MwQueryResponse> call, Throwable caught) {
            cb.error(caught);
        }
    });
}
Also used : MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException)

Example 3 with MwException

use of org.wikipedia.dataclient.mwapi.MwException in project apps-android-wikipedia by wikimedia.

the class NearbyClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull final WikiSite wiki, @NonNull Service service, double latitude, double longitude, double radius, @NonNull final Callback cb) {
    String latLng = String.format(Locale.ROOT, "%f|%f", latitude, longitude);
    radius = Math.min(MAX_RADIUS, radius);
    Call<MwQueryResponse> call = service.request(latLng, radius, Constants.PREFERRED_THUMB_SIZE);
    call.enqueue(new retrofit2.Callback<MwQueryResponse>() {

        @Override
        public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
            // API explicitly tells us we have an error.
            if (response.body().success()) {
                // noinspection ConstantConditions
                cb.success(call, new NearbyResult(wiki, response.body().query().nearbyPages()));
            } else if (response.body().hasError()) {
                // noinspection ConstantConditions
                cb.failure(call, new MwException(response.body().getError()));
            } else {
                cb.success(call, new NearbyResult(wiki, new ArrayList<>()));
            }
        }

        @Override
        public void onFailure(Call<MwQueryResponse> call, Throwable t) {
            cb.failure(call, t);
        }
    });
    return call;
}
Also used : ArrayList(java.util.ArrayList) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 4 with MwException

use of org.wikipedia.dataclient.mwapi.MwException in project apps-android-wikipedia by wikimedia.

the class CreateAccountClient method request.

@SuppressWarnings("checkstyle:parameternumber")
@VisibleForTesting
Call<CreateAccountResponse> request(@NonNull Service service, @NonNull String username, @NonNull String password, @NonNull String retype, @NonNull String token, @Nullable String email, @Nullable String captchaId, @Nullable String captchaWord, @NonNull final Callback cb) {
    Call<CreateAccountResponse> call = service.request(username, password, retype, token, Constants.WIKIPEDIA_URL, email, captchaId, captchaWord);
    call.enqueue(new retrofit2.Callback<CreateAccountResponse>() {

        @Override
        public void onResponse(Call<CreateAccountResponse> call, Response<CreateAccountResponse> response) {
            if (response.body().hasResult()) {
                CreateAccountResponse result = response.body();
                if ("PASS".equals(result.status())) {
                    cb.success(call, new CreateAccountSuccessResult(result.user()));
                } else {
                    cb.failure(call, new CreateAccountException(result.message()));
                }
            } else if (response.body().hasError()) {
                cb.failure(call, new MwException(response.body().getError()));
            } else {
                cb.failure(call, new IOException("An unknown error occurred."));
            }
        }

        @Override
        public void onFailure(Call<CreateAccountResponse> call, Throwable t) {
            cb.failure(call, t);
        }
    });
    return call;
}
Also used : MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 5 with MwException

use of org.wikipedia.dataclient.mwapi.MwException in project apps-android-wikipedia by wikimedia.

the class CsrfTokenClient method requestToken.

@VisibleForTesting
@NonNull
Call<MwQueryResponse> requestToken(@NonNull Service service, @NonNull final Callback cb) {
    Call<MwQueryResponse> call = service.request();
    call.enqueue(new retrofit2.Callback<MwQueryResponse>() {

        @Override
        public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
            if (response.body().success()) {
                // noinspection ConstantConditions
                cb.success(response.body().query().csrfToken());
            } else if (response.body().hasError()) {
                // noinspection ConstantConditions
                cb.failure(new MwException(response.body().getError()));
            } else {
                cb.failure(new IOException("An unknown error occurred."));
            }
        }

        @Override
        public void onFailure(Call<MwQueryResponse> call, Throwable t) {
            cb.failure(t);
        }
    });
    return call;
}
Also used : MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting) NonNull(android.support.annotation.NonNull)

Aggregations

MwException (org.wikipedia.dataclient.mwapi.MwException)20 VisibleForTesting (android.support.annotation.VisibleForTesting)17 IOException (java.io.IOException)15 MwQueryResponse (org.wikipedia.dataclient.mwapi.MwQueryResponse)14 Map (java.util.Map)2 MwQueryPage (org.wikipedia.dataclient.mwapi.MwQueryPage)2 MwServiceError (org.wikipedia.dataclient.mwapi.MwServiceError)2 NonNull (android.support.annotation.NonNull)1 ArrayMap (android.support.v4.util.ArrayMap)1 JsonParseException (com.google.gson.JsonParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ServiceError (org.wikipedia.dataclient.ServiceError)1 HistoryEntry (org.wikipedia.history.HistoryEntry)1 PageTitle (org.wikipedia.page.PageTitle)1 PageImage (org.wikipedia.pageimages.PageImage)1 PageImagesClient (org.wikipedia.pageimages.PageImagesClient)1 ReadingListPage (org.wikipedia.readinglist.database.ReadingListPage)1