forked from commons-app/apps-android-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Categories related client API's migrated to retrofit (commons-app#3053)
* Commit 1 * searchCategories migrated to retrofit * SearchCategoriesFragment migrated to new API * Removed unused code * Created tests * implemented searching by prefix fixed SearchCategoryFragment behaviour where the same categories would be added to the list instead of new ones. * added tests * Migrated searchTitles to searchCategories, function behaviour seems identical
- Loading branch information
1 parent
78141cb
commit d5198be
Showing
8 changed files
with
264 additions
and
170 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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.