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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations