diff --git a/library/src/androidTest/java/com/nextcloud/android/lib/resources/recommendations/GetRecommendationsRemoteOperationIT.kt b/library/src/androidTest/java/com/nextcloud/android/lib/resources/recommendations/GetRecommendationsRemoteOperationIT.kt new file mode 100644 index 000000000..b4d3daa2b --- /dev/null +++ b/library/src/androidTest/java/com/nextcloud/android/lib/resources/recommendations/GetRecommendationsRemoteOperationIT.kt @@ -0,0 +1,24 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me> + * SPDX-License-Identifier: MIT + */ +package com.nextcloud.android.lib.resources.recommendations + +import com.owncloud.android.AbstractIT +import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation +import org.junit.Assert.assertTrue +import org.junit.Test + +class GetRecommendationsRemoteOperationIT : AbstractIT() { + @Test + fun getRecommendations() { + assertTrue(CreateFolderRemoteOperation("/test/", true).execute(client).isSuccess) + + val result = GetRecommendationsRemoteOperation().execute(nextcloudClient).resultData + + assertTrue(result.isNotEmpty()) + } +} diff --git a/library/src/main/java/com/nextcloud/android/lib/resources/recommendations/GetRecommendationsRemoteOperation.kt b/library/src/main/java/com/nextcloud/android/lib/resources/recommendations/GetRecommendationsRemoteOperation.kt new file mode 100644 index 000000000..ec730c4c6 --- /dev/null +++ b/library/src/main/java/com/nextcloud/android/lib/resources/recommendations/GetRecommendationsRemoteOperation.kt @@ -0,0 +1,68 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me> + * SPDX-License-Identifier: MIT + */ +package com.nextcloud.android.lib.resources.recommendations + +import com.google.gson.reflect.TypeToken +import com.nextcloud.common.NextcloudClient +import com.nextcloud.operations.GetMethod +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.ocs.ServerResponse +import com.owncloud.android.lib.resources.OCSRemoteOperation +import org.apache.commons.httpclient.HttpStatus + +/** + * Get recommendation of an user + */ +class GetRecommendationsRemoteOperation : + OCSRemoteOperation<List<Recommendation>>() { + @Suppress("TooGenericExceptionCaught") + override fun run(client: NextcloudClient): RemoteOperationResult<List<Recommendation>> { + var result: RemoteOperationResult<List<Recommendation>> + var getMethod: GetMethod? = null + try { + getMethod = + GetMethod( + client.baseUri.toString() + ENDPOINT + JSON_FORMAT, + true + ) + val status = client.execute(getMethod) + if (status == HttpStatus.SC_OK) { + val map = + getServerResponse( + getMethod, + object : TypeToken<ServerResponse<List<Recommendation>>>() {} + )?.ocs?.data + + if (map != null) { + result = RemoteOperationResult(true, getMethod) + result.setResultData(map) + } else { + result = RemoteOperationResult(false, getMethod) + } + } else { + result = RemoteOperationResult(false, getMethod) + } + } catch (e: Exception) { + result = RemoteOperationResult(e) + Log_OC.e( + TAG, + "Get recommendations failed: " + result.logMessage, + result.exception + ) + } finally { + getMethod?.releaseConnection() + } + return result + } + + companion object { + private val TAG = GetRecommendationsRemoteOperation::class.java.simpleName + private const val ENDPOINT = "/ocs/v2.php/apps/recommendations/api/v1/recommendations" + } +} diff --git a/library/src/main/java/com/nextcloud/android/lib/resources/recommendations/Recommendation.kt b/library/src/main/java/com/nextcloud/android/lib/resources/recommendations/Recommendation.kt new file mode 100644 index 000000000..70eca3376 --- /dev/null +++ b/library/src/main/java/com/nextcloud/android/lib/resources/recommendations/Recommendation.kt @@ -0,0 +1,20 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me> + * SPDX-License-Identifier: MIT + */ + +package com.nextcloud.android.lib.resources.recommendations + +data class Recommendation( + val id: Long, + val timestamp: Long, + val name: String, + val directory: String, + val extension: String, + val mimeType: String, + val hasPreview: Boolean, + val reason: String +)