Search in sources :

Example 16 with MwQueryResponse

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

the class WikipediaApp method getUserIdForLanguage.

private void getUserIdForLanguage(@NonNull final String code) {
    if (!AccountUtil.isLoggedIn() || TextUtils.isEmpty(AccountUtil.getUserName())) {
        return;
    }
    final WikiSite wikiSite = WikiSite.forLanguageCode(code);
    userInfoClient.request(wikiSite, AccountUtil.getUserName(), new UserExtendedInfoClient.Callback() {

        @Override
        public void success(@NonNull Call<MwQueryResponse> call, int id, @NonNull UserExtendedInfoClient.ListUserResponse user) {
            if (AccountUtil.isLoggedIn()) {
                AccountUtil.putUserIdForLanguage(code, id);
                L.v("Found user ID " + id + " for " + code);
            }
        }

        @Override
        public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
            L.e("Failed to get user ID for " + code, caught);
        }
    });
}
Also used : UserExtendedInfoClient(org.wikipedia.login.UserExtendedInfoClient) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) WikiSite(org.wikipedia.dataclient.WikiSite)

Example 17 with MwQueryResponse

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

the class DescriptionClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull Service service, @NonNull final List<PageTitle> titles, @NonNull final Callback cb) {
    Call<MwQueryResponse> call = service.request(TextUtils.join("|", titles));
    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().pages());
            } 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 18 with MwQueryResponse

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

the class CreateAccountInfoClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@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
                String token = response.body().query().createAccountToken();
                // noinspection ConstantConditions
                String captchaId = response.body().query().captchaId();
                cb.success(call, new CreateAccountInfoResult(token, captchaId));
            } 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 19 with MwQueryResponse

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

the class GalleryItemClientTest method testRequestSuccessForImage.

@Test
public void testRequestSuccessForImage() throws Throwable {
    enqueueFromFile("gallery_item_image.json");
    Callback cb = mock(Callback.class);
    Call<MwQueryResponse> call = request(cb, false);
    server().takeRequest();
    ArgumentCaptor<GalleryItem> captor = ArgumentCaptor.forClass(GalleryItem.class);
    // noinspection unchecked
    verify(cb).success(eq(call), captor.capture());
    // noinspection unchecked
    GalleryItem galleryItem = captor.getValue();
    assertThat(galleryItem != null, is(true));
    assertThat(String.valueOf(galleryItem.getHeight()), is("1489"));
    assertThat(String.valueOf(galleryItem.getWidth()), is("2125"));
    assertThat(galleryItem.getThumbUrl(), is("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Kinkaku3402CBcropped.jpg/1280px-Kinkaku3402CBcropped.jpg"));
    assertThat(galleryItem.getMimeType(), is("image/jpeg"));
    assertThat(galleryItem.getUrl(), is("https://upload.wikimedia.org/wikipedia/commons/c/c9/Kinkaku3402CBcropped.jpg"));
}
Also used : Callback(org.wikipedia.gallery.GalleryItemClient.Callback) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) Test(org.junit.Test) MockWebServerTest(org.wikipedia.test.MockWebServerTest)

Example 20 with MwQueryResponse

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

the class ReadingListPageInfoClientTest method testRequestSuccess.

@Test
@SuppressWarnings("checkstyle:magicnumber")
public void testRequestSuccess() throws Throwable {
    enqueueFromFile("reading_list_page_info.json");
    ReadingListPageInfoClient.Callback cb = mock(ReadingListPageInfoClient.Callback.class);
    Call<MwQueryResponse> call = request(cb);
    server().takeRequest();
    ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
    verify(cb).success(eq(call), captor.capture());
    List<MwQueryPage> result = captor.getValue();
    MwQueryPage biden = result.get(0);
    MwQueryPage obama = result.get(1);
    assertThat(biden.title(), is("Joe Biden"));
    assertThat(biden.description(), is("47th Vice President of the United States"));
    assertThat(biden.thumbUrl(), is("https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Official_portrait_of_Vice_President_Joe_Biden.jpg/255px-Official_portrait_of_Vice_President_Joe_Biden.jpg"));
    assertThat(obama.title(), is("Barack Obama"));
    assertThat(obama.description(), is("44th President of the United States of America"));
    assertThat(obama.thumbUrl(), is("https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/256px-President_Barack_Obama.jpg"));
}
Also used : MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) List(java.util.List) Test(org.junit.Test) MockWebServerTest(org.wikipedia.test.MockWebServerTest)

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