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