From 826ee0238d1d239896bf76dfe518756e8ee2d35a Mon Sep 17 00:00:00 2001 From: Olivier Patry Date: Sat, 6 Jan 2024 00:13:38 +0100 Subject: [PATCH] Workaround weird update task edge cases - if the project id provided in the task is invalid, we receive a 500 error from server - if the project id doesn't match the updated task's one, we receive a 200 success without data (can't be used to move a task to another project) --- .../net/opatry/ticktick/TickTickService.kt | 20 ++++++++++++++++++- .../service/TickTickServiceTaskTest.kt | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/src/main/java/net/opatry/ticktick/TickTickService.kt b/lib/src/main/java/net/opatry/ticktick/TickTickService.kt index 8bd390d..32f721f 100644 --- a/lib/src/main/java/net/opatry/ticktick/TickTickService.kt +++ b/lib/src/main/java/net/opatry/ticktick/TickTickService.kt @@ -127,7 +127,25 @@ class HttpTickTickService(private val httpClient: HttpClient) : TickTickService } override suspend fun updateTask(taskId: String, request: TaskUpdateRequest): Task { - return httpClient.postOrThrow("open/v1/task/${taskId}", request) + require(taskId == request.id) { "Provided task id ($taskId) and update request id (${request.id}) doesn't match." } + // FIXME edge cases encountered + // if the project id provided in the task is invalid, we receive a 500 error from server + // if the project id doesn't match the updated task's one, we receive a 200 success without data (can't be used to move a task to another project) + // return httpClient.postOrThrow("open/v1/task/${taskId}", request) + val response = httpClient.post("open/v1/task/${taskId}") { + contentType(ContentType.Application.Json) + setBody(request) + } + + if (response.status.isSuccess()) { + if (response.bodyAsText().isNotEmpty()) { + return response.body() + } else { + throw ClientRequestException(response, "Task not updated (invalid project id? ${request.projectId})") + } + } else { + throw ClientRequestException(response, response.bodyAsText()) + } } override suspend fun completeTask(projectId: String, taskId: String) { diff --git a/lib/src/test/java/net/opatry/ticktick/service/TickTickServiceTaskTest.kt b/lib/src/test/java/net/opatry/ticktick/service/TickTickServiceTaskTest.kt index 8946f81..99ad705 100644 --- a/lib/src/test/java/net/opatry/ticktick/service/TickTickServiceTaskTest.kt +++ b/lib/src/test/java/net/opatry/ticktick/service/TickTickServiceTaskTest.kt @@ -310,7 +310,7 @@ class TickTickServiceTaskTest { runBlocking { ticktickService.updateTask( "6247ee29630c800f064fd145", - TaskUpdateRequest(projectId = "", id = "", content = "Bar") + TaskUpdateRequest(projectId = "6226ff9877acee87727f6bca", id = "6247ee29630c800f064fd145", content = "Bar") ) } }