Skip to content

Commit

Permalink
add v1 api
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Dec 12, 2024
1 parent 7035a16 commit dce1121
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v1

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.status.NextcloudVersion
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Test

class AssistantITV1 : AbstractIT() {
@Before
fun before() {
testOnlyOnServer(NextcloudVersion.nextcloud_28)
}

@Test
fun testGetTaskTypes() {
val result = GetTaskTypesRemoteOperationV1().execute(nextcloudClient)
assertTrue(result.isSuccess)

val taskTypes = result.resultData.types
assertTrue(taskTypes.isNotEmpty())
}

@Test
fun testGetTaskList() {
var result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)
assertTrue(result.resultData.tasks.isEmpty())

// create one task
val input = "Give me some random output for test purpose"
val type = "OCP\\TextProcessing\\FreePromptTaskType"
assertTrue(CreateTaskRemoteOperationV1(input, type).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)

val taskList = result.resultData.tasks
assertTrue(taskList.isNotEmpty())
}

@Test
fun testDeleteTask() {
// create one task
val input = "Give me some random output for test purpose"
val type = "OCP\\TextProcessing\\FreePromptTaskType"
assertTrue(CreateTaskRemoteOperationV1(input, type).execute(nextcloudClient).isSuccess)

var result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)

val tasks = result.resultData.tasks
val countBefore = tasks.size

// delete
assertTrue(DeleteTaskRemoteOperationV1(tasks.first().id).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperationV1("assistant").execute(nextcloudClient)
assertTrue(result.isSuccess)

assertEquals(countBefore - 1, result.resultData.tasks.size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]>
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant
package com.owncloud.android.lib.resources.assistant.v2

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.assistant.model.TaskInputShape
import com.owncloud.android.lib.resources.assistant.model.TaskOutputShape
import com.owncloud.android.lib.resources.assistant.model.TaskTypeData
import com.owncloud.android.lib.resources.assistant.v2.model.TaskInputShape
import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutputShape
import com.owncloud.android.lib.resources.assistant.v2.model.TaskTypeData
import com.owncloud.android.lib.resources.status.NextcloudVersion
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Test

class AssistantIT : AbstractIT() {
class AssistantITV2 : AbstractIT() {
@Before
fun before() {
testOnlyOnServer(NextcloudVersion.nextcloud_30)
Expand Down Expand Up @@ -50,7 +50,7 @@ class AssistantIT : AbstractIT() {

@Test
fun testGetTaskTypes() {
val result = GetTaskTypesRemoteOperation().execute(nextcloudClient)
val result = GetTaskTypesRemoteOperationV2().execute(nextcloudClient)
assertTrue(result.isSuccess)
}

Expand All @@ -59,15 +59,15 @@ class AssistantIT : AbstractIT() {
val taskType = getTaskType()
val selectedTaskType = getSelectedTaskType()

var result = GetTaskListRemoteOperation(selectedTaskType).execute(nextcloudClient)
var result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)
assertTrue(result.resultData.tasks.isEmpty())

// create one task
val input = "Give me some random output for test purpose"
assertTrue(CreateTaskRemoteOperation(input, taskType).execute(nextcloudClient).isSuccess)
assertTrue(CreateTaskRemoteOperationV2(input, taskType).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperation(selectedTaskType).execute(nextcloudClient)
result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)

val taskList = result.resultData.tasks
Expand All @@ -81,18 +81,18 @@ class AssistantIT : AbstractIT() {
val taskType = getTaskType()
val selectedTaskType = getSelectedTaskType()

assertTrue(CreateTaskRemoteOperation(input, taskType).execute(nextcloudClient).isSuccess)
assertTrue(CreateTaskRemoteOperationV2(input, taskType).execute(nextcloudClient).isSuccess)

var result = GetTaskListRemoteOperation(selectedTaskType).execute(nextcloudClient)
var result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)

val tasks = result.resultData.tasks
val countBefore = tasks.size

// delete
assertTrue(DeleteTaskRemoteOperation(tasks.first().id).execute(nextcloudClient).isSuccess)
assertTrue(DeleteTaskRemoteOperationV2(tasks.first().id).execute(nextcloudClient).isSuccess)

result = GetTaskListRemoteOperation(selectedTaskType).execute(nextcloudClient)
result = GetTaskListRemoteOperationV2(selectedTaskType).execute(nextcloudClient)
assertTrue(result.isSuccess)

assertEquals(countBefore - 1, result.resultData.tasks.size)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v1

import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.PostMethod
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.apache.commons.httpclient.HttpStatus

class CreateTaskRemoteOperationV1(private val input: String, private val type: String) :
RemoteOperation<Void>() {
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
val requestBody =
hashMapOf(
"input" to input,
"type" to type,
"appId" to "assistant",
"identifier" to ""
)

val json = gson.toJson(requestBody)

val request = json.toRequestBody("application/json".toMediaTypeOrNull())

val postMethod = PostMethod(client.baseUri.toString() + TAG_URL, true, request)

val status = postMethod.execute(client)

return if (status == HttpStatus.SC_OK) {
RemoteOperationResult<Void>(true, postMethod)
} else {
RemoteOperationResult<Void>(false, postMethod)
}
}

companion object {
const val TAG_URL = "/ocs/v2.php/textprocessing/schedule"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant
package com.owncloud.android.lib.resources.assistant.v1

import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.DeleteMethod
Expand All @@ -17,7 +17,7 @@ import com.owncloud.android.lib.resources.users.DeletePrivateKeyRemoteOperation
import java.io.IOException
import java.net.HttpURLConnection

class DeleteTaskRemoteOperation(private val appId: Long) : RemoteOperation<Void>() {
class DeleteTaskRemoteOperationV1(private val appId: Long) : RemoteOperation<Void>() {
override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
var postMethod: DeleteMethod? = null
var result: RemoteOperationResult<Void>
Expand All @@ -41,6 +41,7 @@ class DeleteTaskRemoteOperation(private val appId: Long) : RemoteOperation<Void>

companion object {
private val TAG = DeletePrivateKeyRemoteOperation::class.java.simpleName
private const val DIRECT_ENDPOINT = "/ocs/v2.php/taskprocessing/task/"
private const val DIRECT_ENDPOINT =
"/ocs/v2.php/textprocessing/task/"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v1

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 com.owncloud.android.lib.resources.assistant.v1.model.TaskList
import org.apache.commons.httpclient.HttpStatus

class GetTaskListRemoteOperationV1(private val appId: String) : OCSRemoteOperation<TaskList>() {
@Suppress("TooGenericExceptionCaught")
override fun run(client: NextcloudClient): RemoteOperationResult<TaskList> {
var result: RemoteOperationResult<TaskList>
var getMethod: GetMethod? = null
try {
getMethod =
GetMethod(client.baseUri.toString() + DIRECT_ENDPOINT + appId + JSON_FORMAT, true)
val status = client.execute(getMethod)
if (status == HttpStatus.SC_OK) {
val taskTypes: TaskList? =
getServerResponse(
getMethod,
object : TypeToken<ServerResponse<TaskList>>() {}
)?.ocs?.data
result = RemoteOperationResult(true, getMethod)
result.setResultData(taskTypes)
} else {
result = RemoteOperationResult(false, getMethod)
}
} catch (e: Exception) {
result = RemoteOperationResult(e)
Log_OC.e(
TAG,
"Get task list for user " + " failed: " + result.logMessage,
result.exception
)
} finally {
getMethod?.releaseConnection()
}
return result
}

companion object {
private val TAG = GetTaskListRemoteOperationV1::class.java.simpleName
private const val DIRECT_ENDPOINT = "/ocs/v2.php/textprocessing/tasks/app/"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v1

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 com.owncloud.android.lib.resources.assistant.v1.model.TaskTypes
import org.apache.commons.httpclient.HttpStatus

class GetTaskTypesRemoteOperationV1 : OCSRemoteOperation<TaskTypes>() {
@Suppress("TooGenericExceptionCaught")
override fun run(client: NextcloudClient): RemoteOperationResult<TaskTypes> {
var result: RemoteOperationResult<TaskTypes>
var getMethod: GetMethod? = null
try {
getMethod =
GetMethod(client.baseUri.toString() + DIRECT_ENDPOINT + JSON_FORMAT, true)
val status = client.execute(getMethod)
if (status == HttpStatus.SC_OK) {
val taskTypes: TaskTypes? =
getServerResponse(
getMethod,
object : TypeToken<ServerResponse<TaskTypes>>() {}
)?.ocs?.data
result = RemoteOperationResult(true, getMethod)
result.setResultData(taskTypes)
} else {
result = RemoteOperationResult(false, getMethod)
}
} catch (e: Exception) {
result = RemoteOperationResult(e)
Log_OC.e(
TAG,
"Get task types for user " + " failed: " + result.logMessage,
result.exception
)
} finally {
getMethod?.releaseConnection()
}
return result
}

companion object {
private val TAG = GetTaskTypesRemoteOperationV1::class.java.simpleName
private const val DIRECT_ENDPOINT = "/ocs/v2.php/textprocessing/tasktypes"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v1.model

data class TaskList(
var tasks: List<Task>
)

data class Task(
val id: Long,
val type: String?,
val status: Long?,
val userId: String?,
val appId: String?,
val input: String?,
val output: String?,
val identifier: String?,
val completionExpectedAt: String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.assistant.v1.model

data class TaskTypes(
var types: List<TaskType>
)

data class TaskType(
val id: String?,
val name: String?,
val description: String?
)
Loading

0 comments on commit dce1121

Please sign in to comment.