Skip to content

Commit

Permalink
Refactor and fix okHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
JorisBodin committed Feb 22, 2022
1 parent 8fe139c commit 420b12e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Core
17 changes: 9 additions & 8 deletions app/src/main/java/com/infomaniak/drive/data/api/ApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.infomaniak.drive.data.api

import androidx.collection.arrayMapOf
import com.google.gson.JsonElement
import com.infomaniak.drive.data.api.ApiRoutes.withFile
import com.infomaniak.drive.data.models.*
import com.infomaniak.drive.data.models.drive.Category
import com.infomaniak.drive.data.models.drive.DriveInfo
Expand Down Expand Up @@ -93,15 +94,15 @@ object ApiRepository {
return callApi(url, GET, okHttpClient = okHttpClient)
}

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"
}
// Increase timeout for this api call because it can take more than 10s to process data
fun getFileActivities(
file: File,
page: Int,
forFileList: Boolean,
okHttpClient: OkHttpClient = HttpClient.okHttpClientLongTimeout,
): ApiResponse<ArrayList<FileActivity>> {
val queries = if (forFileList) "&depth=children&from_date=${file.responseAt}&$withFile" 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
7 changes: 5 additions & 2 deletions app/src/main/java/com/infomaniak/drive/data/api/ApiRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import com.infomaniak.drive.data.models.Team

object ApiRoutes {

private const val with = "with=children,rights,collaborative_folder,favorite,mobile,share_link,categories"
private val with = with("children")
val withFile = with("file")

private fun with(target: String) = "with=$target,rights,collaborative_folder,favorite,mobile,share_link,categories"

private fun fileURL(file: File) = "${DRIVE_API}${file.driveId}/file/${file.id}"

Expand Down Expand Up @@ -111,7 +114,7 @@ object ApiRoutes {
fun dropBox(file: File) = "${fileURL(file)}/collaborate"

fun getLastActivities(driveId: Int) =
"${DRIVE_API}$driveId/file/activity?with=file,rights,collaborative_folder,favorite,mobile,share_link,categories" +
"${DRIVE_API}$driveId/file/activity?$withFile" +
"&depth=unlimited" +
"&actions[]=file_create" +
"&actions[]=file_update" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,11 @@ object FileController {
page: Int,
userDrive: UserDrive? = null
): Map<out Int, FileActivity> {
val okHttpClient = runBlocking {
userDrive?.userId?.let { KDriveHttpClient.getHttpClient(it, 30) } ?: HttpClient.okHttpClientLongTimeout
}
val returnResponse = arrayMapOf<Int, FileActivity>()
val apiResponse = ApiRepository.getFileActivities(folder, page, true)
val apiResponse = ApiRepository.getFileActivities(folder, page, true, okHttpClient)
if (!apiResponse.isSuccess()) return returnResponse

return if (apiResponse.data?.isNotEmpty() == true) {
Expand Down

0 comments on commit 420b12e

Please sign in to comment.