Search in sources :

Example 21 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse 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 22 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse 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 23 with MwQueryResponse

use of org.wikipedia.dataclient.mwapi.MwQueryResponse 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)

Example 24 with MwQueryResponse

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

the class LangLinksClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull Service service, @NonNull PageTitle title, @NonNull final Callback cb) {
    Call<MwQueryResponse> call = service.langLinks(title.getPrefixedText());
    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().langLinks());
            } 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 25 with MwQueryResponse

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

the class FullTextSearchClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull Service service, @NonNull final WikiSite wiki, @NonNull String searchTerm, @Nullable String cont, @Nullable String gsrOffset, int limit, @NonNull final Callback cb) {
    call = service.request(searchTerm, limit, cont, gsrOffset);
    call.enqueue(new retrofit2.Callback<MwQueryResponse>() {

        @Override
        public void onResponse(@NonNull Call<MwQueryResponse> call, @NonNull Response<MwQueryResponse> response) {
            if (response.body().success()) {
                // noinspection ConstantConditions
                cb.success(call, new SearchResults(response.body().query().pages(), wiki, response.body().continuation(), null));
            } else if (response.body().hasError()) {
                // noinspection ConstantConditions
                cb.failure(call, new MwException(response.body().getError()));
            } else {
                // A 'morelike' search query with no results will just return an API warning:
                // 
                // {
                // "batchcomplete": true,
                // "warnings": {
                // "search": {
                // "warnings": "No valid titles provided to 'morelike'."
                // }
                // }
                // }
                // 
                // Just return an empty SearchResults() in this case.
                cb.success(call, new SearchResults());
            }
        }

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

Aggregations

MwQueryResponse (org.wikipedia.dataclient.mwapi.MwQueryResponse)27 MwException (org.wikipedia.dataclient.mwapi.MwException)14 VisibleForTesting (android.support.annotation.VisibleForTesting)12 IOException (java.io.IOException)12 Test (org.junit.Test)7 MockWebServerTest (org.wikipedia.test.MockWebServerTest)7 MwQueryPage (org.wikipedia.dataclient.mwapi.MwQueryPage)5 Callback (org.wikipedia.gallery.GalleryItemClient.Callback)5 ArrayList (java.util.ArrayList)3 NonNull (android.support.annotation.NonNull)2 List (java.util.List)2 Map (java.util.Map)2 PageTitle (org.wikipedia.page.PageTitle)2 PageImage (org.wikipedia.pageimages.PageImage)2 PageImagesClient (org.wikipedia.pageimages.PageImagesClient)2 Bitmap (android.graphics.Bitmap)1 ArrayMap (android.support.v4.util.ArrayMap)1 JsonParseException (com.google.gson.JsonParseException)1 HashMap (java.util.HashMap)1 ServiceError (org.wikipedia.dataclient.ServiceError)1