use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.
the class LoginClient method request.
public void request(@NonNull final WikiSite wiki, @NonNull final String userName, @NonNull final String password, @NonNull final LoginCallback cb) {
cancel();
tokenCall = cachedService.service(wiki).requestLoginToken();
tokenCall.enqueue(new Callback<MwQueryResponse>() {
@Override
public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
if (response.body().success()) {
// noinspection ConstantConditions
login(wiki, userName, password, null, response.body().query().loginToken(), cb);
} else if (response.body().getError() != null) {
// noinspection ConstantConditions
cb.error(new MwException(response.body().getError()));
} else {
cb.error(new IOException("Unexpected error trying to retrieve login token. " + response.body().toString()));
}
}
@Override
public void onFailure(Call<MwQueryResponse> call, Throwable caught) {
cb.error(caught);
}
});
}
use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.
the class NearbyClient method request.
@VisibleForTesting
Call<MwQueryResponse> request(@NonNull final WikiSite wiki, @NonNull Service service, double latitude, double longitude, double radius, @NonNull final Callback cb) {
String latLng = String.format(Locale.ROOT, "%f|%f", latitude, longitude);
radius = Math.min(MAX_RADIUS, radius);
Call<MwQueryResponse> call = service.request(latLng, radius, Constants.PREFERRED_THUMB_SIZE);
call.enqueue(new retrofit2.Callback<MwQueryResponse>() {
@Override
public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
// API explicitly tells us we have an error.
if (response.body().success()) {
// noinspection ConstantConditions
cb.success(call, new NearbyResult(wiki, response.body().query().nearbyPages()));
} else if (response.body().hasError()) {
// noinspection ConstantConditions
cb.failure(call, new MwException(response.body().getError()));
} else {
cb.success(call, new NearbyResult(wiki, new ArrayList<>()));
}
}
@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 CsrfTokenClient method getTokenBlocking.
@NonNull
public String getTokenBlocking() throws Throwable {
String token = "";
Service service = cachedService.service(csrfWikiSite);
for (int retry = 0; retry < MAX_RETRIES_OF_LOGIN_BLOCKING; retry++) {
try {
if (retry > 0) {
// Log in explicitly
new LoginClient().loginBlocking(loginWikiSite, AccountUtil.getUserName(), AccountUtil.getPassword(), "");
}
Response<MwQueryResponse> response = service.request().execute();
if (response.body() == null || !response.body().success() || TextUtils.isEmpty(response.body().query().csrfToken())) {
continue;
}
token = response.body().query().csrfToken();
if (AccountUtil.isLoggedIn() && token.equals(ANON_TOKEN)) {
throw new RuntimeException("App believes we're logged in, but got anonymous token.");
}
break;
} catch (Throwable t) {
L.w(t);
}
}
if (TextUtils.isEmpty(token) || token.equals(ANON_TOKEN)) {
throw new IOException("Invalid token, or login failure.");
}
return token;
}
use of org.wikipedia.dataclient.mwapi.MwQueryResponse in project apps-android-wikipedia by wikimedia.
the class CsrfTokenClient method requestToken.
@VisibleForTesting
@NonNull
Call<MwQueryResponse> requestToken(@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
cb.success(response.body().query().csrfToken());
} else if (response.body().hasError()) {
// noinspection ConstantConditions
cb.failure(new MwException(response.body().getError()));
} else {
cb.failure(new IOException("An unknown error occurred."));
}
}
@Override
public void onFailure(Call<MwQueryResponse> call, Throwable t) {
cb.failure(t);
}
});
return call;
}
use of org.wikipedia.dataclient.mwapi.MwQueryResponse 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;
}
Aggregations