Search in sources :

Example 11 with MwQueryResponse

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);
        }
    });
}
Also used : MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException)

Example 12 with MwQueryResponse

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;
}
Also used : ArrayList(java.util.ArrayList) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 13 with MwQueryResponse

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;
}
Also used : LoginClient(org.wikipedia.login.LoginClient) WikiCachedService(org.wikipedia.dataclient.retrofit.WikiCachedService) MwCachedService(org.wikipedia.dataclient.retrofit.MwCachedService) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) IOException(java.io.IOException) NonNull(android.support.annotation.NonNull)

Example 14 with MwQueryResponse

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;
}
Also used : MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting) NonNull(android.support.annotation.NonNull)

Example 15 with MwQueryResponse

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;
}
Also used : MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) JsonParseException(com.google.gson.JsonParseException) 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