Search in sources :

Example 16 with MwException

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

Example 17 with MwException

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

Example 18 with MwException

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

the class PrefixSearchClient method request.

@VisibleForTesting
Call<PrefixSearchResponse> request(@NonNull Service service, @NonNull final WikiSite wiki, @NonNull String title, @NonNull final Callback cb) {
    call = service.request(title, title);
    call.enqueue(new retrofit2.Callback<PrefixSearchResponse>() {

        @Override
        public void onResponse(@NonNull Call<PrefixSearchResponse> call, @NonNull Response<PrefixSearchResponse> response) {
            if (response.body() != null && response.body().success() && response.body().query().pages() != null) {
                // noinspection ConstantConditions
                List<MwQueryPage> pages = response.body().query().pages();
                // noinspection ConstantConditions
                cb.success(call, new SearchResults(pages, wiki, response.body().continuation(), response.body().suggestion()));
            } else if (response.body() != null && response.body().hasError()) {
                // noinspection ConstantConditions
                cb.failure(call, new MwException(response.body().getError()));
            } else {
                // A prefix search query with no results will return the following:
                // 
                // {
                // "batchcomplete": true,
                // "query": {
                // "search": []
                // }
                // }
                // 
                // Just return an empty SearchResults() in this case.
                cb.success(call, new SearchResults());
            }
        }

        @Override
        public void onFailure(@NonNull Call<PrefixSearchResponse> call, @NonNull Throwable caught) {
            cb.failure(call, caught);
        }
    });
    return call;
}
Also used : List(java.util.List) MwException(org.wikipedia.dataclient.mwapi.MwException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 19 with MwException

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

the class PageFragmentLoadState method pageLoadLeadSectionComplete.

private void pageLoadLeadSectionComplete(PageLead pageLead, int startSequenceNum) {
    if (!fragment.isAdded() || !sequenceNumber.inSync(startSequenceNum)) {
        return;
    }
    if (pageLead.hasError()) {
        ServiceError error = pageLead.getError();
        if (error != null) {
            commonSectionFetchOnCatch(new MwException((MwServiceError) error), startSequenceNum);
        } else {
            commonSectionFetchOnCatch(new IOException(getResources().getString(R.string.error_unknown)), startSequenceNum);
        }
        return;
    }
    Page page = pageLead.toPage(model.getTitle());
    model.setPage(page);
    model.setTitle(page.getTitle());
    editHandler.setPage(model.getPage());
    if (page.getTitle().getDescription() == null) {
        app.getSessionFunnel().noDescription();
    }
    layoutLeadImage(() -> {
        if (!fragment.isAdded()) {
            return;
        }
        fragment.callback().onPageInvalidateOptionsMenu();
        pageLoadRemainingSections(sequenceNumber.get());
    });
    // Update our history entry, in case the Title was changed (i.e. normalized)
    final HistoryEntry curEntry = model.getCurEntry();
    model.setCurEntry(new HistoryEntry(model.getTitle(), curEntry.getTimestamp(), curEntry.getSource()));
    // Fetch larger thumbnail URL from the network, and save it to our DB.
    new PageImagesClient().request(model.getTitle().getWikiSite(), Collections.singletonList(model.getTitle()), new PageImagesClient.Callback() {

        @Override
        public void success(@NonNull Call<MwQueryResponse> call, @NonNull Map<PageTitle, PageImage> results) {
            if (results.containsKey(model.getTitle())) {
                PageImage pageImage = results.get(model.getTitle());
                app.getDatabaseClient(PageImage.class).upsert(pageImage, PageImageHistoryContract.Image.SELECTION);
                updateThumbnail(pageImage.getImageName());
            }
        }

        @Override
        public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
            L.w(caught);
        }
    });
}
Also used : MwServiceError(org.wikipedia.dataclient.mwapi.MwServiceError) ServiceError(org.wikipedia.dataclient.ServiceError) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) ReadingListPage(org.wikipedia.readinglist.database.ReadingListPage) IOException(java.io.IOException) PageImage(org.wikipedia.pageimages.PageImage) MwServiceError(org.wikipedia.dataclient.mwapi.MwServiceError) PageImagesClient(org.wikipedia.pageimages.PageImagesClient) HistoryEntry(org.wikipedia.history.HistoryEntry)

Example 20 with MwException

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

the class PageImagesClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull final WikiSite wiki, @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(@NonNull Call<MwQueryResponse> call, @NonNull Response<MwQueryResponse> response) {
            Map<PageTitle, PageImage> pageImagesMap = new ArrayMap<>();
            // error cases
            if (response.body().success()) {
                // nominal case
                Map<String, PageTitle> titlesMap = new ArrayMap<>();
                for (PageTitle title : titles) {
                    titlesMap.put(title.getPrefixedText(), title);
                }
                Map<String, String> thumbnailSourcesMap = new ArrayMap<>();
                // noinspection ConstantConditions
                for (MwQueryPage page : response.body().query().pages()) {
                    thumbnailSourcesMap.put(new PageTitle(null, page.title(), wiki).getPrefixedText(), page.thumbUrl());
                    if (!TextUtils.isEmpty(page.convertedFrom())) {
                        thumbnailSourcesMap.put(new PageTitle(null, page.convertedFrom(), wiki).getPrefixedText(), page.thumbUrl());
                    }
                    if (!TextUtils.isEmpty(page.redirectFrom())) {
                        thumbnailSourcesMap.put(new PageTitle(null, page.redirectFrom(), wiki).getPrefixedText(), page.thumbUrl());
                    }
                }
                for (String key : titlesMap.keySet()) {
                    if (thumbnailSourcesMap.containsKey(key)) {
                        PageTitle title = titlesMap.get(key);
                        pageImagesMap.put(title, new PageImage(title, thumbnailSourcesMap.get(key)));
                    }
                }
                cb.success(call, pageImagesMap);
            } 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(@NonNull Call<MwQueryResponse> call, @NonNull Throwable t) {
            cb.failure(call, t);
        }
    });
    return call;
}
Also used : MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) PageTitle(org.wikipedia.page.PageTitle) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) VisibleForTesting(android.support.annotation.VisibleForTesting)

Aggregations

MwException (org.wikipedia.dataclient.mwapi.MwException)20 VisibleForTesting (android.support.annotation.VisibleForTesting)17 IOException (java.io.IOException)15 MwQueryResponse (org.wikipedia.dataclient.mwapi.MwQueryResponse)14 Map (java.util.Map)2 MwQueryPage (org.wikipedia.dataclient.mwapi.MwQueryPage)2 MwServiceError (org.wikipedia.dataclient.mwapi.MwServiceError)2 NonNull (android.support.annotation.NonNull)1 ArrayMap (android.support.v4.util.ArrayMap)1 JsonParseException (com.google.gson.JsonParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ServiceError (org.wikipedia.dataclient.ServiceError)1 HistoryEntry (org.wikipedia.history.HistoryEntry)1 PageTitle (org.wikipedia.page.PageTitle)1 PageImage (org.wikipedia.pageimages.PageImage)1 PageImagesClient (org.wikipedia.pageimages.PageImagesClient)1 ReadingListPage (org.wikipedia.readinglist.database.ReadingListPage)1