Skip to content

Commit

Permalink
use suspend function for network calls
Browse files Browse the repository at this point in the history
Signed-off-by: parneet-guraya <[email protected]>
  • Loading branch information
parneet-guraya committed Dec 8, 2024
1 parent 8c851ba commit afe267f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 48 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:5.10.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.0"
testImplementation 'com.facebook.soloader:soloader:0.10.5'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.9.0"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3"
debugImplementation("androidx.fragment:fragment-testing:1.6.2")
testImplementation "commons-io:commons-io:2.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx2.awaitSingle
import timber.log.Timber
import javax.inject.Inject

Expand All @@ -39,7 +38,7 @@ class MediaDetailViewModel(

try {
val result =
okHttpJsonApiClient.getFileUsagesOnCommons(fileName, 10).awaitSingle()
okHttpJsonApiClient.getFileUsagesOnCommons(fileName, 10)

val data = result?.query?.pages?.first()?.fileUsage?.map { it.toUiModel() }

Expand Down Expand Up @@ -69,7 +68,7 @@ class MediaDetailViewModel(
_globalContainerState.update { FileUsagesContainerState.Loading }

try {
val result = okHttpJsonApiClient.getGlobalFileUsages(fileName, 10).awaitSingle()
val result = okHttpJsonApiClient.getGlobalFileUsages(fileName, 10)

val data = result?.query?.pages?.first()?.fileUsage?.map { it.toUiModel() }

Expand Down
98 changes: 54 additions & 44 deletions app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
import fr.free.nrw.commons.wikidata.model.GetWikidataEditCountResponse
import io.reactivex.Observable
import io.reactivex.Single
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -53,8 +55,10 @@ class OkHttpJsonApiClient @Inject constructor(
): Observable<LeaderboardResponse> {
val fetchLeaderboardUrlTemplate =
wikiMediaToolforgeUrl.toString() + LeaderboardConstants.LEADERBOARD_END_POINT
val url = String.format(Locale.ENGLISH,
fetchLeaderboardUrlTemplate, userName, duration, category, limit, offset)
val url = String.format(
Locale.ENGLISH,
fetchLeaderboardUrlTemplate, userName, duration, category, limit, offset
)
val urlBuilder: HttpUrl.Builder = url.toHttpUrlOrNull()!!.newBuilder()
.addQueryParameter("user", userName)
.addQueryParameter("duration", duration)
Expand Down Expand Up @@ -83,66 +87,72 @@ class OkHttpJsonApiClient @Inject constructor(
})
}

fun getFileUsagesOnCommons(
suspend fun getFileUsagesOnCommons(
fileName: String?,
pageSize: Int
): Observable<FileUsagesResponse?> {
val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder()
urlBuilder.addQueryParameter("prop", "fileusage")
urlBuilder.addQueryParameter("titles", fileName)
urlBuilder.addQueryParameter("fulimit", pageSize.toString())
): FileUsagesResponse? {
return withContext(Dispatchers.IO) {

Timber.i("Url %s", urlBuilder.toString())
val request: Request = Request.Builder()
.url(urlBuilder.toString())
.build()
return@withContext try {

return Observable.fromCallable({
val response: Response = okHttpClient.newCall(request).execute()
if (response.body != null && response.isSuccessful) {
val json: String = response.body!!.string()
try {
return@fromCallable gson.fromJson<FileUsagesResponse>(
val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder()
urlBuilder.addQueryParameter("prop", "fileusage")
urlBuilder.addQueryParameter("titles", fileName)
urlBuilder.addQueryParameter("fulimit", pageSize.toString())

Timber.i("Url %s", urlBuilder.toString())
val request: Request = Request.Builder()
.url(urlBuilder.toString())
.build()

val response: Response = okHttpClient.newCall(request).execute()
if (response.body != null && response.isSuccessful) {
val json: String = response.body!!.string()
gson.fromJson<FileUsagesResponse>(
json,
FileUsagesResponse::class.java
)
} catch (e: java.lang.Exception) {
return@fromCallable null
}
} else null
} catch (e: Exception) {
Timber.e(e)
null
}
null
})
}
}

fun getGlobalFileUsages(
suspend fun getGlobalFileUsages(
fileName: String?,
pageSize: Int
): Observable<GlobalFileUsagesResponse?> {
val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder()
urlBuilder.addQueryParameter("prop", "globalusage")
urlBuilder.addQueryParameter("titles", fileName)
urlBuilder.addQueryParameter("gulimit", pageSize.toString())
): GlobalFileUsagesResponse? {

Timber.i("Url %s", urlBuilder.toString())
val request: Request = Request.Builder()
.url(urlBuilder.toString())
.build()
return withContext(Dispatchers.IO) {

return Observable.fromCallable({
val response: Response = okHttpClient.newCall(request).execute()
if (response.body != null && response.isSuccessful) {
val json: String = response.body!!.string()
try {
return@fromCallable gson.fromJson<GlobalFileUsagesResponse>(
return@withContext try {

val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder()
urlBuilder.addQueryParameter("prop", "globalusage")
urlBuilder.addQueryParameter("titles", fileName)
urlBuilder.addQueryParameter("gulimit", pageSize.toString())

Timber.i("Url %s", urlBuilder.toString())
val request: Request = Request.Builder()
.url(urlBuilder.toString())
.build()

val response: Response = okHttpClient.newCall(request).execute()
if (response.body != null && response.isSuccessful) {
val json: String = response.body!!.string()

gson.fromJson<GlobalFileUsagesResponse>(
json,
GlobalFileUsagesResponse::class.java
)
} catch (e: java.lang.Exception) {
return@fromCallable null
}
} else null
} catch (e: Exception) {
Timber.e(e)
null
}
null
})
}
}

fun setAvatar(username: String?, avatar: String?): Single<UpdateAvatarResponse?> {
Expand Down

0 comments on commit afe267f

Please sign in to comment.