Skip to content

Commit 8b36ee5

Browse files
ilgazermaskaravivek
authored andcommitted
Migrated OkHttpJsonApiClient#getMedia and getPictureOfTheDay to MediaClient (#3056)
* With media client APIs migrated to retrofit (#2998) * With media client APIs migrated to retrofit * Add test cases and java docs * Fix test * Fix build * Fix build and other minor issues * Fix tests * Categories related client API's migrated to retrofit (#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 * With media client APIs migrated to retrofit (#2998) * With media client APIs migrated to retrofit * Add test cases and java docs * Fix test * Fix build * Fix build and other minor issues * Fix tests * Categories related client API's migrated to retrofit (#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 * OkHttpJsonApi#getMediaList migrated to retrofit (#3054) * Migrated OkHttpJsonApi#getMediaList partially to MediaClient#getCategoryImages * Migrated rest of OkHttpJsonApi#getMediaList functionality to MediaClient#getCategoryImages * Removed unused code and tests * Fixed small bug * Added tests * Removed unused CategoryImageController * getSubCategoryList and getParentCategoryList migrated to retrofit (#3055) * Migrated getSubCategoryList to retrofit * Migrated getParentCategoryList to retrofit * Removed obsolete functions * Added tests * Fixed small bugs * Migrated OkHttpJsonApiClient#getMedia and getPictureOfTheDay to MediaClient * Removed obsolete functions and added tests * Fixed merge errors * With media client APIs migrated to retrofit (#2998) * With media client APIs migrated to retrofit * Add test cases and java docs * Fix test * Fix build * Fix build and other minor issues * Fix tests * Categories related client API's migrated to retrofit (#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 * OkHttpJsonApi#getMediaList migrated to retrofit (#3054) * Migrated OkHttpJsonApi#getMediaList partially to MediaClient#getCategoryImages * Migrated rest of OkHttpJsonApi#getMediaList functionality to MediaClient#getCategoryImages * Removed unused code and tests * Fixed small bug * Added tests * Removed unused CategoryImageController * getSubCategoryList and getParentCategoryList migrated to retrofit (#3055) * Migrated getSubCategoryList to retrofit * Migrated getParentCategoryList to retrofit * Removed obsolete functions * Added tests * Fixed small bugs * Consume login client from data client library (#2894) Fix actions for review client Use data client library for notifications With delete helper migrated to data client With wikidata edits With notifications and modifications migrated to data client With upload migrated to retrofit Delete unused code Reuse thank interface from the library
1 parent d24ca9d commit 8b36ee5

15 files changed

+181
-159
lines changed

app/src/main/java/fr/free/nrw/commons/Media.java

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
public class Media implements Parcelable {
3131

32+
public static final Media EMPTY = new Media("");
3233
public static Creator<Media> CREATOR = new Creator<Media>() {
3334
@Override
3435
public Media createFromParcel(Parcel parcel) {

app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Single<Media> fetchMediaDetails(String filename) {
5757
* @return return data rich Media object
5858
*/
5959
public Single<Media> getMediaFromFileName(String filename) {
60-
return okHttpJsonApiClient.getMedia(filename, false);
60+
return mediaClient.getMedia(filename);
6161
}
6262

6363
/**

app/src/main/java/fr/free/nrw/commons/auth/SessionManager.java

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import fr.free.nrw.commons.mwapi.MediaWikiApi;
2121
import io.reactivex.Completable;
2222
import io.reactivex.Observable;
23+
import timber.log.Timber;
2324

2425
/**
2526
* Manage the current logged in user session.

app/src/main/java/fr/free/nrw/commons/bookmarks/pictures/BookmarkPicturesController.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import fr.free.nrw.commons.Media;
1212
import fr.free.nrw.commons.bookmarks.Bookmark;
13+
import fr.free.nrw.commons.media.MediaClient;
1314
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
1415
import io.reactivex.Observable;
1516
import io.reactivex.ObservableSource;
@@ -19,15 +20,14 @@
1920
@Singleton
2021
public class BookmarkPicturesController {
2122

22-
private final OkHttpJsonApiClient okHttpJsonApiClient;
23+
private final MediaClient mediaClient;
2324
private final BookmarkPicturesDao bookmarkDao;
2425

2526
private List<Bookmark> currentBookmarks;
2627

2728
@Inject
28-
public BookmarkPicturesController(OkHttpJsonApiClient okHttpJsonApiClient,
29-
BookmarkPicturesDao bookmarkDao) {
30-
this.okHttpJsonApiClient = okHttpJsonApiClient;
29+
public BookmarkPicturesController(MediaClient mediaClient, BookmarkPicturesDao bookmarkDao) {
30+
this.mediaClient = mediaClient;
3131
this.bookmarkDao = bookmarkDao;
3232
currentBookmarks = new ArrayList<>();
3333
}
@@ -47,7 +47,7 @@ Single<List<Media>> loadBookmarkedPictures() {
4747

4848
private Observable<Media> getMediaFromBookmark(Bookmark bookmark) {
4949
Media dummyMedia = new Media("");
50-
return okHttpJsonApiClient.getMedia(bookmark.getMediaName(), false)
50+
return mediaClient.getMedia(bookmark.getMediaName())
5151
.map(media -> media == null ? dummyMedia : media)
5252
.onErrorReturn(throwable -> dummyMedia)
5353
.toObservable();

app/src/main/java/fr/free/nrw/commons/media/MediaClient.java

+57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package fr.free.nrw.commons.media;
22

33

4+
import androidx.annotation.NonNull;
5+
import androidx.annotation.Nullable;
6+
7+
import org.wikipedia.dataclient.mwapi.MwQueryPage;
8+
import org.wikipedia.dataclient.mwapi.MwQueryResponse;
9+
import org.wikipedia.dataclient.mwapi.MwQueryResult;
10+
11+
import java.util.Date;
412
import org.wikipedia.dataclient.mwapi.MwQueryResponse;
513

614
import java.util.ArrayList;
@@ -13,6 +21,13 @@
1321
import javax.inject.Singleton;
1422

1523
import fr.free.nrw.commons.Media;
24+
import fr.free.nrw.commons.utils.CommonsDateUtil;
25+
import io.reactivex.Observable;
26+
import io.reactivex.Single;
27+
import okhttp3.HttpUrl;
28+
import okhttp3.Request;
29+
import okhttp3.Response;
30+
import timber.log.Timber;
1631
import io.reactivex.Observable;
1732
import io.reactivex.Single;
1833

@@ -103,5 +118,47 @@ private Single<List<Media>> responseToMediaList(Observable<MwQueryResponse> resp
103118
.map(Media::from)
104119
.collect(ArrayList<Media>::new, List::add);
105120
}
121+
122+
/**
123+
* Fetches Media object from the imageInfo API
124+
*
125+
* @param titles the tiles to be searched for. Can be filename or template name
126+
* @return
127+
*/
128+
public Single<Media> getMedia(String titles) {
129+
return mediaInterface.getMedia(titles)
130+
.flatMap(mwQueryResponse -> {
131+
if (null == mwQueryResponse
132+
|| null == mwQueryResponse.query()
133+
|| null == mwQueryResponse.query().firstPage()) {
134+
return Observable.empty();
135+
}
136+
return Observable.just(mwQueryResponse.query().firstPage());
137+
})
138+
.map(Media::from)
139+
.single(Media.EMPTY);
140+
}
106141

142+
/**
143+
* The method returns the picture of the day
144+
*
145+
* @return Media object corresponding to the picture of the day
146+
*/
147+
@NonNull
148+
public Single<Media> getPictureOfTheDay() {
149+
String date = CommonsDateUtil.getIso8601DateFormatShort().format(new Date());
150+
Timber.d("Current date is %s", date);
151+
String template = "Template:Potd/" + date;
152+
return mediaInterface.getMediaWithGenerator(template)
153+
.flatMap(mwQueryResponse -> {
154+
if (null == mwQueryResponse
155+
|| null == mwQueryResponse.query()
156+
|| null == mwQueryResponse.query().firstPage()) {
157+
return Observable.empty();
158+
}
159+
return Observable.just(mwQueryResponse.query().firstPage());
160+
})
161+
.map(Media::from)
162+
.single(Media.EMPTY);
163+
}
107164
}

app/src/main/java/fr/free/nrw/commons/media/MediaInterface.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public interface MediaInterface {
3232
@GET("w/api.php?action=query&format=json&formatversion=2&list=allimages")
3333
Observable<MwQueryResponse> checkFileExistsUsingSha(@Query("aisha1") String aisha1);
3434

35-
3635
/**
3736
* This method retrieves a list of Media objects filtered using image generator query
3837
*
@@ -63,4 +62,28 @@ public interface MediaInterface {
6362
"|Artist|LicenseShortName|LicenseUrl")
6463
Observable<MwQueryResponse> getMediaListFromSearch(@Query("gsrsearch") String keyword, @Query("gsrlimit") int itemLimit, @QueryMap Map<String, String> continuation);
6564

65+
/**
66+
* Fetches Media object from the imageInfo API
67+
*
68+
* @param title the tiles to be searched for. Can be filename or template name
69+
* @return
70+
*/
71+
@GET("w/api.php?action=query&format=json&formatversion=2" +
72+
"&prop=imageinfo&iiprop=url|extmetadata&iiurlwidth=640" +
73+
"&iiextmetadatafilter=DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal" +
74+
"|Artist|LicenseShortName|LicenseUrl")
75+
Observable<MwQueryResponse> getMedia(@Query("titles") String title);
76+
77+
/**
78+
* Fetches Media object from the imageInfo API
79+
* Passes an image generator parameter
80+
*
81+
* @param title the tiles to be searched for. Can be filename or template name
82+
* @return
83+
*/
84+
@GET("w/api.php?action=query&format=json&formatversion=2&generator=images" +
85+
"&prop=imageinfo&iiprop=url|extmetadata&iiurlwidth=640" +
86+
"&iiextmetadatafilter=DateTime|Categories|GPSLatitude|GPSLongitude|ImageDescription|DateTimeOriginal" +
87+
"|Artist|LicenseShortName|LicenseUrl")
88+
Observable<MwQueryResponse> getMediaWithGenerator(@Query("titles") String title);
6689
}

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import androidx.annotation.NonNull;
66
import androidx.annotation.Nullable;
7+
import com.google.gson.Gson;
78

89
import org.apache.http.conn.ClientConnectionManager;
910
import org.apache.http.conn.scheme.PlainSocketFactory;
@@ -24,6 +25,10 @@
2425

2526
import fr.free.nrw.commons.BuildConfig;
2627
import fr.free.nrw.commons.CommonsApplication;
28+
29+
import fr.free.nrw.commons.BuildConfig;
30+
import fr.free.nrw.commons.CommonsApplication;
31+
2732
import io.reactivex.Single;
2833
import timber.log.Timber;
2934

app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java

-52
Original file line numberDiff line numberDiff line change
@@ -228,58 +228,6 @@ public Single<CampaignResponseDTO> getCampaigns() {
228228
});
229229
}
230230

231-
/**
232-
* The method returns the picture of the day
233-
*
234-
* @return Media object corresponding to the picture of the day
235-
*/
236-
@Nullable
237-
public Single<Media> getPictureOfTheDay() {
238-
String date = CommonsDateUtil.getIso8601DateFormatShort().format(new Date());
239-
Timber.d("Current date is %s", date);
240-
String template = "Template:Potd/" + date;
241-
return getMedia(template, true);
242-
}
243-
244-
/**
245-
* Fetches Media object from the imageInfo API
246-
*
247-
* @param titles the tiles to be searched for. Can be filename or template name
248-
* @param useGenerator specifies if a image generator parameter needs to be passed or not
249-
* @return
250-
*/
251-
public Single<Media> getMedia(String titles, boolean useGenerator) {
252-
HttpUrl.Builder urlBuilder = HttpUrl
253-
.parse(commonsBaseUrl)
254-
.newBuilder()
255-
.addQueryParameter("action", "query")
256-
.addQueryParameter("format", "json")
257-
.addQueryParameter("formatversion", "2")
258-
.addQueryParameter("titles", titles);
259-
260-
if (useGenerator) {
261-
urlBuilder.addQueryParameter("generator", "images");
262-
}
263-
264-
Request request = new Request.Builder()
265-
.url(appendMediaProperties(urlBuilder).build())
266-
.build();
267-
268-
return Single.fromCallable(() -> {
269-
Response response = okHttpClient.newCall(request).execute();
270-
if (response.body() != null && response.isSuccessful()) {
271-
String json = response.body().string();
272-
MwQueryResponse mwQueryPage = gson.fromJson(json, MwQueryResponse.class);
273-
if (mwQueryPage.success() && mwQueryPage.query().firstPage() != null) {
274-
return Media.from(mwQueryPage.query().firstPage());
275-
}
276-
}
277-
return null;
278-
});
279-
}
280-
281-
282-
283231
/**
284232
* Whenever imageInfo is fetched, these common properties can be specified for the API call
285233
* https://www.mediawiki.org/wiki/API:Imageinfo

app/src/main/java/fr/free/nrw/commons/review/ReviewHelper.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,21 @@ public class ReviewHelper {
2525

2626
private static final String[] imageExtensions = new String[]{".jpg", ".jpeg", ".png"};
2727

28-
private final OkHttpJsonApiClient okHttpJsonApiClient;
29-
private final MediaWikiApi mediaWikiApi;
3028
private final MediaClient mediaClient;
3129
private final ReviewInterface reviewInterface;
3230

3331
@Inject
34-
public ReviewHelper(OkHttpJsonApiClient okHttpJsonApiClient,
35-
MediaWikiApi mediaWikiApi,
36-
MediaClient mediaClient, ReviewInterface reviewInterface) {
37-
this.okHttpJsonApiClient = okHttpJsonApiClient;
38-
this.mediaWikiApi = mediaWikiApi;
32+
public ReviewHelper(MediaClient mediaClient, ReviewInterface reviewInterface) {
3933
this.mediaClient = mediaClient;
4034
this.reviewInterface = reviewInterface;
4135
}
4236

4337
/**
44-
* Fetches recent changes from MediaWiki API
38+
* Fetches recent changes from MediaWiki AP
4539
* Calls the API to get 10 changes in the last 1 hour
4640
* Earlier we were getting changes for the last 30 days but as the API returns just 10 results
4741
* its best to fetch for just last 1 hour.
42+
*
4843
* @return
4944
*/
5045
private Observable<RecentChange> getRecentChanges() {
@@ -84,6 +79,7 @@ public Single<Media> getRandomMedia() {
8479
/**
8580
* Returns a proper Media object if the file is not already nominated for deletion
8681
* Else it returns an empty Media object
82+
*
8783
* @param recentChange
8884
* @return
8985
*/
@@ -94,13 +90,14 @@ private Single<Media> getRandomMediaFromRecentChange(RecentChange recentChange)
9490
if (isDeleted) {
9591
return Single.just(new Media(""));
9692
}
97-
return okHttpJsonApiClient.getMedia(recentChange.getTitle(), false);
93+
return mediaClient.getMedia(recentChange.getTitle());
9894
});
9995

10096
}
10197

10298
/**
10399
* Gets the first revision of the file from filename
100+
*
104101
* @param filename
105102
* @return
106103
*/
@@ -113,6 +110,7 @@ Observable<MwQueryPage.Revision> getFirstRevisionOfFile(String filename) {
113110
* Checks if the change is reviewable or not.
114111
* - checks the type and revisionId of the change
115112
* - checks supported image extensions
113+
*
116114
* @param recentChange
117115
* @return
118116
*/

app/src/main/java/fr/free/nrw/commons/widget/PicOfDayAppWidget.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import fr.free.nrw.commons.R;
2828
import fr.free.nrw.commons.contributions.MainActivity;
2929
import fr.free.nrw.commons.di.ApplicationlessInjection;
30+
import fr.free.nrw.commons.media.MediaClient;
3031
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
3132
import io.reactivex.android.schedulers.AndroidSchedulers;
3233
import io.reactivex.disposables.CompositeDisposable;
@@ -42,7 +43,7 @@ public class PicOfDayAppWidget extends AppWidgetProvider {
4243

4344
private CompositeDisposable compositeDisposable = new CompositeDisposable();
4445

45-
@Inject OkHttpJsonApiClient okHttpJsonApiClient;
46+
@Inject MediaClient mediaClient;
4647

4748
void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
4849
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.pic_of_day_app_widget);
@@ -67,7 +68,7 @@ private void loadPictureOfTheDay(Context context,
6768
RemoteViews views,
6869
AppWidgetManager appWidgetManager,
6970
int appWidgetId) {
70-
compositeDisposable.add(okHttpJsonApiClient.getPictureOfTheDay()
71+
compositeDisposable.add(mediaClient.getPictureOfTheDay()
7172
.subscribeOn(Schedulers.io())
7273
.observeOn(AndroidSchedulers.mainThread())
7374
.subscribe(

app/src/test/kotlin/fr/free/nrw/commons/MediaDataExtractorTest.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ class MediaDataExtractorTest {
2424
internal var mwApi: MediaWikiApi? = null
2525

2626
@Mock
27-
internal var okHttpJsonApiClient: OkHttpJsonApiClient? = null
27+
internal var mediaClient: MediaClient? = null
28+
29+
@Mock
30+
internal var mediaClient: MediaClient? = null
31+
32+
@Mock
33+
internal var mediaClient: MediaClient? = null
2834

2935
@Mock
3036
internal var mediaClient: MediaClient? = null
@@ -46,7 +52,7 @@ class MediaDataExtractorTest {
4652
*/
4753
@Test
4854
fun fetchMediaDetails() {
49-
`when`(okHttpJsonApiClient?.getMedia(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean()))
55+
`when`(mediaClient?.getMedia(ArgumentMatchers.anyString()))
5056
.thenReturn(Single.just(mock(Media::class.java)))
5157

5258
`when`(mediaClient?.checkPageExistsUsingTitle(ArgumentMatchers.anyString()))

0 commit comments

Comments
 (0)