-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Categories related client API's migrated to retrofit #3053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maskaravivek
merged 8 commits into
commons-app:backend-overhaul
from
ilgazer:backend-overhaul-retrofit-categories
Jul 5, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f2cfccd
Commit 1
ilgazer e5c0dc9
searchCategories migrated to retrofit
ilgazer 42e89a8
SearchCategoriesFragment migrated to new API
ilgazer d3f54e1
Removed unused code
ilgazer 9d026e4
Created tests
ilgazer 7679136
implemented searching by prefix
ilgazer d95c85b
added tests
ilgazer fabf903
Migrated searchTitles to searchCategories, function behaviour seems i…
ilgazer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/fr/free/nrw/commons/category/CategoryClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package fr.free.nrw.commons.category; | ||
|
|
||
|
|
||
| import org.wikipedia.dataclient.mwapi.MwQueryPage; | ||
| import org.wikipedia.dataclient.mwapi.MwQueryResponse; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import io.reactivex.Observable; | ||
| import timber.log.Timber; | ||
|
|
||
| /** | ||
| * Category Client to handle custom calls to Commons MediaWiki APIs | ||
| */ | ||
| @Singleton | ||
| public class CategoryClient { | ||
|
|
||
| private final CategoryInterface CategoryInterface; | ||
|
|
||
| @Inject | ||
| public CategoryClient(CategoryInterface CategoryInterface) { | ||
| this.CategoryInterface = CategoryInterface; | ||
| } | ||
|
|
||
| /** | ||
| * Searches for categories containing the specified string. | ||
| * | ||
| * @param filter The string to be searched | ||
| * @param itemLimit How many results are returned | ||
| * @param offset Starts returning items from the nth result. If offset is 9, the response starts with the 9th item of the search result | ||
| * @return | ||
| */ | ||
| public Observable<String> searchCategories(String filter, int itemLimit, int offset) { | ||
| return responseToCategoryName(CategoryInterface.searchCategories(filter, itemLimit, offset)); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Searches for categories containing the specified string. | ||
| * | ||
| * @param filter The string to be searched | ||
| * @param itemLimit How many results are returned | ||
| * @return | ||
| */ | ||
| public Observable<String> searchCategories(String filter, int itemLimit) { | ||
| return searchCategories(filter, itemLimit, 0); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Searches for categories starting with the specified string. | ||
| * | ||
| * @param prefix The prefix to be searched | ||
| * @param itemLimit How many results are returned | ||
| * @param offset Starts returning items from the nth result. If offset is 9, the response starts with the 9th item of the search result | ||
| * @return | ||
| */ | ||
| public Observable<String> searchCategoriesForPrefix(String prefix, int itemLimit, int offset) { | ||
| return responseToCategoryName(CategoryInterface.searchCategoriesForPrefix(prefix, itemLimit, offset)); | ||
| } | ||
|
|
||
| /** | ||
| * Searches for categories starting with the specified string. | ||
| * | ||
| * @param prefix The prefix to be searched | ||
| * @param itemLimit How many results are returned | ||
| * @return | ||
| */ | ||
| public Observable<String> searchCategoriesForPrefix(String prefix, int itemLimit) { | ||
| return searchCategoriesForPrefix(prefix, itemLimit, 0); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Internal function to reduce code reuse. Extracts the categories returned from MwQueryResponse. | ||
| * | ||
| * @param responseObservable The query response observable | ||
| * @return Observable emitting the categories returned. If our search yielded "Category:Test", "Test" is emitted. | ||
| */ | ||
| private Observable<String> responseToCategoryName(Observable<MwQueryResponse> responseObservable) { | ||
| return responseObservable | ||
| .flatMap(mwQueryResponse -> { | ||
| List<MwQueryPage> pages = mwQueryResponse.query().pages(); | ||
| if (pages != null) | ||
| return Observable.fromIterable(pages); | ||
| else | ||
| Timber.d("No categories returned."); | ||
| return Observable.empty(); | ||
| }) | ||
| .map(MwQueryPage::title) | ||
| .doOnEach(s -> Timber.d("Category returned: %s", s)) | ||
| .map(cat -> cat.replace("Category:", "")); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/fr/free/nrw/commons/category/CategoryInterface.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package fr.free.nrw.commons.category; | ||
|
|
||
| import org.wikipedia.dataclient.mwapi.MwQueryResponse; | ||
|
|
||
| import io.reactivex.Observable; | ||
| import retrofit2.http.GET; | ||
| import retrofit2.http.Query; | ||
|
|
||
| /** | ||
| * Interface for interacting with Commons category related APIs | ||
| */ | ||
| public interface CategoryInterface { | ||
|
|
||
| /** | ||
| * Searches for categories with the specified name. | ||
| * Replaces ApacheHttpClientMediaWikiApi#allCategories | ||
| * | ||
| * @param filter The string to be searched | ||
| * @param itemLimit How many results are returned | ||
| * @return | ||
| */ | ||
| @GET("w/api.php?action=query&format=json&formatversion=2" | ||
| + "&generator=search&gsrnamespace=14") | ||
| Observable<MwQueryResponse> searchCategories(@Query("gsrsearch") String filter, | ||
| @Query("gsrlimit") int itemLimit, @Query("gsroffset") int offset); | ||
|
|
||
| @GET("w/api.php?action=query&format=json&formatversion=2" | ||
| + "&generator=allcategories") | ||
| Observable<MwQueryResponse> searchCategoriesForPrefix(@Query("gacprefix") String prefix, | ||
| @Query("gaclimit") int itemLimit, @Query("gacoffset") int offset); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.