Skip to content

Commit

Permalink
refactor timeout fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 22, 2022
1 parent b55b17a commit 8fe139c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ import com.infomaniak.drive.utils.ApiTestUtils.createFileForTest
import com.infomaniak.drive.utils.ApiTestUtils.deleteTestFile
import com.infomaniak.drive.utils.ApiTestUtils.getCategory
import com.infomaniak.drive.utils.ApiTestUtils.getShareLink
import com.infomaniak.drive.utils.KDriveHttpClient
import com.infomaniak.drive.utils.Utils.ROOT_ID
import kotlinx.coroutines.runBlocking
import org.junit.*
import org.junit.runner.RunWith

Expand Down Expand Up @@ -129,8 +127,7 @@ class ApiRepositoryTest : KDriveTest() {

@Test
fun getFileActivities() {
val okHttpClientWithTimeout = runBlocking { KDriveHttpClient.getHttpClient(user.id, 30) }
with(getFileActivities(okHttpClientWithTimeout, testFile, 1)) {
with(getFileActivities(testFile, 1, false)) {
if (isSuccess()) {
assertApiResponse(this)
} else {
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/com/infomaniak/drive/data/api/ApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import com.google.gson.JsonElement
import com.infomaniak.drive.data.models.*
import com.infomaniak.drive.data.models.drive.Category
import com.infomaniak.drive.data.models.drive.DriveInfo
import com.infomaniak.drive.utils.AccountUtils
import com.infomaniak.drive.utils.KDriveHttpClient
import com.infomaniak.lib.core.models.ApiResponse
import com.infomaniak.lib.core.models.User
import com.infomaniak.lib.core.networking.HttpClient
import com.infomaniak.lib.core.utils.ApiController.ApiMethod.*
import com.infomaniak.lib.core.utils.ApiController.callApi
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient

object ApiRepository {
Expand Down Expand Up @@ -90,9 +93,15 @@ object ApiRepository {
return callApi(url, GET, okHttpClient = okHttpClient)
}

fun getFileActivities(okHttpClient: OkHttpClient, file: File, page: Int): ApiResponse<ArrayList<FileActivity>> {
val url = "${ApiRoutes.getFileActivities(file)}?${pagination(page)}&depth=children&from_date=${file.responseAt}" +
"&with=file,rights,collaborative_folder,favorite,share_link,mobile,categories" + ACTIONS
fun getFileActivities(file: File, page: Int, forFileList: Boolean): ApiResponse<ArrayList<FileActivity>> {
val queries = if (forFileList) {
"&depth=children&from_date=${file.responseAt}&with=file,rights,collaborative_folder,favorite,share_link,mobile,categories"
} else {
"&with=user"
}
val url = "${ApiRoutes.getFileActivities(file)}?${pagination(page)}$queries$ACTIONS"
// Increase timeout for this api call because it can take more than 10s to process data
val okHttpClient = runBlocking { KDriveHttpClient.getHttpClient(AccountUtils.currentUserId, 30) }
return callApi(url, GET, okHttpClient = okHttpClient)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,11 +748,8 @@ object FileController {
page: Int,
userDrive: UserDrive? = null
): Map<out Int, FileActivity> {
val okHttpClient = runBlocking {
userDrive?.userId?.let { KDriveHttpClient.getHttpClient(it) } ?: HttpClient.okHttpClient
}
val returnResponse = arrayMapOf<Int, FileActivity>()
val apiResponse = ApiRepository.getFileActivities(okHttpClient, folder, page)
val apiResponse = ApiRepository.getFileActivities(folder, page, true)
if (!apiResponse.isSuccess()) return returnResponse

return if (apiResponse.data?.isNotEmpty() == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.models.FileActivity
import com.infomaniak.drive.data.models.FileComment
import com.infomaniak.drive.data.models.Share
import com.infomaniak.drive.utils.AccountUtils
import com.infomaniak.drive.utils.KDriveHttpClient
import com.infomaniak.lib.core.models.ApiResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -46,9 +44,7 @@ class FileDetailsViewModel : ViewModel() {

return liveData(Dispatchers.IO + getFileActivitiesJob) {
suspend fun recursive(page: Int) {
// Increase timeout for this api call because it can take more than 10s to process data
val okHttpClient = KDriveHttpClient.getHttpClient(AccountUtils.currentUserId, 30)
val apiRepository = ApiRepository.getFileActivities(okHttpClient, file, page)
val apiRepository = ApiRepository.getFileActivities(file, page, false)
if (apiRepository.isSuccess()) {
when {
apiRepository.data?.isNullOrEmpty() == true -> emit(null)
Expand Down

0 comments on commit 8fe139c

Please sign in to comment.