Search in sources :

Example 6 with MwException

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

the class EditPreviewClient method request.

@VisibleForTesting
Call<EditPreview> request(@NonNull Service service, @NonNull PageTitle title, @NonNull String wikitext, @NonNull final Callback cb) {
    Call<EditPreview> call = service.previewEdit(title.getPrefixedText(), wikitext);
    call.enqueue(new retrofit2.Callback<EditPreview>() {

        @Override
        public void onResponse(Call<EditPreview> call, Response<EditPreview> response) {
            if (response.body().success() && response.body().hasPreviewResult()) {
                cb.success(call, response.body().result());
            } 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<EditPreview> 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 7 with MwException

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

the class WikitextClient method request.

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

        @Override
        public void onResponse(@NonNull Call<MwQueryResponse> call, @NonNull Response<MwQueryResponse> response) {
            // noinspection ConstantConditions
            if (response.body() != null && response.body().success() && response.body().query() != null && response.body().query().firstPage() != null && getRevision(response.body().query()) != null) {
                // noinspection ConstantConditions
                MwQueryPage.Revision rev = getRevision(response.body().query());
                cb.success(call, response.body().query().firstPage().title(), rev.content(), rev.timeStamp());
            } else if (response.body() != null && response.body().hasError()) {
                // noinspection ConstantConditions
                cb.failure(call, new MwException(response.body().getError()));
            } else {
                Throwable t = new JsonParseException("Error parsing wikitext from query response");
                cb.failure(call, t);
            }
        }

        @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) JsonParseException(com.google.gson.JsonParseException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 8 with MwException

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

the class DescriptionClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull Service service, @NonNull final 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(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
            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."));
            }
        }

        @Override
        public void onFailure(Call<MwQueryResponse> call, 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 9 with MwException

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

the class CaptchaClient method request.

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

        @Override
        public void onResponse(Call<Captcha> call, Response<Captcha> response) {
            if (response.body().success()) {
                cb.success(call, new CaptchaResult(response.body().captchaId()));
            } 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."));
            }
        }

        @Override
        public void onFailure(Call<Captcha> 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 10 with MwException

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

the class CreateAccountInfoClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@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
                String token = response.body().query().createAccountToken();
                // noinspection ConstantConditions
                String captchaId = response.body().query().captchaId();
                cb.success(call, new CreateAccountInfoResult(token, captchaId));
            } 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."));
            }
        }

        @Override
        public void onFailure(Call<MwQueryResponse> call, 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)

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