fix: handle exceptions while fetching contributors list

This commit is contained in:
Akash Yadav
2023-12-27 11:52:41 +05:30
parent a5b45fc0f8
commit 9a358c42c5
4 changed files with 74 additions and 55 deletions
@@ -0,0 +1,66 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.contributors
import com.itsaky.androidide.utils.ILogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
/**
* @author Akash Yadav
*/
internal object Contributors {
val log: ILogger = ILogger.newInstance("Contributors")
@JvmStatic
suspend inline fun <reified Service, reified Model : Contributor> getAllContributors(
baseUrl: String,
crossinline action: (Service) -> Call<List<Model>>
): List<Model> {
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(Service::class.java)
return withContext(Dispatchers.IO) {
try {
val response = action(service).execute()
if (!response.isSuccessful) {
log.error(
"Failed to get contributors list [${Model::class.java.name}], request unsuccessful",
response.errorBody()?.string() ?: "(empty error response)"
)
return@withContext emptyList()
}
return@withContext response.body() ?: run {
log.error("Response body is null")
emptyList()
}
} catch (err: Throwable) {
err.printStackTrace()
emptyList()
}
}
}
}
@@ -18,12 +18,7 @@
package com.itsaky.androidide.contributors
import com.google.gson.annotations.SerializedName
import com.itsaky.androidide.utils.ILogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
private interface CrowdinTranslatorsService {
@@ -35,30 +30,15 @@ private interface CrowdinTranslatorsService {
object CrowdinTranslators {
internal const val CROWDIN_CONTRIBUTORS_JSON = "crowdin-contributors.json"
private val log = ILogger.newInstance("CrowdinTranslators")
/**
* Get all Crowdin translators.
*/
suspend fun getAllTranslators(): List<CrowdinTranslator> {
val retrofit = Retrofit.Builder()
.baseUrl("${GITHUB_RAW_API_REPO_BRANCH_URL}/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(CrowdinTranslatorsService::class.java)
val response = withContext(Dispatchers.IO) { service.getAllTranslators().execute() }
if (!response.isSuccessful) {
log.error("Failed to get Crowdin translators list, request unsuccessful",
response.errorBody()?.string() ?: "(empty error response)")
return emptyList()
}
return response.body() ?: run {
log.error("Response body is null")
emptyList()
}
return Contributors.getAllContributors<CrowdinTranslatorsService, CrowdinTranslator>(
"${GITHUB_RAW_API_REPO_BRANCH_URL}/",
CrowdinTranslatorsService::getAllTranslators
)
}
}
@@ -18,12 +18,7 @@
package com.itsaky.androidide.contributors
import com.google.gson.annotations.SerializedName
import com.itsaky.androidide.utils.ILogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
/**
@@ -37,30 +32,14 @@ interface GitHubContributorsService {
object GitHubContributors {
private val log = ILogger.newInstance("GitHubContributors")
/**
* Get all GitHub contributors.
*/
suspend fun getAllContributors(): List<GitHubContributor> {
val retrofit = Retrofit.Builder()
.baseUrl("${GITHUB_API_REPO_URL}/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(GitHubContributorsService::class.java)
val response = withContext(Dispatchers.IO) { service.getAllContributors().execute() }
if (!response.isSuccessful) {
log.error("Failed to get GitHub contributors list, request unsuccessful",
response.errorBody()?.string() ?: "(empty error response)")
return emptyList()
}
return response.body() ?: run {
log.error("Response body is null")
emptyList()
}
return Contributors.getAllContributors<GitHubContributorsService, GitHubContributor>(
"${GITHUB_API_REPO_URL}/",
GitHubContributorsService::getAllContributors
)
}
}
@@ -41,12 +41,6 @@ class ContributorsViewModel : ViewModel() {
private const val CONTRIBUTORS_MAX_SIZE = 30
}
val crowdinTranslators: List<CrowdinTranslator>
get() = _crowdinTranslators.value ?: emptyList()
val githubContributors: List<GitHubContributor>
get() = _githubContributors.value ?: emptyList()
fun fetchCrowdinTranslators() {
viewModelScope.launch(Dispatchers.Default) {
val translators = CrowdinTranslators.getAllTranslators()