Search in sources :

Example 11 with MwException

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

the class DescriptionEditClient method handleError.

private void handleError(@NonNull Call<DescriptionEdit> call, @NonNull DescriptionEdit body, @NonNull Callback cb) {
    MwServiceError error = body.getError();
    String info = body.info();
    RuntimeException exception = new RuntimeException(info != null ? info : "An unknown error occurred");
    if (body.badLoginState() || body.badToken()) {
        cb.invalidLogin(call, exception);
    } else if (error != null && error.hasMessageName(ABUSEFILTER_DISALLOWED)) {
        cb.abusefilter(call, ABUSEFILTER_DISALLOWED, error.getMessageHtml(ABUSEFILTER_DISALLOWED));
    } else if (error != null && error.hasMessageName(ABUSEFILTER_WARNING)) {
        cb.abusefilter(call, ABUSEFILTER_WARNING, error.getMessageHtml(ABUSEFILTER_WARNING));
    } else {
        // noinspection ConstantConditions
        cb.failure(call, new MwException(error));
    }
}
Also used : MwException(org.wikipedia.dataclient.mwapi.MwException) MwServiceError(org.wikipedia.dataclient.mwapi.MwServiceError)

Example 12 with MwException

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

the class EditClient method request.

@VisibleForTesting
@SuppressWarnings("checkstyle:parameternumber")
Call<Edit> request(@NonNull Service service, @NonNull PageTitle title, int section, @NonNull String text, @NonNull String token, @NonNull String summary, @Nullable String baseTimeStamp, boolean loggedIn, @Nullable String captchaId, @Nullable String captchaWord, @NonNull final Callback cb) {
    Call<Edit> call = service.edit(title.getPrefixedText(), section, summary, loggedIn ? "user" : null, text, baseTimeStamp, token, captchaId, captchaWord);
    call.enqueue(new retrofit2.Callback<Edit>() {

        @Override
        public void onResponse(@NonNull Call<Edit> call, @NonNull Response<Edit> response) {
            if (response.body().hasEditResult()) {
                handleEditResult(response.body().edit(), call, cb);
            } else if (response.body().hasError()) {
                RuntimeException e = response.body().badLoginState() ? new UserNotLoggedInException() : new MwException(response.body().getError());
                cb.failure(call, e);
            } else {
                cb.failure(call, new IOException("An unknown error occurred."));
            }
        }

        @Override
        public void onFailure(@NonNull Call<Edit> call, @NonNull 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 13 with MwException

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

the class GalleryCollectionClient method request.

@VisibleForTesting
Map<String, ImageInfo> request(@NonNull Service service, @NonNull PageTitle title, boolean getThumbs) throws IOException, MwException {
    Map<String, ImageInfo> result = new HashMap<>();
    MwQueryResponse currentResponse;
    Map<String, String> continuation = null;
    do {
        currentResponse = continuation == null ? fetch(service, title, getThumbs) : continueFetch(service, title, getThumbs, continuation);
        if (currentResponse.success()) {
            // TODO: Technically, new results should be merged with rather than overwrite old ones.
            // However, Map.merge() requires Java 8. As of this writing (May 2017), the Jack
            // compiler is deprecated, but still required to bump JAVA_VERSION to 1_8.
            // 
            // In the meantime, based on manual testing, overwriting seems not to result in any
            // information loss, and should be adequate in practice.
            // 
            // noinspection ConstantConditions
            result.putAll(currentResponse.query().images());
            continuation = currentResponse.continuation();
        } else if (currentResponse.hasError()) {
            // noinspection ConstantConditions
            throw new MwException(currentResponse.getError());
        } else {
            throw new IOException("An unknown error occurred.");
        }
    } while (continuation != null);
    return result;
}
Also used : HashMap(java.util.HashMap) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 14 with MwException

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

the class GalleryItemClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull final Service service, @NonNull final PageTitle title, @NonNull final Callback cb, final boolean isVideo) {
    Call<MwQueryResponse> call = (isVideo) ? service.requestVideo(title.toString()) : service.requestImage(title.toString());
    call.enqueue(new retrofit2.Callback<MwQueryResponse>() {

        @Override
        public void onResponse(@NonNull Call<MwQueryResponse> call, @NonNull Response<MwQueryResponse> response) {
            MwQueryResponse mwQueryResponse = response.body();
            if (mwQueryResponse != null && mwQueryResponse.success() && mwQueryResponse.query() != null) {
                GalleryItem galleryItem = null;
                if (isVideo) {
                    Map<String, VideoInfo> m = mwQueryResponse.query().videos();
                    if (m.size() != 0) {
                        String key = m.keySet().iterator().next();
                        galleryItem = new GalleryItem(key, m.get(key));
                    }
                } else {
                    Map<String, ImageInfo> m = mwQueryResponse.query().images();
                    if (m.size() != 0) {
                        String key = m.keySet().iterator().next();
                        galleryItem = new GalleryItem(key, m.get(key));
                    }
                }
                if (galleryItem != null) {
                    cb.success(call, galleryItem);
                } else {
                    cb.failure(call, new IOException("An unknown error occurred."));
                }
            } else if (mwQueryResponse != null && mwQueryResponse.hasError()) {
                // noinspection ConstantConditions
                cb.failure(call, new MwException(response.body().getError()));
            } else {
                cb.failure(call, new IOException("Null gallery item received."));
            }
        }

        @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) Map(java.util.Map) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 15 with MwException

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

the class ImageLicenseFetchClient method request.

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

        @Override
        public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
            if (response.body().success()) {
                // noinspection ConstantConditions
                MwQueryPage page = response.body().query().pages().get(0);
                cb.success(call, page.imageInfo() != null && page.imageInfo().getMetadata() != null ? new ImageLicense(page.imageInfo().getMetadata()) : new ImageLicense());
            } 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 : MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) 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